private GetFullPath ( IDirectoryInfo destination, IStorageProvider provider ) : IFileInfo | ||
destination | IDirectoryInfo | /// The destination directory. /// |
provider | IStorageProvider | /// The storage provider. /// |
return | IFileInfo |
/// <summary> /// Processes a single file. /// </summary> /// <param name="type"> /// The sort type to use. /// </param> /// <param name="file"> /// The file to process. /// </param> /// <param name="destination"> /// The destination to process the files to. /// </param> private void ProcessFile(SortType type, FileResult file, IDirectoryInfo destination) { IFileInfo destinationInfo = file.GetFullPath(destination, this.storageProvider); if (destinationInfo.Directory != null && !destinationInfo.Directory.Exists) { destinationInfo.Directory.Create(); } else { if (!this.HandleRenameAndOverwrite(file, destination, ref destinationInfo)) { return; } } if (destinationInfo.Exists) { Logger.OnLogMessage(this, "Skipping {0}. Already exists.", LogType.Info, destinationInfo.Name.Truncate()); return; } switch (type) { case SortType.Move: file.InputFile.MoveTo(destinationInfo.FullName); Logger.OnLogMessage(this, "Moved {0}", LogType.Info, file.InputFile.Name.Truncate()); if (this.settings.DeleteEmptySubdirectories) { this.DeleteEmptySubdirectories(file); } break; case SortType.Copy: file.InputFile.CopyTo(destinationInfo.FullName); Logger.OnLogMessage(this, "Copied {0}", LogType.Info, file.InputFile.Name.Truncate()); break; } foreach (var episode in file.Episodes) { episode.FileCount++; episode.Save(this.storageProvider); } }
/// <summary> /// Handles the rename and overwrite of the file. /// </summary> /// <param name="file"> /// The file being processed. /// </param> /// <param name="destination"> /// The destination directory. /// </param> /// <param name="destinationInfo"> /// The destination file. /// </param> /// <returns> /// A value indicating whether the ProcessFile operation should continue or not. /// </returns> private bool HandleRenameAndOverwrite( FileResult file, IDirectoryInfo destination, ref IFileInfo destinationInfo) { // If the directory didn't exist then check it for the episode. bool containsOverwriteKeyword = file.ContainsKeyword(this.settings.OverwriteKeywords); // Rename the file that is already in the destination if it exists under a different name. if (this.settings.RenameIfExists || containsOverwriteKeyword) { // Get the files that are already in the destination directory. List<FileResult> results = this.scanManager.SearchDestinationFolder(destinationInfo.Directory).Where( x => x.Episodes != null && !x.Episodes.Where((t, i) => !file.Episodes[i].Equals(t)).Any()).ToList(); // If the episode already exists. if (results.Count > 0) { if (containsOverwriteKeyword) { foreach (FileResult result in results) { result.InputFile.Delete(); foreach (Episode episode in result.Episodes) { episode.FileCount--; episode.Save(this.storageProvider); } } } else if (this.settings.RenameIfExists && results[0].InputFile.Extension.Equals(destinationInfo.Extension)) { // Can't rename more than 1 file to the same thing. // Also don't rename if the file name is already the same. string currentName = results[0].InputFile.Name; string newName = destinationInfo.Name; if (results.Count == 1 && !currentName.Equals(newName)) { string originalName = results[0].InputFile.Name; results[0].InputFile.MoveTo(destinationInfo.FullName); Logger.OnLogMessage( this, "Renamed {0} to {1}", LogType.Info, originalName.Truncate(30), destinationInfo.Name.Truncate(30)); return false; } } } // Refresh the destination info as it may have changed. destinationInfo = file.GetFullPath(destination, this.storageProvider); } return true; }