@@ -122,45 +122,43 @@ private static void EnumerateFolderBookmark(JsonElement folderElement, ICollecti
122122
123123 private void LoadFaviconsFromDb ( string dbPath , List < Bookmark > bookmarks )
124124 {
125+ // Use a copy to avoid lock issues with the original file
126+ var tempDbPath = Path . Combine ( _faviconCacheDir , $ "tempfavicons_{ Guid . NewGuid ( ) } .db") ;
127+
125128 try
126129 {
127- // Use a copy to avoid lock issues with the original file
128- var tempDbPath = Path . Combine ( _faviconCacheDir , $ "tempfavicons_{ Guid . NewGuid ( ) } .db") ;
130+ File . Copy ( dbPath , tempDbPath , true ) ;
131+ }
132+ catch ( Exception ex )
133+ {
134+ Main . _context . API . LogException ( ClassName , $ "Failed to copy favicon DB: { dbPath } ", ex ) ;
135+ return ;
136+ }
137+ finally
138+ {
139+ File . Delete ( tempDbPath ) ;
140+ }
129141
130- try
131- {
132- File . Copy ( dbPath , tempDbPath , true ) ;
133- }
134- catch ( Exception ex )
135- {
136- Main . _context . API . LogException ( ClassName , $ "Failed to copy favicon DB: { dbPath } ", ex ) ;
137- return ;
138- }
139- finally
140- {
141- File . Delete ( tempDbPath ) ;
142- }
142+ try
143+ {
144+ using var connection = new SqliteConnection ( $ "Data Source={ tempDbPath } ") ;
145+ connection . Open ( ) ;
143146
144- try
147+ foreach ( var bookmark in bookmarks )
145148 {
146- using var connection = new SqliteConnection ( $ "Data Source={ tempDbPath } ") ;
147- connection . Open ( ) ;
148-
149- foreach ( var bookmark in bookmarks )
149+ try
150150 {
151- try
152- {
153- var url = bookmark . Url ;
154- if ( string . IsNullOrEmpty ( url ) ) continue ;
151+ var url = bookmark . Url ;
152+ if ( string . IsNullOrEmpty ( url ) ) continue ;
155153
156- // Extract domain from URL
157- if ( ! Uri . TryCreate ( url , UriKind . Absolute , out Uri uri ) )
158- continue ;
154+ // Extract domain from URL
155+ if ( ! Uri . TryCreate ( url , UriKind . Absolute , out Uri uri ) )
156+ continue ;
159157
160- var domain = uri . Host ;
158+ var domain = uri . Host ;
161159
162- using var cmd = connection . CreateCommand ( ) ;
163- cmd . CommandText = @"
160+ using var cmd = connection . CreateCommand ( ) ;
161+ cmd . CommandText = @"
164162 SELECT f.id, b.image_data
165163 FROM favicons f
166164 JOIN favicon_bitmaps b ON f.id = b.icon_id
@@ -169,51 +167,46 @@ WHERE m.page_url LIKE @url
169167 ORDER BY b.width DESC
170168 LIMIT 1" ;
171169
172- cmd . Parameters . AddWithValue ( "@url" , $ "%{ domain } %") ;
170+ cmd . Parameters . AddWithValue ( "@url" , $ "%{ domain } %") ;
173171
174- using var reader = cmd . ExecuteReader ( ) ;
175- if ( ! reader . Read ( ) || reader . IsDBNull ( 1 ) )
176- continue ;
172+ using var reader = cmd . ExecuteReader ( ) ;
173+ if ( ! reader . Read ( ) || reader . IsDBNull ( 1 ) )
174+ continue ;
177175
178- var iconId = reader . GetInt64 ( 0 ) . ToString ( ) ;
179- var imageData = ( byte [ ] ) reader [ "image_data" ] ;
176+ var iconId = reader . GetInt64 ( 0 ) . ToString ( ) ;
177+ var imageData = ( byte [ ] ) reader [ "image_data" ] ;
180178
181- if ( imageData is not { Length : > 0 } )
182- continue ;
179+ if ( imageData is not { Length : > 0 } )
180+ continue ;
183181
184- var faviconPath = Path . Combine ( _faviconCacheDir , $ "chromium_{ domain } _{ iconId } .png") ;
185- SaveBitmapData ( imageData , faviconPath ) ;
182+ var faviconPath = Path . Combine ( _faviconCacheDir , $ "chromium_{ domain } _{ iconId } .png") ;
183+ SaveBitmapData ( imageData , faviconPath ) ;
186184
187- bookmark . FaviconPath = faviconPath ;
188- }
189- catch ( Exception ex )
190- {
191- Main . _context . API . LogException ( ClassName , $ "Failed to extract bookmark favicon: { bookmark . Url } ", ex ) ;
192- }
185+ bookmark . FaviconPath = faviconPath ;
186+ }
187+ catch ( Exception ex )
188+ {
189+ Main . _context . API . LogException ( ClassName , $ "Failed to extract bookmark favicon: { bookmark . Url } ", ex ) ;
193190 }
194-
195- // https://github.com/dotnet/efcore/issues/26580
196- SqliteConnection . ClearPool ( connection ) ;
197- connection . Close ( ) ;
198- }
199- catch ( Exception ex )
200- {
201- Main . _context . API . LogException ( ClassName , $ "Failed to connect to SQLite: { tempDbPath } ", ex ) ;
202191 }
203192
204- // Delete temporary file
205- try
206- {
207- File . Delete ( tempDbPath ) ;
208- }
209- catch ( Exception ex )
210- {
211- Main . _context . API . LogException ( ClassName , $ "Failed to delete temporary favicon DB: { tempDbPath } ", ex ) ;
212- }
193+ // https://github.com/dotnet/efcore/issues/26580
194+ SqliteConnection . ClearPool ( connection ) ;
195+ connection . Close ( ) ;
196+ }
197+ catch ( Exception ex )
198+ {
199+ Main . _context . API . LogException ( ClassName , $ "Failed to connect to SQLite: { tempDbPath } ", ex ) ;
200+ }
201+
202+ // Delete temporary file
203+ try
204+ {
205+ File . Delete ( tempDbPath ) ;
213206 }
214207 catch ( Exception ex )
215208 {
216- Main . _context . API . LogException ( ClassName , $ "Failed to load favicon DB: { dbPath } ", ex ) ;
209+ Main . _context . API . LogException ( ClassName , $ "Failed to delete temporary favicon DB: { tempDbPath } ", ex ) ;
217210 }
218211 }
219212
0 commit comments