Make sure temporary files deleted

This commit is contained in:
Jack251970 2025-04-09 17:27:34 +08:00
parent 62a5dd784d
commit 150ea84191

View file

@ -41,6 +41,8 @@ public abstract class FirefoxBookmarkLoaderBase : IBookmarkLoader
if (string.IsNullOrEmpty(placesPath) || !File.Exists(placesPath)) if (string.IsNullOrEmpty(placesPath) || !File.Exists(placesPath))
return bookmarks; return bookmarks;
var tempDbPath = Path.Combine(_faviconCacheDir, $"tempplaces_{Guid.NewGuid()}.sqlite");
try try
{ {
// Try to register file monitoring // Try to register file monitoring
@ -54,7 +56,6 @@ public abstract class FirefoxBookmarkLoaderBase : IBookmarkLoader
} }
// Use a copy to avoid lock issues with the original file // Use a copy to avoid lock issues with the original file
var tempDbPath = Path.Combine(_faviconCacheDir, $"tempplaces_{Guid.NewGuid()}.sqlite");
File.Copy(placesPath, tempDbPath, true); File.Copy(placesPath, tempDbPath, true);
// Connect to database and execute query // Connect to database and execute query
@ -83,31 +84,32 @@ public abstract class FirefoxBookmarkLoaderBase : IBookmarkLoader
// https://github.com/dotnet/efcore/issues/26580 // https://github.com/dotnet/efcore/issues/26580
SqliteConnection.ClearPool(dbConnection); SqliteConnection.ClearPool(dbConnection);
dbConnection.Close(); dbConnection.Close();
// Delete temporary file
try
{
File.Delete(tempDbPath);
}
catch (Exception ex)
{
Main._context.API.LogException(ClassName, $"Failed to delete temporary favicon DB: {tempDbPath}", ex);
}
} }
catch (Exception ex) catch (Exception ex)
{ {
Main._context.API.LogException(ClassName, $"Failed to load Firefox bookmarks: {placesPath}", ex); Main._context.API.LogException(ClassName, $"Failed to load Firefox bookmarks: {placesPath}", ex);
} }
// Delete temporary file
try
{
File.Delete(tempDbPath);
}
catch (Exception ex)
{
Main._context.API.LogException(ClassName, $"Failed to delete temporary favicon DB: {tempDbPath}", ex);
}
return bookmarks; return bookmarks;
} }
private void LoadFaviconsFromDb(string faviconDbPath, List<Bookmark> bookmarks) private void LoadFaviconsFromDb(string faviconDbPath, List<Bookmark> bookmarks)
{ {
var tempDbPath = Path.Combine(_faviconCacheDir, $"tempfavicons_{Guid.NewGuid()}.sqlite");
try try
{ {
// Use a copy to avoid lock issues with the original file // Use a copy to avoid lock issues with the original file
var tempDbPath = Path.Combine(_faviconCacheDir, $"tempfavicons_{Guid.NewGuid()}.sqlite");
File.Copy(faviconDbPath, tempDbPath, true); File.Copy(faviconDbPath, tempDbPath, true);
var defaultIconPath = Path.Combine( var defaultIconPath = Path.Combine(
@ -177,21 +179,21 @@ public abstract class FirefoxBookmarkLoaderBase : IBookmarkLoader
// https://github.com/dotnet/efcore/issues/26580 // https://github.com/dotnet/efcore/issues/26580
SqliteConnection.ClearPool(connection); SqliteConnection.ClearPool(connection);
connection.Close(); connection.Close();
// Delete temporary file
try
{
File.Delete(tempDbPath);
}
catch (Exception ex)
{
Main._context.API.LogException(ClassName, $"Failed to delete temporary favicon DB: {tempDbPath}", ex);
}
} }
catch (Exception ex) catch (Exception ex)
{ {
Main._context.API.LogException(ClassName, $"Failed to load Firefox favicon DB: {faviconDbPath}", ex); Main._context.API.LogException(ClassName, $"Failed to load Firefox favicon DB: {faviconDbPath}", ex);
} }
// Delete temporary file
try
{
File.Delete(tempDbPath);
}
catch (Exception ex)
{
Main._context.API.LogException(ClassName, $"Failed to delete temporary favicon DB: {tempDbPath}", ex);
}
} }
private static void SaveBitmapData(byte[] imageData, string outputPath) private static void SaveBitmapData(byte[] imageData, string outputPath)