using System.IO; using System.Linq; using EmbyToolbox.Models; namespace EmbyToolbox.Services; public sealed class ExternalFileCleanupService { public IReadOnlyList MoveUsedToUseless(string sourceVideoPath, IReadOnlyList 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(); 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; } }