/// <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;
}