-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTempBatchManager.cs
More file actions
111 lines (94 loc) · 2.98 KB
/
Copy pathTempBatchManager.cs
File metadata and controls
111 lines (94 loc) · 2.98 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.IO;
namespace DropResize
{
public class TempBatchManager
{
private TempBatchManager(string batchFolder)
{
BatchFolder = batchFolder;
}
public string BatchFolder { get; private set; }
public void Delete()
{
if (string.IsNullOrEmpty(BatchFolder) || !Directory.Exists(BatchFolder))
{
return;
}
try
{
Directory.Delete(BatchFolder, true);
}
catch
{
// Temporary output cleanup should not prevent the app from closing.
}
}
public static void DeleteRoot()
{
var root = Path.Combine(Path.GetTempPath(), "Drop&Resize");
if (!Directory.Exists(root))
{
return;
}
try
{
Directory.Delete(root, true);
}
catch
{
// Leftover temporary files are non-fatal and may be locked by drag targets.
}
}
public static TempBatchManager Create()
{
var root = Path.Combine(Path.GetTempPath(), "Drop&Resize");
Directory.CreateDirectory(root);
var folderName =
"batch_" +
DateTime.Now.ToString("yyyyMMdd_HHmmss_fff") +
"_" +
Guid.NewGuid().ToString("N").Substring(0, 8);
var folder = Path.Combine(root, folderName);
Directory.CreateDirectory(folder);
return new TempBatchManager(folder);
}
public static string GetUniqueOutputPath(string outputFolder, string inputPath, ResizeProfile profile)
{
var baseName = MakeSafeFileName(Path.GetFileNameWithoutExtension(inputPath));
var extension = GetExtension(profile.OutputFormat);
var candidate = Path.Combine(outputFolder, baseName + profile.Suffix + extension);
var index = 2;
while (File.Exists(candidate))
{
candidate = Path.Combine(outputFolder, baseName + profile.Suffix + "_" + index + extension);
index++;
}
return candidate;
}
private static string MakeSafeFileName(string name)
{
foreach (var invalid in Path.GetInvalidFileNameChars())
{
name = name.Replace(invalid, '_');
}
if (string.IsNullOrWhiteSpace(name))
{
return "image";
}
return name;
}
private static string GetExtension(OutputFormat format)
{
switch (format)
{
case OutputFormat.Png:
return ".png";
case OutputFormat.Bmp:
return ".bmp";
default:
return ".jpg";
}
}
}
}