Skip to content

Commit d7b8f85

Browse files
committed
Use FaviconHelper
1 parent 2ed32b3 commit d7b8f85

File tree

3 files changed

+92
-123
lines changed

3 files changed

+92
-123
lines changed

Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromiumBookmarkLoader.cs

Lines changed: 11 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.IO;
55
using System.Text.Json;
66
using System.Threading.Tasks;
7+
using Flow.Launcher.Plugin.BrowserBookmark.Helper;
78
using Flow.Launcher.Plugin.BrowserBookmark.Models;
89
using Microsoft.Data.Sqlite;
910

@@ -131,31 +132,7 @@ private static void EnumerateFolderBookmark(JsonElement folderElement, ICollecti
131132

132133
private void LoadFaviconsFromDb(string dbPath, List<Bookmark> bookmarks)
133134
{
134-
// Use a copy to avoid lock issues with the original file
135-
var tempDbPath = Path.Combine(_faviconCacheDir, $"tempfavicons_{Guid.NewGuid()}.db");
136-
137-
try
138-
{
139-
File.Copy(dbPath, tempDbPath, true);
140-
}
141-
catch (Exception ex)
142-
{
143-
try
144-
{
145-
if (File.Exists(tempDbPath))
146-
{
147-
File.Delete(tempDbPath);
148-
}
149-
}
150-
catch (Exception ex1)
151-
{
152-
Main._context.API.LogException(ClassName, $"Failed to delete temporary favicon DB: {tempDbPath}", ex1);
153-
}
154-
Main._context.API.LogException(ClassName, $"Failed to copy favicon DB: {dbPath}", ex);
155-
return;
156-
}
157-
158-
try
135+
FaviconHelper.LoadFaviconsFromDb(_faviconCacheDir, dbPath, (tempDbPath) =>
159136
{
160137
// Since some bookmarks may have same favicon id, we need to record them to avoid duplicates
161138
var savedPaths = new ConcurrentDictionary<string, bool>();
@@ -181,13 +158,13 @@ private void LoadFaviconsFromDb(string dbPath, List<Bookmark> bookmarks)
181158

182159
using var cmd = connection.CreateCommand();
183160
cmd.CommandText = @"
184-
SELECT f.id, b.image_data
185-
FROM favicons f
186-
JOIN favicon_bitmaps b ON f.id = b.icon_id
187-
JOIN icon_mapping m ON f.id = m.icon_id
188-
WHERE m.page_url LIKE @url
189-
ORDER BY b.width DESC
190-
LIMIT 1";
161+
SELECT f.id, b.image_data
162+
FROM favicons f
163+
JOIN favicon_bitmaps b ON f.id = b.icon_id
164+
JOIN icon_mapping m ON f.id = m.icon_id
165+
WHERE m.page_url LIKE @url
166+
ORDER BY b.width DESC
167+
LIMIT 1";
191168

192169
cmd.Parameters.AddWithValue("@url", $"%{domain}%");
193170

@@ -206,7 +183,7 @@ ORDER BY b.width DESC
206183
// Filter out duplicate favicons
207184
if (savedPaths.TryAdd(faviconPath, true))
208185
{
209-
SaveBitmapData(imageData, faviconPath);
186+
FaviconHelper.SaveBitmapData(imageData, faviconPath);
210187
}
211188

212189
bookmark.FaviconPath = faviconPath;
@@ -223,32 +200,6 @@ ORDER BY b.width DESC
223200
connection.Dispose();
224201
}
225202
});
226-
}
227-
catch (Exception ex)
228-
{
229-
Main._context.API.LogException(ClassName, $"Failed to connect to SQLite: {tempDbPath}", ex);
230-
}
231-
232-
// Delete temporary file
233-
try
234-
{
235-
File.Delete(tempDbPath);
236-
}
237-
catch (Exception ex)
238-
{
239-
Main._context.API.LogException(ClassName, $"Failed to delete temporary favicon DB: {tempDbPath}", ex);
240-
}
241-
}
242-
243-
private static void SaveBitmapData(byte[] imageData, string outputPath)
244-
{
245-
try
246-
{
247-
File.WriteAllBytes(outputPath, imageData);
248-
}
249-
catch (Exception ex)
250-
{
251-
Main._context.API.LogException(ClassName, $"Failed to save image: {outputPath}", ex);
252-
}
203+
});
253204
}
254205
}

Plugins/Flow.Launcher.Plugin.BrowserBookmark/FirefoxBookmarkLoader.cs

Lines changed: 5 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.IO;
55
using System.Linq;
66
using System.Threading.Tasks;
7+
using Flow.Launcher.Plugin.BrowserBookmark.Helper;
78
using Flow.Launcher.Plugin.BrowserBookmark.Models;
89
using Microsoft.Data.Sqlite;
910

@@ -118,31 +119,7 @@ protected List<Bookmark> GetBookmarksFromPath(string placesPath)
118119

119120
private void LoadFaviconsFromDb(string dbPath, List<Bookmark> bookmarks)
120121
{
121-
// Use a copy to avoid lock issues with the original file
122-
var tempDbPath = Path.Combine(_faviconCacheDir, $"tempfavicons_{Guid.NewGuid()}.sqlite");
123-
124-
try
125-
{
126-
File.Copy(dbPath, tempDbPath, true);
127-
}
128-
catch (Exception ex)
129-
{
130-
try
131-
{
132-
if (File.Exists(tempDbPath))
133-
{
134-
File.Delete(tempDbPath);
135-
}
136-
}
137-
catch (Exception ex1)
138-
{
139-
Main._context.API.LogException(ClassName, $"Failed to delete temporary favicon DB: {tempDbPath}", ex1);
140-
}
141-
Main._context.API.LogException(ClassName, $"Failed to copy favicon DB: {dbPath}", ex);
142-
return;
143-
}
144-
145-
try
122+
FaviconHelper.LoadFaviconsFromDb(_faviconCacheDir, dbPath, (tempDbPath) =>
146123
{
147124
// Since some bookmarks may have same favicon id, we need to record them to avoid duplicates
148125
var savedPaths = new ConcurrentDictionary<string, bool>();
@@ -190,7 +167,7 @@ ORDER BY i.width DESC -- Select largest icon available
190167
return;
191168

192169
string faviconPath;
193-
if (IsSvgData(imageData))
170+
if (FaviconHelper.IsSvgData(imageData))
194171
{
195172
faviconPath = Path.Combine(_faviconCacheDir, $"firefox_{domain}.svg");
196173
}
@@ -202,7 +179,7 @@ ORDER BY i.width DESC -- Select largest icon available
202179
// Filter out duplicate favicons
203180
if (savedPaths.TryAdd(faviconPath, true))
204181
{
205-
SaveBitmapData(imageData, faviconPath);
182+
FaviconHelper.SaveBitmapData(imageData, faviconPath);
206183
}
207184

208185
bookmark.FaviconPath = faviconPath;
@@ -219,42 +196,7 @@ ORDER BY i.width DESC -- Select largest icon available
219196
connection.Dispose();
220197
}
221198
});
222-
}
223-
catch (Exception ex)
224-
{
225-
Main._context.API.LogException(ClassName, $"Failed to load Firefox favicon DB: {tempDbPath}", ex);
226-
}
227-
228-
// Delete temporary file
229-
try
230-
{
231-
File.Delete(tempDbPath);
232-
}
233-
catch (Exception ex)
234-
{
235-
Main._context.API.LogException(ClassName, $"Failed to delete temporary favicon DB: {tempDbPath}", ex);
236-
}
237-
}
238-
239-
private static void SaveBitmapData(byte[] imageData, string outputPath)
240-
{
241-
try
242-
{
243-
File.WriteAllBytes(outputPath, imageData);
244-
}
245-
catch (Exception ex)
246-
{
247-
Main._context.API.LogException(ClassName, $"Failed to save image: {outputPath}", ex);
248-
}
249-
}
250-
251-
private static bool IsSvgData(byte[] data)
252-
{
253-
if (data.Length < 5)
254-
return false;
255-
string start = System.Text.Encoding.ASCII.GetString(data, 0, Math.Min(100, data.Length));
256-
return start.Contains("<svg") ||
257-
(start.StartsWith("<?xml") && start.Contains("<svg"));
199+
});
258200
}
259201
}
260202

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System;
2+
using System.IO;
3+
4+
namespace Flow.Launcher.Plugin.BrowserBookmark.Helper;
5+
6+
public static class FaviconHelper
7+
{
8+
private static readonly string ClassName = nameof(FaviconHelper);
9+
10+
public static void LoadFaviconsFromDb(string faviconCacheDir, string dbPath, Action<string> loadAction)
11+
{
12+
// Use a copy to avoid lock issues with the original file
13+
var tempDbPath = Path.Combine(faviconCacheDir, $"tempfavicons_{Guid.NewGuid()}.db");
14+
15+
try
16+
{
17+
File.Copy(dbPath, tempDbPath, true);
18+
}
19+
catch (Exception ex)
20+
{
21+
try
22+
{
23+
if (File.Exists(tempDbPath))
24+
{
25+
File.Delete(tempDbPath);
26+
}
27+
}
28+
catch (Exception ex1)
29+
{
30+
Main._context.API.LogException(ClassName, $"Failed to delete temporary favicon DB: {tempDbPath}", ex1);
31+
}
32+
Main._context.API.LogException(ClassName, $"Failed to copy favicon DB: {dbPath}", ex);
33+
return;
34+
}
35+
36+
try
37+
{
38+
loadAction(tempDbPath);
39+
}
40+
catch (Exception ex)
41+
{
42+
Main._context.API.LogException(ClassName, $"Failed to connect to SQLite: {tempDbPath}", ex);
43+
}
44+
45+
// Delete temporary file
46+
try
47+
{
48+
File.Delete(tempDbPath);
49+
}
50+
catch (Exception ex)
51+
{
52+
Main._context.API.LogException(ClassName, $"Failed to delete temporary favicon DB: {tempDbPath}", ex);
53+
}
54+
}
55+
56+
public static void SaveBitmapData(byte[] imageData, string outputPath)
57+
{
58+
try
59+
{
60+
File.WriteAllBytes(outputPath, imageData);
61+
}
62+
catch (Exception ex)
63+
{
64+
Main._context.API.LogException(ClassName, $"Failed to save image: {outputPath}", ex);
65+
}
66+
}
67+
68+
public static bool IsSvgData(byte[] data)
69+
{
70+
if (data.Length < 5)
71+
return false;
72+
string start = System.Text.Encoding.ASCII.GetString(data, 0, Math.Min(100, data.Length));
73+
return start.Contains("<svg") ||
74+
(start.StartsWith("<?xml") && start.Contains("<svg"));
75+
}
76+
}

0 commit comments

Comments
 (0)