public (IImportFolder destination, string subfolder) GetDestination(MoveEventArgs args) { var visitor = new ScriptRenamerVisitor(args); if (CheckBadArgs(visitor)) { args.Cancel = true; return(null, null); } SetupAndLaunch(visitor); if (visitor.FindLastLocation) { IImportFolder fld = null; var lastFileLocation = ((IEnumerable <dynamic>)VideoLocalRepo.GetByAniDBAnimeID(visitor.AnimeInfo.AnimeID)) .Where(vl => !string.Equals(vl.CRC32, visitor.FileInfo.Hashes.CRC, StringComparison.OrdinalIgnoreCase)) .OrderByDescending(vl => vl.DateTimeUpdated) .Select(vl => vl.GetBestVideoLocalPlace()) .FirstOrDefault(vlp => (fld = (IImportFolder)ImportFolderRepo.GetByID(vlp.ImportFolderID)) is not null && (fld.DropFolderType.HasFlag(DropFolderType.Destination) || fld.DropFolderType.HasFlag(DropFolderType.Excluded))); string subFld = Path.GetDirectoryName(lastFileLocation?.FilePath); if (fld is not null && subFld is not null) { return(fld, subFld); } } var(destfolder, olddestfolder) = GetNewAndOldDestinations(args, visitor); var subfolder = GetNewSubfolder(args, visitor, olddestfolder); return(destfolder, subfolder); }
public (IImportFolder destination, string subfolder) GetDestination(MoveEventArgs args) { if (args?.EpisodeInfo == null) { throw new ArgumentException("File is unrecognized. Not Moving"); } // get the series var series = args.AnimeInfo?.FirstOrDefault(); if (series == null) { throw new ArgumentException("Series cannot be found for file"); } // replace the invalid characters string name = series.PreferredTitle.ReplaceInvalidPathCharacters(); if (string.IsNullOrEmpty(name)) { throw new ArgumentException("Series Name is null or empty"); } var group = args.GroupInfo?.FirstOrDefault(); if (group == null) { throw new ArgumentException("Group could not be found for file"); } string path; if (group.Series.Count == 1) { path = name; } else { var groupName = Utils.ReplaceInvalidFolderNameCharacters(group.Name); path = Path.Combine(groupName, name); } IImportFolder destFolder = series.Restricted switch { true => args.AvailableFolders.FirstOrDefault(a => a.Location.Contains("Hentai") && ValidDestinationFolder(a)), false => args.AvailableFolders.FirstOrDefault(a => !a.Location.Contains("Hentai") && ValidDestinationFolder(a)) }; return(destFolder, path); }
private static bool ValidDestinationFolder(IImportFolder dest) => dest.DropFolderType.HasFlag(DropFolderType.Destination);
private static string GetNewSubfolder(MoveEventArgs args, ScriptRenamerVisitor visitor, IImportFolder olddestfolder) { if (visitor.Destination is not null && visitor.Subfolder is null) { throw new ArgumentException("Destination set without Subfolder"); } var oldsubfolder = olddestfolder is null ? null : $"{NormPath(Path.GetDirectoryName(args.FileInfo.FilePath))}/" .Replace($"{NormPath(olddestfolder.Location)}/", null, StringComparison.OrdinalIgnoreCase).TrimEnd('/'); var oldsubfoldersplit = olddestfolder is null?Array.Empty <string>() : oldsubfolder.Split('/'); var newsubfoldersplit = visitor.Subfolder.Trim((char)0x1F).Split((char)0x1F) .Select(f => f == "*" ? f : RemoveInvalidFilenameChars(f.ReplaceInvalidPathCharacters())).ToArray(); var subfolder = string.Empty; for (var i = 0; i < newsubfoldersplit.Length; i++) { if (newsubfoldersplit[i] == "*") { if (i < oldsubfoldersplit.Length) { subfolder += oldsubfoldersplit[i] + '/'; } else { throw new ArgumentException("Could not find subfolder from wildcard"); } } else { subfolder += newsubfoldersplit[i] + '/'; } } subfolder = NormPath(subfolder); return(subfolder == string.Empty ? throw new ArgumentException("Subfolder cannot be set to empty") : subfolder); }