From 8ab040f612ee04dac4813a08cdeaddd446a64dc9 Mon Sep 17 00:00:00 2001 From: Kevin Richter <1887585+beschoenen@users.noreply.github.com> Date: Sun, 7 Aug 2022 20:52:54 +0200 Subject: [PATCH] Fixed: Improve moving file to location where another one exists --- src/NzbDrone.Common/Disk/DiskProviderBase.cs | 5 +++++ .../Disk/FileAlreadyExistsException.cs | 14 ++++++++++++++ .../MediaFiles/RenameEpisodeFileService.cs | 4 ++++ 3 files changed, 23 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 1f91a9654..e2dd26586 100644 --- a/src/NzbDrone.Common/Disk/DiskProviderBase.cs +++ b/src/NzbDrone.Common/Disk/DiskProviderBase.cs @@ -265,6 +265,11 @@ namespace NzbDrone.Common.Disk protected virtual void MoveFileInternal(string source, string destination) { + if (File.Exists(destination)) + { + throw new FileAlreadyExistsException("File already exists", destination); + } + 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..6a987444d --- /dev/null +++ b/src/NzbDrone.Common/Disk/FileAlreadyExistsException.cs @@ -0,0 +1,14 @@ +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/RenameEpisodeFileService.cs b/src/NzbDrone.Core/MediaFiles/RenameEpisodeFileService.cs index bd16f2d87..a4f9d97a9 100644 --- a/src/NzbDrone.Core/MediaFiles/RenameEpisodeFileService.cs +++ b/src/NzbDrone.Core/MediaFiles/RenameEpisodeFileService.cs @@ -134,6 +134,10 @@ namespace NzbDrone.Core.MediaFiles _eventAggregator.PublishEvent(new EpisodeFileRenamedEvent(series, episodeFile, previousPath)); } + 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);