Fixed: Improve moving file to location where another one exists

Closes #1762
This commit is contained in:
Qstick 2023-01-25 22:12:33 -06:00
parent d8f11bc3cb
commit df0a5f004d
3 changed files with 24 additions and 0 deletions

View file

@ -278,6 +278,11 @@ public void MoveFolder(string source, string destination)
protected virtual void MoveFileInternal(string source, string destination)
{
if (File.Exists(destination))
{
throw new FileAlreadyExistsException("File already exists", destination);
}
_fileSystem.File.Move(source, destination);
}

View file

@ -0,0 +1,15 @@
using System;
namespace NzbDrone.Common.Disk
{
public class FileAlreadyExistsException : Exception
{
public string Filename { get; set; }
public FileAlreadyExistsException(string message, string filename)
: base(message)
{
Filename = filename;
}
}
}

View file

@ -135,6 +135,10 @@ private void RenameFiles(List<BookFile> bookFiles, Author author)
_eventAggregator.PublishEvent(new BookFileRenamedEvent(author, bookFile, bookFilePath));
}
catch (FileAlreadyExistsException ex)
{
_logger.Warn("File not renamed, there is already a file at the destination: {0}", ex.Filename);
}
catch (SameFilenameException ex)
{
_logger.Debug("File not renamed, source and destination are the same: {0}", ex.Filename);