Пример #1
0
        /// <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.
            var containsOverwriteKeyword = file.ContainsKeyword(settings.OverwriteKeywords);

            // Rename the file that is already in the destination if it exists under a different name.
            if (settings.RenameIfExists || containsOverwriteKeyword)
            {
                // Get the files that are already in the destination directory.
                var results = 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 (var result in results)
                        {
                            result.InputFile.Delete();
                            foreach (var episode in result.Episodes)
                            {
                                episode.FileCount--;
                                episode.Save(storageProvider);
                            }
                        }
                    }
                    else if (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.
                        var currentName = results[0].InputFile.Name;
                        var newName     = destinationInfo.Name;

                        if (results.Count == 1 && !currentName.Equals(newName))
                        {
                            var 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 = fileResultManager.GetFullPath(file, destination);
            }

            return(true);
        }