Skip to content

Commit fa9c7c2

Browse files
committed
review feedback
1 parent 2cfc1a2 commit fa9c7c2

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

Plugins/Flow.Launcher.Plugin.BrowserBookmark/Services/FaviconService.cs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ private async Task SaveFailedFetchesAsync()
8383
await File.WriteAllTextAsync(_failsFilePath, json, _cts.Token);
8484
}
8585
catch (OperationCanceledException) { /* Swallow if app is closing */ }
86+
catch (ObjectDisposedException) { /* Swallow if disposing */ }
8687
catch (Exception ex)
8788
{
8889
_context.API.LogException(nameof(FaviconService), $"Failed to save failed favicons file to {_failsFilePath}", ex);
@@ -102,22 +103,37 @@ public async Task ProcessBookmarkFavicons(IReadOnlyList<Bookmark> bookmarks, Can
102103

103104
await Parallel.ForEachAsync(bookmarks, options, async (bookmark, token) =>
104105
{
105-
var cachePath = GetCachePath(bookmark.Url, _faviconCacheDir);
106-
if (File.Exists(cachePath))
106+
var pageCachePath = GetCachePath(bookmark.Url, _faviconCacheDir);
107+
var hostCachePath = Uri.TryCreate(bookmark.Url, UriKind.Absolute, out var pageUri)
108+
? GetCachePath(pageUri.GetLeftPart(UriPartial.Authority), _faviconCacheDir)
109+
: pageCachePath;
110+
if (File.Exists(hostCachePath))
107111
{
108-
bookmark.FaviconPath = cachePath;
112+
bookmark.FaviconPath = hostCachePath;
113+
return;
114+
}
115+
if (File.Exists(pageCachePath))
116+
{
117+
bookmark.FaviconPath = pageCachePath;
109118
return;
110119
}
111-
112120
// 1. Try local browser database
113121
var localData = await _localExtractor.GetFaviconDataAsync(bookmark, token);
114122
if (localData != null)
115123
{
116124
var (pngData, _) = await _imageConverter.ToPngAsync(new MemoryStream(localData), token);
117125
if (pngData != null)
118126
{
119-
await File.WriteAllBytesAsync(cachePath, pngData, token);
120-
bookmark.FaviconPath = cachePath;
127+
var path = hostCachePath;
128+
try
129+
{
130+
await File.WriteAllBytesAsync(path, pngData, token);
131+
}
132+
catch (IOException)
133+
{
134+
// Another thread may have created it concurrently.
135+
}
136+
bookmark.FaviconPath = path;
121137
return;
122138
}
123139
}
@@ -132,6 +148,7 @@ await Parallel.ForEachAsync(bookmarks, options, async (bookmark, token) =>
132148
}
133149
}
134150
});
151+
135152
}
136153

137154
private async Task<string?> GetFaviconFromWebAsync(Uri url, CancellationToken token)
@@ -211,7 +228,14 @@ private static string GetCachePath(string url, string cacheDir)
211228

212229
if (bestResult.PngData != null)
213230
{
214-
await File.WriteAllBytesAsync(cachePath, bestResult.PngData, _cts.Token);
231+
try
232+
{
233+
await File.WriteAllBytesAsync(cachePath, bestResult.PngData, _cts.Token);
234+
}
235+
catch (IOException)
236+
{
237+
// Another thread may have created it concurrently.
238+
}
215239
_context.API.LogDebug(nameof(FaviconService), $"Favicon for {urlString} cached successfully.");
216240
if (_failedFetches.TryRemove(urlString, out _))
217241
{

0 commit comments

Comments
 (0)