-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioPreviewTempFileCleanup.cs
More file actions
74 lines (65 loc) · 1.95 KB
/
AudioPreviewTempFileCleanup.cs
File metadata and controls
74 lines (65 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System.IO;
namespace CFRezManager;
internal static class AudioPreviewTempFileCleanup
{
private static readonly string PreviewDirectory =
Path.GetFullPath(Path.Combine(Path.GetTempPath(), "CFRezManager", "AudioPreview"));
public static void Queue(IEnumerable<string> paths)
{
string[] safePaths = paths
.Where(IsSafePreviewPath)
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToArray();
if (safePaths.Length == 0)
{
return;
}
_ = Task.Run(() => DeleteWithRetriesAsync(safePaths));
}
private static async Task DeleteWithRetriesAsync(IReadOnlyList<string> paths)
{
var remaining = new HashSet<string>(paths, StringComparer.OrdinalIgnoreCase);
for (int attempt = 0; attempt < 12 && remaining.Count > 0; attempt++)
{
if (attempt > 0)
{
await Task.Delay(Math.Min(250 * attempt, 2_000));
}
foreach (string path in remaining.ToArray())
{
try
{
if (!File.Exists(path))
{
remaining.Remove(path);
continue;
}
File.Delete(path);
remaining.Remove(path);
}
catch (IOException)
{
}
catch (UnauthorizedAccessException)
{
}
}
}
}
private static bool IsSafePreviewPath(string path)
{
if (string.IsNullOrWhiteSpace(path))
{
return false;
}
try
{
string fullPath = Path.GetFullPath(path);
return fullPath.StartsWith(PreviewDirectory + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase);
}
catch
{
return false;
}
}
}