فهرست منبع

Better cleanup of temporary files

Ruben 4 ماه پیش
والد
کامیت
4990d46361
2فایلهای تغییر یافته به همراه73 افزوده شده و 8 حذف شده
  1. 30 0
      src/PicView.Avalonia/Navigation/ImageLoader.cs
  2. 43 8
      src/PicView.Core/FileHandling/TempFileHelper.cs

+ 30 - 0
src/PicView.Avalonia/Navigation/ImageLoader.cs

@@ -5,6 +5,7 @@ using PicView.Avalonia.UI;
 using PicView.Avalonia.ViewModels;
 using PicView.Core.ArchiveHandling;
 using PicView.Core.DebugTools;
+using PicView.Core.FileHandling;
 using PicView.Core.FileHistory;
 using PicView.Core.Gallery;
 using PicView.Core.Http;
@@ -234,11 +235,19 @@ public static class ImageLoader
         vm.IsLoading = true;
         TitleManager.SetLoadingTitle(vm);
 
+        string? prevArchiveLocation = null;
+        var previousArchiveExist = !string.IsNullOrEmpty(ArchiveExtraction.TempZipDirectory);
+        if (previousArchiveExist)
+        {
+            prevArchiveLocation = ArchiveExtraction.TempZipDirectory;
+        }
+
         var extraction = await ArchiveExtraction
             .ExtractArchiveAsync(path, vm.PlatformService.ExtractWithLocalSoftwareAsync).ConfigureAwait(false);
         if (!extraction)
         {
             await ErrorHandling.ReloadAsync(vm);
+            Clean();
             return;
         }
 
@@ -258,11 +267,31 @@ public static class ImageLoader
 
             FileHistoryManager.Add(path);
             MainKeyboardShortcuts.ClearKeyDownModifiers(); // Fix possible modifier key state issue
+            if (previousArchiveExist)
+            {
+                try
+                {
+                    Directory.Delete(prevArchiveLocation, true);
+                }
+                catch (Exception e)
+                {
+                    DebugHelper.LogDebug(nameof(ImageLoader), nameof(LoadPicFromArchiveAsync), e);
+                }
+            }
         }
         else
         {
             await imageIterator.DisposeAsync();
             await ErrorHandling.ReloadAsync(vm);
+            Clean();
+        }
+        
+        return;
+
+        void Clean()
+        {
+            TempFileHelper.DeleteTempFiles();
+            ArchiveExtraction.Cleanup();
         }
     }
 
@@ -344,6 +373,7 @@ public static class ImageLoader
         FileHistoryManager.Add(url);
 
         await NavigationManager.DisposeImageIteratorAsync();
+        TempFileHelper.TempFilePath = destination;
     }
 
     #endregion

+ 43 - 8
src/PicView.Core/FileHandling/TempFileHelper.cs

@@ -1,41 +1,76 @@
-using System.Diagnostics;
+
 using PicView.Core.DebugTools;
+#if DEBUG
+using System.Diagnostics;
+#endif
 
 namespace PicView.Core.FileHandling;
 
 public static class TempFileHelper
 {
     /// <summary>
-    /// File path for the extracted folder
+    /// Gets or sets the path to a temporary file or directory that is used by the application
+    /// for various operations. This property is commonly set to a value in the system's
+    /// temporary folder and may hold paths for temporary files or directories created during runtime.
     /// </summary>
     public static string? TempFilePath { get; set; }
 
+    /// <summary>
+    /// Creates a temporary directory in the system's temporary path and assigns its path to the TempFilePath property.
+    /// The method generates a unique directory name and ensures the directory is created.
+    /// Returns true if the directory is successfully created, otherwise false.
+    /// </summary>
+    /// <returns>A boolean value indicating whether the temporary directory was successfully created.</returns>
     public static bool CreateTempDirectory()
     {
-        TempFilePath = Path.GetTempPath() + Path.GetRandomFileName();
+        TempFilePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
         Directory.CreateDirectory(TempFilePath);
 
         return Directory.Exists(TempFilePath);
     }
-    
+
     /// <summary>
-    /// Deletes the temporary files when an archived file has been opened
+    /// Deletes temporary files and directories stored in the path specified by the TempFilePath property.
+    /// If the path is a file, the file and its parent directory (if any) are deleted.
+    /// If the path is a directory, all files within the directory are deleted, and the directory itself is removed.
+    /// In case of errors during the deletion process, exceptions are logged for debugging purposes.
     /// </summary>
     public static void DeleteTempFiles()
     {
+        if (string.IsNullOrEmpty(TempFilePath))
+            return;
+
+        var isFile = false;
+
         if (!Directory.Exists(TempFilePath))
         {
-            return;
+            if (!File.Exists(TempFilePath))
+            {
+                return;
+            }
+
+            isFile = true;
         }
 
         try
         {
-            Array.ForEach(Directory.GetFiles(TempFilePath), File.Delete);
+            if (isFile)
+            {
+                File.Delete(TempFilePath);
+                Directory.Delete(Path.GetDirectoryName(TempFilePath));
+            }
+            else
+            {
+                Array.ForEach(Directory.GetFiles(TempFilePath), File.Delete);
+            }
 #if DEBUG
             Trace.WriteLine("Temp zip files deleted");
 #endif
 
-            Directory.Delete(TempFilePath);
+            if (!isFile)
+            {
+                Directory.Delete(TempFilePath);
+            }
 #if DEBUG
             Trace.WriteLine("Temp zip folder " + TempFilePath + " deleted");
 #endif