From df0a5f004d7c4ccec7933aa5c4709b7ab9636dc0 Mon Sep 17 00:00:00 2001 From: Qstick Date: Wed, 25 Jan 2023 22:12:33 -0600 Subject: [PATCH] Fixed: Improve moving file to location where another one exists Closes #1762 --- src/NzbDrone.Common/Disk/DiskProviderBase.cs | 5 +++++ .../Disk/FileAlreadyExistsException.cs | 15 +++++++++++++++ .../MediaFiles/RenameBookFileService.cs | 4 ++++ 3 files changed, 24 insertions(+) create mode 100644 src/NzbDrone.Common/Disk/FileAlreadyExistsException.cs diff --git a/src/NzbDrone.Common/Disk/DiskProviderBase.cs b/src/NzbDrone.Common/Disk/DiskProviderBase.cs index 30d4e0227..368dbe2b8 100644 --- a/src/NzbDrone.Common/Disk/DiskProviderBase.cs +++ b/src/NzbDrone.Common/Disk/DiskProviderBase.cs @@ -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); } diff --git a/src/NzbDrone.Common/Disk/FileAlreadyExistsException.cs b/src/NzbDrone.Common/Disk/FileAlreadyExistsException.cs new file mode 100644 index 000000000..69acb4cd7 --- /dev/null +++ b/src/NzbDrone.Common/Disk/FileAlreadyExistsException.cs @@ -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; + } + } +} diff --git a/src/NzbDrone.Core/MediaFiles/RenameBookFileService.cs b/src/NzbDrone.Core/MediaFiles/RenameBookFileService.cs index 1bbe61971..94df44e46 100644 --- a/src/NzbDrone.Core/MediaFiles/RenameBookFileService.cs +++ b/src/NzbDrone.Core/MediaFiles/RenameBookFileService.cs @@ -135,6 +135,10 @@ private void RenameFiles(List 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);