Enhance CleanupManualLeftoversAsync to remove subtitles from nested folders and empty directories
ci / build-backend (push) Successful in 2m50s
ci / build-frontend (push) Successful in 1m7s
ci / tests (push) Successful in 3m1s
ci / sonar (push) Successful in 6m15s

Updated the CleanupManualLeftoversAsync method to improve its functionality by allowing it to delete subtitle files located in nested directories and remove empty directories after the last episode is processed. Introduced a CompanionMatcher class to accurately identify companion files based on naming conventions. Added integration tests to verify the new cleanup behavior, ensuring that only relevant files are removed while preserving necessary content.
This commit is contained in:
Leonid Pershin
2026-07-27 08:57:11 +03:00
parent 0fd4e762f4
commit f22aee4685
3 changed files with 149 additions and 28 deletions
@@ -127,6 +127,66 @@ public sealed class ManualInboxStorageTests : IDisposable
Assert.True(Directory.Exists(_paths.ManualDir));
}
[Fact]
public async Task Cleanup_RemovesSubtitlesFromNestedFolders()
{
// Раскладка из жизни: субтитры лежат не рядом с серией, а в подпапках по языкам, и названы
// иначе — совпадает только номер серии.
Write("Season 16/S16E21.mkv");
Write("Season 16/subs_ru/S16E21 - The Father, the Son.srt");
Write("Season 16/subs_en/S16E21 - The Father, the Son.srt");
Write("Season 16/subs_ru/S16E20 - Home Away From Homer.srt");
Write("Season 16/extras/S16E21 удалённые сцены.mkv");
await _storage.PromoteToOriginalAsync(
MediaSource.ManualInbox,
"Season 16/S16E21.mkv",
Guid.NewGuid(),
".mkv",
default
);
await _storage.CleanupManualLeftoversAsync("Season 16/S16E21.mkv", default);
var season = Path.Combine(_paths.ManualDir, "Season 16");
Assert.False(
File.Exists(Path.Combine(season, "subs_ru", "S16E21 - The Father, the Son.srt"))
);
Assert.False(
File.Exists(Path.Combine(season, "subs_en", "S16E21 - The Father, the Son.srt"))
);
// Субтитры соседней серии остаются: её ещё не забирали.
Assert.True(
File.Exists(Path.Combine(season, "subs_ru", "S16E20 - Home Away From Homer.srt"))
);
// Видео не трогаем нигде, даже с тем же номером серии, — это другой материал.
Assert.True(File.Exists(Path.Combine(season, "extras", "S16E21 удалённые сцены.mkv")));
}
[Fact]
public async Task Cleanup_RemovesEmptiedNestedFolders_AfterLastEpisode()
{
Write("Season 16/S16E01.mkv");
Write("Season 16/S16E02.mkv");
Write("Season 16/subs_ru/S16E01 - Treehouse.srt");
Write("Season 16/subs_ru/S16E02 - All's Fair.srt");
foreach (var episode in new[] { "S16E01", "S16E02" })
{
await _storage.PromoteToOriginalAsync(
MediaSource.ManualInbox,
$"Season 16/{episode}.mkv",
Guid.NewGuid(),
".mkv",
default
);
await _storage.CleanupManualLeftoversAsync($"Season 16/{episode}.mkv", default);
}
// Опустевшие подпапки уходят вместе с каталогом сезона — корень manual/ остаётся.
Assert.False(Directory.Exists(Path.Combine(_paths.ManualDir, "Season 16")));
Assert.True(Directory.Exists(_paths.ManualDir));
}
[Fact]
public async Task Cleanup_KeepsFolderThatStillHasFiles()
{