WIP: icon database

This commit is contained in:
Ray committed 2021-09-21 13:28:52 +01:00
1 parent 682af23f55
commit 4dd399f537
9 files changed
+342 -46

No files matched your search

+15
View File
@@ -0,0 +1,15 @@
using bzit.bomg.Models;
using System.Drawing;
namespace BookmarkManager
{
public class BookmarkResult
{
public BookmarkItem Item { get; set; } = null;
public string IconURL { get; set; } = null;
public Image Icon { get; set; } = null;
}
}
+22
View File
@@ -0,0 +1,22 @@
using System;
using System.Security.Cryptography;
using System.Text;
namespace BookmarkManager
{
public class Crypto
{
public static string GetSHA256Hash(string text)
{
if (string.IsNullOrWhiteSpace(text)) return string.Empty;
using (var sha = new SHA256Managed())
{
byte[] hash = sha.ComputeHash(Encoding.UTF8.GetBytes(text));
return BitConverter.ToString(hash).Replace("-", string.Empty) + "+" + text.Length.ToString();
}
}
}
}
+244
View File
@@ -0,0 +1,244 @@
using Microsoft.Data.Sqlite;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace BookmarkManager
{
public class IconDatabase
{
protected string dbFilename = null;
protected SqliteConnection dbConnection = null;
public IconDatabase(string filename)
{
if (string.IsNullOrWhiteSpace(filename)) return;
dbFilename = filename;
bool isNew = !File.Exists(dbFilename);
SqliteConnectionStringBuilder builder = new SqliteConnectionStringBuilder();
builder.DataSource = dbFilename;
builder.Cache = SqliteCacheMode.Private;
builder.Mode = SqliteOpenMode.ReadWriteCreate;
dbConnection = new SqliteConnection(builder.ToString());
dbConnection.Open();
if (isNew) Initialise();
//if (File.Exists(filename))
//{
// File.Delete("test.db3");
//}
}
public bool AddIcon(string id, Image image)
{
if (dbConnection == null) return false;
if (string.IsNullOrWhiteSpace(id)) return false;
if (image == null) return DeleteIcon(id);
if (HasIcon(id)) return UpdateIcon(id, image);
SqliteCommand command = new SqliteCommand();
command.Connection = dbConnection;
command.CommandText = "INSERT INTO icons (id, content) VALUES (@id, @content)";
command.Parameters.Add("@id", SqliteType.Text).Value = id;
command.Parameters.Add("@content", SqliteType.Blob, 20).Value = ImageToBytes(image);
try
{
command.ExecuteNonQuery();
}
catch (Exception)
{
return false;
}
return true;
}
public void Close()
{
if (dbConnection == null) return;
dbConnection.Close();
dbConnection.Dispose();
dbConnection = null;
}
public bool DeleteIcon(string id)
{
if (dbConnection == null) return false;
if (string.IsNullOrWhiteSpace(id)) return false;
SqliteCommand command = new SqliteCommand();
command.Connection = dbConnection;
command.CommandText = "DELETE FROM icons WHERE id = @id";
command.Parameters.Add("@id", SqliteType.Text).Value = id;
try
{
command.ExecuteNonQuery();
}
catch (Exception)
{
return false;
}
return true;
}
public Image FindIcon(string id)
{
if (dbConnection == null) return null;
if (string.IsNullOrWhiteSpace(id)) return null;
byte[] iconBytes = null;
SqliteCommand command = new SqliteCommand();
command.Connection = dbConnection;
command.CommandText = "SELECT content FROM icons WHERE @id";
command.Parameters.Add("@id", SqliteType.Text).Value = id;
try
{
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
if (reader["content"] != null && !Convert.IsDBNull(reader["content"]))
{
iconBytes = (byte[])reader["content"];
}
}
}
command.Dispose();
}
catch (Exception)
{
return null;
}
if (iconBytes == null)
{
return null;
}
Image img = null;
try
{
img = Image.FromStream(new MemoryStream(iconBytes));
}
catch (Exception)
{
return null;
}
return new Bitmap(img, 16, 16);
}
public bool HasIcon(string id)
{
if (dbConnection == null) return false;
if (string.IsNullOrWhiteSpace(id)) return false;
int rv = 0;
SqliteCommand command = new SqliteCommand();
command.Connection = dbConnection;
command.CommandText = "SELECT COUNT(1) AS count FROM icons WHERE @id";
command.Parameters.Add("@id", SqliteType.Text).Value = id;
try
{
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
if (reader["count"] != null && !Convert.IsDBNull(reader["count"]))
{
rv = (int)reader["count"];
}
}
}
command.Dispose();
}
catch (Exception)
{
return false;
}
return (rv > 0);
}
public bool UpdateIcon(string id, Image image)
{
if (dbConnection == null) return false;
if (string.IsNullOrWhiteSpace(id)) return false;
if (image == null) return DeleteIcon(id);
if (!HasIcon(id)) return AddIcon(id, image);
SqliteCommand command = new SqliteCommand();
command.Connection = dbConnection;
command.CommandText = "UPDATE icons SET content = @content WHERE id = @id";
command.Parameters.Add("@id", SqliteType.Text).Value = id;
command.Parameters.Add("@content", SqliteType.Blob, 20).Value = ImageToBytes(image);
try
{
command.ExecuteNonQuery();
}
catch (Exception)
{
return false;
}
return true;
}
protected bool Initialise()
{
if (dbConnection == null) return false;
try
{
SqliteCommand command = new SqliteCommand("CREATE TABLE icons (id TEXT PRIMARY KEY, content BLOB)", dbConnection);
command.ExecuteNonQuery();
command.Dispose();
}
catch (Exception)
{
return false;
}
return true;
}
protected byte[] ImageToBytes(Image image)
{
MemoryStream stream = new MemoryStream();
//image.Save(stream, ((image.RawFormat == null) ? ImageFormat.Bmp : new ImageFormat(image.RawFormat.Guid)));
image.Save(stream, ImageFormat.Bmp);
stream.Close();
return stream.ToArray();
}
}
}
+10 -9
View File
@@ -11,7 +11,7 @@ namespace BookmarkManager
protected HttpWeb webClient = null;
public BookmarkItem RetrieveDetails(string url)
public BookmarkResult RetrieveDetails(string url)
{
string sourceCode = retrieveSourceCode(url);
if (string.IsNullOrWhiteSpace(sourceCode))
@@ -19,24 +19,25 @@ namespace BookmarkManager
return null;
}
BookmarkItem rs = new BookmarkItem();
BookmarkResult rs = new BookmarkResult();
rs.Item = new BookmarkItem();
HtmlDocument document = new HtmlDocument();
document.LoadHtml(sourceCode);
rs.SiteName = parseSiteTitle(document);
rs.SiteAddress = url;
rs.SiteDescription = parseSiteDescription(document);
rs.FaviconAddress = parseSiteIcon(document);
rs.Item.SiteName = parseSiteTitle(document);
rs.Item.SiteAddress = url;
rs.Item.SiteDescription = parseSiteDescription(document);
rs.IconURL = parseSiteIcon(document);
// resolve relative URL
if (!string.IsNullOrWhiteSpace(rs.FaviconAddress))
if (!string.IsNullOrWhiteSpace(rs.IconURL))
{
Uri iconAddressURI;
bool rv = Uri.TryCreate(new Uri(url), rs.FaviconAddress, out iconAddressURI);
bool rv = Uri.TryCreate(new Uri(url), rs.IconURL, out iconAddressURI);
if (rv)
{
rs.FaviconAddress = iconAddressURI.ToString();
rs.IconURL = iconAddressURI.ToString();
}
}