66 lines
1.7 KiB
C#
66 lines
1.7 KiB
C#
using System.IO;
|
|
using System.Linq;
|
|
using EmbyToolbox.Models;
|
|
|
|
namespace EmbyToolbox.Services;
|
|
|
|
public sealed class ExternalFileCleanupService
|
|
{
|
|
public IReadOnlyList<string> MoveUsedToUseless(string sourceVideoPath, IReadOnlyList<SidecarFile> usedFiles)
|
|
{
|
|
if (usedFiles.Count == 0)
|
|
{
|
|
return [];
|
|
}
|
|
|
|
// Шрифты считаются переиспользуемыми ресурсами каталога и не перемещаются.
|
|
var movable = usedFiles
|
|
.Where(f => !f.IsFont)
|
|
.DistinctBy(x => x.FullPath)
|
|
.ToList();
|
|
if (movable.Count == 0)
|
|
{
|
|
return [];
|
|
}
|
|
|
|
var dir = Path.GetDirectoryName(sourceVideoPath);
|
|
if (string.IsNullOrWhiteSpace(dir))
|
|
{
|
|
return [];
|
|
}
|
|
|
|
var useless = Path.Combine(dir, "useless");
|
|
Directory.CreateDirectory(useless);
|
|
var moved = new List<string>();
|
|
foreach (var f in movable)
|
|
{
|
|
if (!File.Exists(f.FullPath))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var target = BuildUniqueTarget(useless, Path.GetFileName(f.FullPath));
|
|
File.Move(f.FullPath, target, overwrite: false);
|
|
moved.Add(target);
|
|
}
|
|
|
|
return moved;
|
|
}
|
|
|
|
private static string BuildUniqueTarget(string dir, string fileName)
|
|
{
|
|
var name = Path.GetFileNameWithoutExtension(fileName);
|
|
var ext = Path.GetExtension(fileName);
|
|
var candidate = Path.Combine(dir, fileName);
|
|
var i = 1;
|
|
while (File.Exists(candidate))
|
|
{
|
|
candidate = Path.Combine(dir, $"{name}_{i}{ext}");
|
|
i++;
|
|
}
|
|
|
|
return candidate;
|
|
}
|
|
}
|
|
|