-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConversionService.cs
More file actions
538 lines (467 loc) · 20.5 KB
/
ConversionService.cs
File metadata and controls
538 lines (467 loc) · 20.5 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
namespace FileOrganizer;
internal sealed record ConversionCapabilities(
bool ImagePipelineAvailable,
bool FfmpegAvailable,
bool LibreOfficeAvailable,
bool ImageMagickAvailable,
string AudioVideoPipeline,
string DocumentPipeline,
string ImagePipeline,
string ShellMode);
internal sealed record ConversionBatchResult(
IReadOnlyList<FileActionRecord> Operations,
IReadOnlyList<ConversionRecord> Conversions);
internal static class ConversionService
{
private static readonly HashSet<string> ImageExtensions = new(StringComparer.OrdinalIgnoreCase)
{
".jpg", ".jpeg", ".png", ".bmp", ".gif", ".tif", ".tiff"
};
private static readonly HashSet<string> AudioExtensions = new(StringComparer.OrdinalIgnoreCase)
{
".mp3", ".wav", ".flac", ".aac", ".ogg", ".m4a", ".wma", ".aiff", ".alac"
};
private static readonly HashSet<string> VideoExtensions = new(StringComparer.OrdinalIgnoreCase)
{
".mp4", ".mov", ".avi", ".mkv", ".wmv", ".webm", ".m4v", ".mpeg", ".mpg"
};
private static readonly HashSet<string> DocumentExtensions = new(StringComparer.OrdinalIgnoreCase)
{
".pdf", ".doc", ".docx", ".txt", ".rtf", ".odt", ".md", ".tex", ".ppt", ".pptx", ".odp", ".xls", ".xlsx", ".ods", ".csv", ".tsv"
};
public static ConversionCapabilities DetectCapabilities()
{
var ffmpegPath = TryFindExecutable("ffmpeg.exe", new[]
{
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "ffmpeg", "bin", "ffmpeg.exe"),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "ffmpeg", "bin", "ffmpeg.exe")
});
var sofficePath = TryFindExecutable("soffice.exe", new[]
{
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "LibreOffice", "program", "soffice.exe"),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "LibreOffice", "program", "soffice.exe")
});
var magickPath = TryFindExecutable("magick.exe", new[]
{
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "ImageMagick-7.1.1-Q16-HDRI", "magick.exe"),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "ImageMagick-7.1.1-Q16", "magick.exe"),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "ImageMagick-7.1.1-Q16", "magick.exe")
});
return new ConversionCapabilities(
ImagePipelineAvailable: true,
FfmpegAvailable: !string.IsNullOrWhiteSpace(ffmpegPath),
LibreOfficeAvailable: !string.IsNullOrWhiteSpace(sofficePath),
ImageMagickAvailable: !string.IsNullOrWhiteSpace(magickPath),
AudioVideoPipeline: string.IsNullOrWhiteSpace(ffmpegPath) ? "ffmpeg not found" : "ffmpeg ready",
DocumentPipeline: string.IsNullOrWhiteSpace(sofficePath) ? "LibreOffice not found" : "LibreOffice ready",
ImagePipeline: !string.IsNullOrWhiteSpace(magickPath) ? "built-in + ImageMagick" : "built-in image pipeline",
ShellMode: "Browser shell"
);
}
public static ConversionBatchResult Process(
IReadOnlyList<FileActionRecord> operations,
OrganizerOptions options,
IProgress<OrganizerProgressInfo>? progress,
CancellationToken cancellationToken)
{
if (!options.IncludeConvert || string.Equals(options.ConvertTargetFormat, "Keep", StringComparison.OrdinalIgnoreCase))
{
return new ConversionBatchResult(operations, Array.Empty<ConversionRecord>());
}
var updatedOperations = operations.ToList();
var conversions = new List<ConversionRecord>();
var candidates = operations
.Select((item, index) => new { Item = item, Index = index })
.Where(entry => options.WhatIf
? entry.Item.ShouldExecute && entry.Item.Status != FileOperationStatus.Skipped && entry.Item.Status != FileOperationStatus.Failed
: entry.Item.Status == FileOperationStatus.Executed)
.ToList();
for (var index = 0; index < candidates.Count; index++)
{
cancellationToken.ThrowIfCancellationRequested();
var current = candidates[index];
progress?.Report(new OrganizerProgressInfo("Convert", index + 1, candidates.Count, $"Converting {Path.GetFileName(current.Item.DestinationPath)}"));
var conversion = options.WhatIf
? BuildPreviewRecord(current.Item, options)
: ExecuteOne(current.Item, options, cancellationToken);
conversions.Add(conversion);
if (conversion.Status == ConversionStatus.Executed)
{
var mergedGeneratedPaths = current.Item.GeneratedPaths.Concat(new[] { conversion.OutputPath }).Distinct(StringComparer.OrdinalIgnoreCase).ToArray();
updatedOperations[current.Index] = current.Item with
{
GeneratedPaths = mergedGeneratedPaths,
StatusMessage = AppendStatusMessage(current.Item.StatusMessage, $"Converted to {Path.GetFileName(conversion.OutputPath)} via {conversion.Converter}.")
};
}
}
return new ConversionBatchResult(updatedOperations, conversions);
}
private static ConversionRecord BuildPreviewRecord(FileActionRecord item, OrganizerOptions options)
{
if (!MatchesSourceFamily(item.Extension, options.ConvertSourceFormat))
{
return CreateSkippedRecord(item.DestinationPath, item.DestinationPath, options.ConvertTargetFormat, "preview", "Skipped because the file does not match the selected source format.");
}
var supported = ResolveSupportedConverter(item.DestinationPath, options.ConvertTargetFormat);
if (supported is null)
{
return CreateSkippedRecord(item.DestinationPath, item.DestinationPath, options.ConvertTargetFormat, "preview", "Skipped because the requested conversion pair is not supported.");
}
var plannedOutput = BuildOutputPath(item.DestinationPath, options.ConvertTargetFormat, ensureUnique: false);
if (string.Equals(item.DestinationPath, plannedOutput, StringComparison.OrdinalIgnoreCase))
{
return CreateSkippedRecord(item.DestinationPath, plannedOutput, options.ConvertTargetFormat, supported.Value.DisplayName, "Skipped because the file is already in the target format.");
}
return new ConversionRecord
{
SourcePath = item.DestinationPath,
OutputPath = plannedOutput,
TargetFormat = options.ConvertTargetFormat,
Converter = supported.Value.DisplayName,
Status = ConversionStatus.Planned,
Message = "Preview only. Conversion would run after the file operation stage."
};
}
private static ConversionRecord ExecuteOne(FileActionRecord item, OrganizerOptions options, CancellationToken cancellationToken)
{
if (!MatchesSourceFamily(item.Extension, options.ConvertSourceFormat))
{
return CreateSkippedRecord(item.DestinationPath, item.DestinationPath, options.ConvertTargetFormat, "filter", "Skipped because the file does not match the selected source format.");
}
var supported = ResolveSupportedConverter(item.DestinationPath, options.ConvertTargetFormat);
if (supported is null)
{
return CreateSkippedRecord(item.DestinationPath, item.DestinationPath, options.ConvertTargetFormat, "none", "Skipped because the requested conversion pair is not supported.");
}
var outputPath = BuildOutputPath(item.DestinationPath, options.ConvertTargetFormat, ensureUnique: true);
if (string.Equals(item.DestinationPath, outputPath, StringComparison.OrdinalIgnoreCase))
{
return CreateSkippedRecord(item.DestinationPath, outputPath, options.ConvertTargetFormat, supported.Value.DisplayName, "Skipped because the file is already in the target format.");
}
try
{
switch (supported.Value.Kind)
{
case "image":
ConvertImage(item.DestinationPath, outputPath, options.ConvertTargetFormat, options.ConvertQuality);
break;
case "ffmpeg":
ConvertWithFfmpeg(item.DestinationPath, outputPath, options.ConvertTargetFormat, options.ConvertQuality, cancellationToken);
break;
case "libreoffice":
ConvertWithLibreOffice(item.DestinationPath, outputPath, cancellationToken);
break;
case "magick":
ConvertWithImageMagick(item.DestinationPath, outputPath, cancellationToken);
break;
default:
return CreateSkippedRecord(item.DestinationPath, outputPath, options.ConvertTargetFormat, supported.Value.DisplayName, "Skipped because no converter is configured for this target.");
}
return new ConversionRecord
{
SourcePath = item.DestinationPath,
OutputPath = outputPath,
TargetFormat = options.ConvertTargetFormat,
Converter = supported.Value.DisplayName,
Status = ConversionStatus.Executed,
Message = "Converted successfully."
};
}
catch (Exception ex)
{
TryDeleteFile(outputPath);
return new ConversionRecord
{
SourcePath = item.DestinationPath,
OutputPath = outputPath,
TargetFormat = options.ConvertTargetFormat,
Converter = supported.Value.DisplayName,
Status = ConversionStatus.Failed,
Message = ex.Message
};
}
}
private static (string Kind, string DisplayName)? ResolveSupportedConverter(string sourcePath, string targetFormat)
{
var extension = Path.GetExtension(sourcePath);
var normalizedTarget = NormalizeTarget(targetFormat);
var capabilities = DetectCapabilities();
if (normalizedTarget is ".jpg" or ".png" && ImageExtensions.Contains(extension))
{
return ("image", "Built-in image pipeline");
}
if (normalizedTarget is ".mp3" or ".wav" or ".mp4" && (AudioExtensions.Contains(extension) || VideoExtensions.Contains(extension)))
{
return capabilities.FfmpegAvailable ? ("ffmpeg", "ffmpeg") : null;
}
if (normalizedTarget == ".pdf")
{
if (DocumentExtensions.Contains(extension))
{
return capabilities.LibreOfficeAvailable ? ("libreoffice", "LibreOffice") : null;
}
if (ImageExtensions.Contains(extension))
{
return capabilities.ImageMagickAvailable ? ("magick", "ImageMagick") : null;
}
}
return null;
}
private static bool MatchesSourceFamily(string extension, string sourceFormat)
{
return sourceFormat.ToLowerInvariant() switch
{
"any" => true,
"document" => DocumentExtensions.Contains(extension),
"image" => ImageExtensions.Contains(extension),
"audio" => AudioExtensions.Contains(extension),
"video" => VideoExtensions.Contains(extension),
_ => true
};
}
private static string BuildOutputPath(string sourcePath, string targetFormat, bool ensureUnique)
{
var normalizedTarget = NormalizeTarget(targetFormat);
var directory = Path.GetDirectoryName(sourcePath) ?? string.Empty;
var baseName = Path.GetFileNameWithoutExtension(sourcePath);
var desired = Path.Combine(directory, baseName + normalizedTarget);
if (!ensureUnique)
{
return desired;
}
if (!File.Exists(desired))
{
return desired;
}
var counter = 1;
while (true)
{
var candidate = Path.Combine(directory, $"{baseName} ({counter}){normalizedTarget}");
if (!File.Exists(candidate))
{
return candidate;
}
counter++;
}
}
private static string NormalizeTarget(string targetFormat)
{
return targetFormat.ToLowerInvariant() switch
{
"pdf" => ".pdf",
"jpg" => ".jpg",
"png" => ".png",
"mp3" => ".mp3",
"wav" => ".wav",
"mp4" => ".mp4",
_ => Path.GetExtension(targetFormat)
};
}
private static void ConvertImage(string sourcePath, string outputPath, string targetFormat, string qualityMode)
{
using var sourceImage = Image.FromFile(sourcePath);
var target = NormalizeTarget(targetFormat);
if (target == ".png")
{
sourceImage.Save(outputPath, ImageFormat.Png);
return;
}
using var bitmap = new Bitmap(sourceImage.Width, sourceImage.Height);
using (var graphics = Graphics.FromImage(bitmap))
{
graphics.Clear(Color.Black);
graphics.DrawImage(sourceImage, 0, 0, sourceImage.Width, sourceImage.Height);
}
var encoder = ImageCodecInfo.GetImageEncoders().First(codec => codec.FormatID == ImageFormat.Jpeg.Guid);
using var parameters = new EncoderParameters(1);
parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, GetJpegQuality(qualityMode));
bitmap.Save(outputPath, encoder, parameters);
}
private static byte GetJpegQuality(string qualityMode)
{
return qualityMode.ToLowerInvariant() switch
{
"keep" => 95,
"compact" => 72,
_ => 84
};
}
private static void ConvertWithFfmpeg(string sourcePath, string outputPath, string targetFormat, string qualityMode, CancellationToken cancellationToken)
{
var ffmpegPath = TryFindExecutable("ffmpeg.exe", Array.Empty<string>())
?? throw new InvalidOperationException("ffmpeg was not found on this machine.");
var target = NormalizeTarget(targetFormat);
var arguments = target switch
{
".mp3" => $"-y -i {Quote(sourcePath)} -vn -q:a {GetMp3Quality(qualityMode)} {Quote(outputPath)}",
".wav" => $"-y -i {Quote(sourcePath)} -vn {Quote(outputPath)}",
".mp4" => $"-y -i {Quote(sourcePath)} -movflags +faststart -pix_fmt yuv420p -c:v libx264 -preset medium -crf {GetMp4Crf(qualityMode)} -c:a aac -b:a 160k {Quote(outputPath)}",
_ => throw new InvalidOperationException($"Unsupported ffmpeg target: {targetFormat}")
};
RunProcess(ffmpegPath, arguments, cancellationToken);
}
private static string GetMp3Quality(string qualityMode)
{
return qualityMode.ToLowerInvariant() switch
{
"keep" => "0",
"compact" => "5",
_ => "2"
};
}
private static string GetMp4Crf(string qualityMode)
{
return qualityMode.ToLowerInvariant() switch
{
"keep" => "18",
"compact" => "28",
_ => "23"
};
}
private static void ConvertWithLibreOffice(string sourcePath, string outputPath, CancellationToken cancellationToken)
{
var sofficePath = TryFindExecutable("soffice.exe", Array.Empty<string>())
?? throw new InvalidOperationException("LibreOffice was not found on this machine.");
var tempDirectory = Path.Combine(Path.GetTempPath(), "argus-convert-" + Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture));
Directory.CreateDirectory(tempDirectory);
try
{
RunProcess(sofficePath, $"--headless --convert-to pdf --outdir {Quote(tempDirectory)} {Quote(sourcePath)}", cancellationToken);
var expected = Path.Combine(tempDirectory, Path.GetFileNameWithoutExtension(sourcePath) + ".pdf");
if (!File.Exists(expected))
{
throw new InvalidOperationException("LibreOffice did not create the expected PDF output.");
}
File.Copy(expected, outputPath, overwrite: false);
}
finally
{
TryDeleteDirectory(tempDirectory);
}
}
private static void ConvertWithImageMagick(string sourcePath, string outputPath, CancellationToken cancellationToken)
{
var magickPath = TryFindExecutable("magick.exe", Array.Empty<string>())
?? throw new InvalidOperationException("ImageMagick was not found on this machine.");
RunProcess(magickPath, $"{Quote(sourcePath)} {Quote(outputPath)}", cancellationToken);
}
private static void RunProcess(string fileName, string arguments, CancellationToken cancellationToken)
{
using var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = fileName,
Arguments = arguments,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
StandardOutputEncoding = Encoding.UTF8,
StandardErrorEncoding = Encoding.UTF8
}
};
process.Start();
var standardOutput = process.StandardOutput.ReadToEnd();
var standardError = process.StandardError.ReadToEnd();
process.WaitForExit();
cancellationToken.ThrowIfCancellationRequested();
if (process.ExitCode != 0)
{
var message = string.IsNullOrWhiteSpace(standardError) ? standardOutput : standardError;
throw new InvalidOperationException(string.IsNullOrWhiteSpace(message)
? $"Conversion process failed with exit code {process.ExitCode}."
: message.Trim());
}
}
private static ConversionRecord CreateSkippedRecord(string sourcePath, string outputPath, string targetFormat, string converter, string message)
{
return new ConversionRecord
{
SourcePath = sourcePath,
OutputPath = outputPath,
TargetFormat = targetFormat,
Converter = converter,
Status = ConversionStatus.Skipped,
Message = message
};
}
private static string? TryFindExecutable(string executableName, IEnumerable<string> explicitCandidates)
{
foreach (var candidate in explicitCandidates.Where(path => !string.IsNullOrWhiteSpace(path)))
{
if (File.Exists(candidate))
{
return candidate;
}
}
var pathValue = Environment.GetEnvironmentVariable("PATH") ?? string.Empty;
foreach (var directory in pathValue.Split(Path.PathSeparator, StringSplitOptions.RemoveEmptyEntries))
{
try
{
var candidate = Path.Combine(directory.Trim(), executableName);
if (File.Exists(candidate))
{
return candidate;
}
}
catch
{
// Ignore malformed PATH entries.
}
}
return null;
}
private static void TryDeleteFile(string path)
{
try
{
if (File.Exists(path))
{
File.Delete(path);
}
}
catch
{
// Best-effort cleanup only.
}
}
private static void TryDeleteDirectory(string path)
{
try
{
if (Directory.Exists(path))
{
Directory.Delete(path, recursive: true);
}
}
catch
{
// Best-effort cleanup only.
}
}
private static string AppendStatusMessage(string? existing, string addition)
{
if (string.IsNullOrWhiteSpace(existing))
{
return addition;
}
return existing + " " + addition;
}
private static string Quote(string value)
{
return "\"" + value.Replace("\"", "\\\"", StringComparison.Ordinal) + "\"";
}
}