Skip to content

armgit / Filee

Public Imported from GitHub
0

Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.

Delete the work folders a crash left in the temp folder

flexeykinDeve8892feca765
diff --git a/src/Filee.App/App.axaml.cs b/src/Filee.App/App.axaml.cs
index b6724eba..618e0d55 100644
--- a/src/Filee.App/App.axaml.cs
+++ b/src/Filee.App/App.axaml.cs
@@ -135,6 +135,8 @@ public partial class App : Application
AppHost.Get<WatchFolderService>().Start();
// Originals of files from a browser that a crash kept from being deleted ("Save only the converted file").
_ = Task.Run(() => Filee.Core.Web.WebDrop.DeleteStale(Filee.Core.Web.WebDrop.DefaultWorkRoot, TimeSpan.FromDays(1)));
+ // Likewise the work folders of conversions that never got to clean up after themselves.
+ _ = Task.Run(() => JobQueue.DeleteStaleWorkFolders(JobQueue.DefaultWorkRoot, TimeSpan.FromDays(1)));
// Once LibreOffice is installed: prepare its profiles while the user isn't converting yet.
AppHost.Get<EngineDownloadService>().WarmUpInBackground(TimeSpan.FromSeconds(10));
diff --git a/src/Filee.Core/Conversion/JobQueue.cs b/src/Filee.Core/Conversion/JobQueue.cs
index 228e0703..cfa0b825 100644
--- a/src/Filee.Core/Conversion/JobQueue.cs
+++ b/src/Filee.Core/Conversion/JobQueue.cs
@@ -79,7 +79,7 @@ public sealed class JobQueue : IAsyncDisposable
job.State = JobState.Running;
Notify(job);
- var workDir = Path.Combine(Path.GetTempPath(), "Filee", job.Id);
+ var workDir = Path.Combine(DefaultWorkRoot, job.Id);
Directory.CreateDirectory(workDir);
try
{
@@ -699,6 +699,46 @@ public sealed class JobQueue : IAsyncDisposable
}
}
+ /// <summary>Where every job has its work folder, named by the job's <see cref="ConversionJob.Id"/>.</summary>
+ public static string DefaultWorkRoot => Path.Combine(Path.GetTempPath(), "Filee");
+
+ /// <summary>
+ /// Deletes the work folders of jobs a crash, a forced quit or a power cut kept from removing them: intermediate
+ /// PDFs, TIFF pages and video frames can take gigabytes, and the OS doesn't empty its temp folder by itself. Only a
+ /// job's folder nothing was written in for <paramref name="age"/> goes, so a job still running in another Filee (the
+ /// command line) keeps its own; files from a browser have a sweep of their own (<c>WebDrop.DeleteStale</c>).
+ /// </summary>
+ public static void DeleteStaleWorkFolders(string workRoot, TimeSpan age)
+ {
+ try
+ {
+ if (!Directory.Exists(workRoot))
+ return;
+ foreach (var folder in Directory.EnumerateDirectories(workRoot))
+ {
+ if (IsJobId(Path.GetFileName(folder)) && DateTime.UtcNow - LastWrite(folder) > age)
+ TryDelete(folder);
+ }
+ }
+ catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
+ {
+ }
+
+ static bool IsJobId(string name) => name.Length == 10 && name.All(char.IsAsciiHexDigitLower);
+
+ // A folder's own time changes only when an entry right inside it does: a long job writes deeper down.
+ static DateTime LastWrite(string folder)
+ {
+ var last = Directory.GetLastWriteTimeUtc(folder);
+ foreach (var entry in new DirectoryInfo(folder).EnumerateFileSystemInfos("*", SearchOption.AllDirectories))
+ {
+ if (entry.LastWriteTimeUtc > last)
+ last = entry.LastWriteTimeUtc;
+ }
+ return last;
+ }
+ }
+
private static void TryDelete(string dir)
{
try
diff --git a/tests/Filee.Core.Tests/JobQueueTests.cs b/tests/Filee.Core.Tests/JobQueueTests.cs
index 546258df..3114e7ac 100644
--- a/tests/Filee.Core.Tests/JobQueueTests.cs
+++ b/tests/Filee.Core.Tests/JobQueueTests.cs
@@ -537,6 +537,39 @@ public class JobQueueTests
Assert.Equal(2, job.Outputs.Distinct().Count());
}
+ [Fact]
+ public void Work_folders_a_crash_left_behind_are_deleted_and_running_ones_kept()
+ {
+ using var root = new TempDir();
+ var old = DateTime.UtcNow.AddDays(-3);
+ string Folder(string name, DateTime written, string? nested = null)
+ {
+ var folder = Path.Combine(root.Path, name);
+ Directory.CreateDirectory(Path.Combine(folder, "a1b2c3d4"));
+ var file = Path.Combine(folder, "a1b2c3d4", "page.tiff");
+ File.WriteAllText(file, "x");
+ File.SetLastWriteTimeUtc(file, nested is null ? written : DateTime.UtcNow);
+ Directory.SetLastWriteTimeUtc(Path.Combine(folder, "a1b2c3d4"), written);
+ Directory.SetLastWriteTimeUtc(folder, written);
+ return folder;
+ }
+
+ var crashed = Folder("0123456789", old);
+ var running = Folder("abcdef0123", DateTime.UtcNow);
+ // Made days ago, but a long encode is still writing into it.
+ var encoding = Folder("fedcba9876", old, nested: "still writing");
+ var web = Folder("web", old);
+ var other = Folder("not-a-job", old);
+
+ JobQueue.DeleteStaleWorkFolders(root.Path, TimeSpan.FromDays(1));
+
+ Assert.False(Directory.Exists(crashed));
+ Assert.True(Directory.Exists(running));
+ Assert.True(Directory.Exists(encoding));
+ Assert.True(Directory.Exists(web)); // WebDrop.DeleteStale's
+ Assert.True(Directory.Exists(other));
+ }
+
[Fact]
public async Task Enqueue_runs_in_background_and_raises_finished()
{