diff --git a/Shoko.Server/API/v1/Implementations/ShokoServiceImplementation/ShokoServiceImplementation_Utilities.cs b/Shoko.Server/API/v1/Implementations/ShokoServiceImplementation/ShokoServiceImplementation_Utilities.cs index 03e6fbe22..6e2bc4eee 100644 --- a/Shoko.Server/API/v1/Implementations/ShokoServiceImplementation/ShokoServiceImplementation_Utilities.cs +++ b/Shoko.Server/API/v1/Implementations/ShokoServiceImplementation/ShokoServiceImplementation_Utilities.cs @@ -412,14 +412,16 @@ public CL_VideoLocal_Renamed RenameFilePreview(int videoLocalID) else { ret.VideoLocal = null; - if (string.IsNullOrEmpty(vid?.GetBestVideoLocalPlace(true)?.FullServerPath)) + if (string.IsNullOrEmpty(vid.GetBestVideoLocalPlace(true)?.FullServerPath)) { ret.VideoLocal = null; ret.Success = false; ret.NewFileName = "ERROR: The file could not be found."; return ret; } - ret.NewFileName = RenameFileHelper.GetRenamer(Shoko.Models.Constants.Renamer.TempFileName)?.GetFileName(vid.GetBestVideoLocalPlace()); + + ret.NewFileName = RenameFileHelper.GetFilename(vid.GetBestVideoLocalPlace(), + Shoko.Models.Constants.Renamer.TempFileName); if (string.IsNullOrEmpty(ret.NewFileName)) { diff --git a/Shoko.Server/Renamer/RenameFileHelper.cs b/Shoko.Server/Renamer/RenameFileHelper.cs index 8bbf3cb92..e871d05c8 100644 --- a/Shoko.Server/Renamer/RenameFileHelper.cs +++ b/Shoko.Server/Renamer/RenameFileHelper.cs @@ -23,48 +23,46 @@ public class RenameFileHelper private static IDictionary LegacyScriptImplementations = new Dictionary(); public static IDictionary LegacyScriptDescriptions { get; } = new Dictionary(); - public static string GetFilename(SVR_VideoLocal_Place place) + public static string GetFilename(SVR_VideoLocal_Place place, string fallbackScript = null) { string result = Path.GetFileName(place.FilePath); foreach (var renamer in GetPluginRenamersSorted()) { - var args = new RenameEventArgs - { - AnimeInfo = place.VideoLocal?.GetAnimeEpisodes().Select(a => a?.GetAnimeSeries()?.GetAnime()) - .Where(a => a != null).Cast().ToList(), - GroupInfo = place.VideoLocal?.GetAnimeEpisodes().Select(a => a.GetAnimeSeries()?.AnimeGroup) - .Where(a => a != null).DistinctBy(a => a.AnimeGroupID).Cast().ToList(), - EpisodeInfo = place.VideoLocal?.GetAnimeEpisodes().Where(a => a != null).Cast().ToList(), - FileInfo = place - }; + RenameEventArgs args = GetRenameEventArgs(place); renamer.GetFilename(args); if (args.Cancel) return null; if (string.IsNullOrEmpty(args.Result)) continue; return args.Result; } - string attempt = GetRenamerWithFallback()?.GetFileName(place); + string attempt = fallbackScript == null + ? GetRenamerWithFallback()?.GetFileName(place) + : GetRenamer(fallbackScript)?.GetFileName(place); if (attempt != null) result = attempt; return result; } - + + private static RenameEventArgs GetRenameEventArgs(SVR_VideoLocal_Place place) + { + var args = new RenameEventArgs + { + AnimeInfo = place.VideoLocal?.GetAnimeEpisodes().Select(a => a?.GetAnimeSeries()?.GetAnime()) + .Where(a => a != null).Cast().ToList(), + GroupInfo = place.VideoLocal?.GetAnimeEpisodes().Select(a => a.GetAnimeSeries()?.AnimeGroup) + .Where(a => a != null).DistinctBy(a => a.AnimeGroupID).Cast().ToList(), + EpisodeInfo = place.VideoLocal?.GetAnimeEpisodes().Where(a => a != null).Cast().ToList(), + FileInfo = place + }; + return args; + } + public static (ImportFolder, string) GetDestination(SVR_VideoLocal_Place place) { foreach (var renamer in GetPluginRenamersSorted()) { - var args = new MoveEventArgs - { - AnimeInfo = place.VideoLocal?.GetAnimeEpisodes().Select(a => a?.GetAnimeSeries()?.GetAnime()) - .Where(a => a != null).Cast().ToList(), - GroupInfo = place.VideoLocal?.GetAnimeEpisodes().Select(a => a.GetAnimeSeries()?.AnimeGroup) - .Where(a => a != null).DistinctBy(a => a.AnimeGroupID).Cast().ToList(), - EpisodeInfo = place.VideoLocal?.GetAnimeEpisodes().Where(a => a != null).Cast().ToList(), - FileInfo = place, - AvailableFolders = RepoFactory.ImportFolder.GetAll().Cast() - .Where(a => a.DropFolderType != DropFolderType.Excluded).ToList() - }; + MoveEventArgs args = GetMoveEventArgs(place); renamer.GetDestination(args); if (args.Cancel) return (null, null); if (string.IsNullOrEmpty(args.DestinationPath) || args.DestinationImportFolder == null) continue; @@ -79,7 +77,23 @@ public static (ImportFolder, string) GetDestination(SVR_VideoLocal_Place place) return (null, null); } - + + private static MoveEventArgs GetMoveEventArgs(SVR_VideoLocal_Place place) + { + var args = new MoveEventArgs + { + AnimeInfo = place.VideoLocal?.GetAnimeEpisodes().Select(a => a?.GetAnimeSeries()?.GetAnime()) + .Where(a => a != null).Cast().ToList(), + GroupInfo = place.VideoLocal?.GetAnimeEpisodes().Select(a => a.GetAnimeSeries()?.AnimeGroup) + .Where(a => a != null).DistinctBy(a => a.AnimeGroupID).Cast().ToList(), + EpisodeInfo = place.VideoLocal?.GetAnimeEpisodes().Where(a => a != null).Cast().ToList(), + FileInfo = place, + AvailableFolders = RepoFactory.ImportFolder.GetAll().Cast() + .Where(a => a.DropFolderType != DropFolderType.Excluded).ToList() + }; + return args; + } + public static IRenamer GetRenamer() { var script = RepoFactory.RenameScript.GetDefaultScript();