From 99ab65f7902a133865400fbf4d03390ce11c0834 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sat, 20 Jan 2024 19:22:07 -0800 Subject: [PATCH] New: Add recycle bin path for deleted movies to webhook/custom script (cherry picked from commit a71d40edba1388d67e4deefd8bfc354a7a83c6b1) Closes #9674 --- .../Checks/RemotePathMappingCheckFixture.cs | 2 +- .../HistoryTests/HistoryServiceFixture.cs | 2 +- .../NotificationTests/SynologyIndexerFixture.cs | 12 ++++++------ .../NotificationTests/Xbmc/OnDownloadFixture.cs | 11 +++++++---- src/NzbDrone.Core/MediaFiles/DeletedMovieFile.cs | 14 ++++++++++++++ .../MediaFiles/Events/MovieFileImportedEvent.cs | 4 ++-- .../MediaFiles/MovieFileMoveResult.cs | 6 +++--- .../MediaFiles/MovieImport/ImportApprovedMovie.cs | 2 +- src/NzbDrone.Core/MediaFiles/RecycleBinProvider.cs | 8 ++++++-- .../MediaFiles/ScriptImportDecider.cs | 6 +++--- .../MediaFiles/UpgradeMediaFileService.cs | 5 +++-- .../Notifications/CustomScript/CustomScript.cs | 7 ++++--- src/NzbDrone.Core/Notifications/DownloadMessage.cs | 2 +- .../Notifications/Synology/SynologyIndexer.cs | 2 +- .../Notifications/Webhook/WebhookBase.cs | 5 +++-- .../Notifications/Webhook/WebhookMovieFile.cs | 1 + src/NzbDrone.Core/Parser/Model/LocalMovie.cs | 2 +- 17 files changed, 58 insertions(+), 33 deletions(-) create mode 100644 src/NzbDrone.Core/MediaFiles/DeletedMovieFile.cs diff --git a/src/NzbDrone.Core.Test/HealthCheck/Checks/RemotePathMappingCheckFixture.cs b/src/NzbDrone.Core.Test/HealthCheck/Checks/RemotePathMappingCheckFixture.cs index 8da6ff5c9..6ffc52197 100644 --- a/src/NzbDrone.Core.Test/HealthCheck/Checks/RemotePathMappingCheckFixture.cs +++ b/src/NzbDrone.Core.Test/HealthCheck/Checks/RemotePathMappingCheckFixture.cs @@ -176,7 +176,7 @@ namespace NzbDrone.Core.Test.HealthCheck.Checks public void should_return_ok_on_movie_imported_event() { GivenFolderExists(_downloadRootPath); - var importEvent = new MovieFileImportedEvent(new LocalMovie(), new MovieFile(), new List(), true, new DownloadClientItem()); + var importEvent = new MovieFileImportedEvent(new LocalMovie(), new MovieFile(), new List(), true, new DownloadClientItem()); Subject.Check(importEvent).ShouldBeOk(); } diff --git a/src/NzbDrone.Core.Test/HistoryTests/HistoryServiceFixture.cs b/src/NzbDrone.Core.Test/HistoryTests/HistoryServiceFixture.cs index e1c312bd3..96adf1be7 100644 --- a/src/NzbDrone.Core.Test/HistoryTests/HistoryServiceFixture.cs +++ b/src/NzbDrone.Core.Test/HistoryTests/HistoryServiceFixture.cs @@ -91,7 +91,7 @@ namespace NzbDrone.Core.Test.HistoryTests DownloadId = "abcd" }; - Subject.Handle(new MovieFileImportedEvent(localMovie, movieFile, new List(), true, downloadClientItem)); + Subject.Handle(new MovieFileImportedEvent(localMovie, movieFile, new List(), true, downloadClientItem)); Mocker.GetMock() .Verify(v => v.Insert(It.Is(h => h.SourceTitle == Path.GetFileNameWithoutExtension(localMovie.Path)))); diff --git a/src/NzbDrone.Core.Test/NotificationTests/SynologyIndexerFixture.cs b/src/NzbDrone.Core.Test/NotificationTests/SynologyIndexerFixture.cs index f114e624e..32ec6219b 100644 --- a/src/NzbDrone.Core.Test/NotificationTests/SynologyIndexerFixture.cs +++ b/src/NzbDrone.Core.Test/NotificationTests/SynologyIndexerFixture.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Generic; using Moq; using NUnit.Framework; using NzbDrone.Core.MediaFiles; @@ -33,16 +33,16 @@ namespace NzbDrone.Core.Test.NotificationTests RelativePath = "moviefile1.mkv" }, - OldMovieFiles = new List + OldMovieFiles = new List { - new MovieFile + new DeletedMovieFile(new MovieFile { RelativePath = "oldmoviefile1.mkv" - }, - new MovieFile + }, null), + new DeletedMovieFile(new MovieFile { RelativePath = "oldmoviefile2.mkv" - } + }, null) } }; diff --git a/src/NzbDrone.Core.Test/NotificationTests/Xbmc/OnDownloadFixture.cs b/src/NzbDrone.Core.Test/NotificationTests/Xbmc/OnDownloadFixture.cs index 5337ad226..fd11fe3fa 100644 --- a/src/NzbDrone.Core.Test/NotificationTests/Xbmc/OnDownloadFixture.cs +++ b/src/NzbDrone.Core.Test/NotificationTests/Xbmc/OnDownloadFixture.cs @@ -28,7 +28,7 @@ namespace NzbDrone.Core.Test.NotificationTests.Xbmc _downloadMessage = Builder.CreateNew() .With(d => d.Movie = movie) .With(d => d.MovieFile = movieFile) - .With(d => d.OldMovieFiles = new List()) + .With(d => d.OldMovieFiles = new List()) .Build(); Subject.Definition = new NotificationDefinition(); @@ -40,9 +40,12 @@ namespace NzbDrone.Core.Test.NotificationTests.Xbmc private void GivenOldFiles() { - _downloadMessage.OldMovieFiles = Builder.CreateListOfSize(1) - .Build() - .ToList(); + _downloadMessage.OldMovieFiles = Builder + .CreateListOfSize(1) + .All() + .WithFactory(() => new DeletedMovieFile(Builder.CreateNew().Build(), null)) + .Build() + .ToList(); Subject.Definition.Settings = new XbmcSettings { diff --git a/src/NzbDrone.Core/MediaFiles/DeletedMovieFile.cs b/src/NzbDrone.Core/MediaFiles/DeletedMovieFile.cs new file mode 100644 index 000000000..1cbdb05aa --- /dev/null +++ b/src/NzbDrone.Core/MediaFiles/DeletedMovieFile.cs @@ -0,0 +1,14 @@ +namespace NzbDrone.Core.MediaFiles +{ + public class DeletedMovieFile + { + public string RecycleBinPath { get; set; } + public MovieFile MovieFile { get; set; } + + public DeletedMovieFile(MovieFile movieFile, string recycleBinPath) + { + MovieFile = movieFile; + RecycleBinPath = recycleBinPath; + } + } +} diff --git a/src/NzbDrone.Core/MediaFiles/Events/MovieFileImportedEvent.cs b/src/NzbDrone.Core/MediaFiles/Events/MovieFileImportedEvent.cs index cb10a9edb..ae31af4ac 100644 --- a/src/NzbDrone.Core/MediaFiles/Events/MovieFileImportedEvent.cs +++ b/src/NzbDrone.Core/MediaFiles/Events/MovieFileImportedEvent.cs @@ -9,12 +9,12 @@ namespace NzbDrone.Core.MediaFiles.Events { public LocalMovie MovieInfo { get; private set; } public MovieFile ImportedMovie { get; private set; } - public List OldFiles { get; private set; } + public List OldFiles { get; private set; } public bool NewDownload { get; private set; } public DownloadClientItemClientInfo DownloadClientInfo { get; set; } public string DownloadId { get; private set; } - public MovieFileImportedEvent(LocalMovie movieInfo, MovieFile importedMovie, List oldFiles, bool newDownload, DownloadClientItem downloadClientItem) + public MovieFileImportedEvent(LocalMovie movieInfo, MovieFile importedMovie, List oldFiles, bool newDownload, DownloadClientItem downloadClientItem) { MovieInfo = movieInfo; ImportedMovie = importedMovie; diff --git a/src/NzbDrone.Core/MediaFiles/MovieFileMoveResult.cs b/src/NzbDrone.Core/MediaFiles/MovieFileMoveResult.cs index a52faed61..bc2c3f070 100644 --- a/src/NzbDrone.Core/MediaFiles/MovieFileMoveResult.cs +++ b/src/NzbDrone.Core/MediaFiles/MovieFileMoveResult.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Generic; namespace NzbDrone.Core.MediaFiles { @@ -6,10 +6,10 @@ namespace NzbDrone.Core.MediaFiles { public MovieFileMoveResult() { - OldFiles = new List(); + OldFiles = new List(); } public MovieFile MovieFile { get; set; } - public List OldFiles { get; set; } + public List OldFiles { get; set; } } } diff --git a/src/NzbDrone.Core/MediaFiles/MovieImport/ImportApprovedMovie.cs b/src/NzbDrone.Core/MediaFiles/MovieImport/ImportApprovedMovie.cs index ecffc494f..6c151a563 100644 --- a/src/NzbDrone.Core/MediaFiles/MovieImport/ImportApprovedMovie.cs +++ b/src/NzbDrone.Core/MediaFiles/MovieImport/ImportApprovedMovie.cs @@ -73,7 +73,7 @@ namespace NzbDrone.Core.MediaFiles.MovieImport foreach (var importDecision in qualifiedImports.OrderByDescending(e => e.LocalMovie.Size)) { var localMovie = importDecision.LocalMovie; - var oldFiles = new List(); + var oldFiles = new List(); try { diff --git a/src/NzbDrone.Core/MediaFiles/RecycleBinProvider.cs b/src/NzbDrone.Core/MediaFiles/RecycleBinProvider.cs index 1bf3e5874..8f41405f5 100644 --- a/src/NzbDrone.Core/MediaFiles/RecycleBinProvider.cs +++ b/src/NzbDrone.Core/MediaFiles/RecycleBinProvider.cs @@ -14,7 +14,7 @@ namespace NzbDrone.Core.MediaFiles public interface IRecycleBinProvider { void DeleteFolder(string path); - void DeleteFile(string path, string subfolder = ""); + string DeleteFile(string path, string subfolder = ""); void Empty(); void Cleanup(); } @@ -66,7 +66,7 @@ namespace NzbDrone.Core.MediaFiles } } - public void DeleteFile(string path, string subfolder = "") + public string DeleteFile(string path, string subfolder = "") { _logger.Debug("Attempting to send '{0}' to recycling bin", path); var recyclingBin = _configService.RecycleBin; @@ -82,6 +82,8 @@ namespace NzbDrone.Core.MediaFiles _diskProvider.DeleteFile(path); _logger.Debug("File has been permanently deleted: {0}", path); + + return null; } else { @@ -128,6 +130,8 @@ namespace NzbDrone.Core.MediaFiles SetLastWriteTime(destination, DateTime.UtcNow); _logger.Debug("File has been moved to the recycling bin: {0}", destination); + + return destination; } } diff --git a/src/NzbDrone.Core/MediaFiles/ScriptImportDecider.cs b/src/NzbDrone.Core/MediaFiles/ScriptImportDecider.cs index 2a7370a3e..56888ffb0 100644 --- a/src/NzbDrone.Core/MediaFiles/ScriptImportDecider.cs +++ b/src/NzbDrone.Core/MediaFiles/ScriptImportDecider.cs @@ -170,9 +170,9 @@ namespace NzbDrone.Core.MediaFiles if (oldFiles.Any()) { - environmentVariables.Add("Radarr_DeletedRelativePaths", string.Join("|", oldFiles.Select(e => e.RelativePath))); - environmentVariables.Add("Radarr_DeletedPaths", string.Join("|", oldFiles.Select(e => Path.Combine(movie.Path, e.RelativePath)))); - environmentVariables.Add("Radarr_DeletedDateAdded", string.Join("|", oldFiles.Select(e => e.DateAdded))); + environmentVariables.Add("Radarr_DeletedRelativePaths", string.Join("|", oldFiles.Select(e => e.MovieFile.RelativePath))); + environmentVariables.Add("Radarr_DeletedPaths", string.Join("|", oldFiles.Select(e => Path.Combine(movie.Path, e.MovieFile.RelativePath)))); + environmentVariables.Add("Radarr_DeletedDateAdded", string.Join("|", oldFiles.Select(e => e.MovieFile.DateAdded))); } _logger.Debug("Executing external script: {0}", _configService.ScriptImportPath); diff --git a/src/NzbDrone.Core/MediaFiles/UpgradeMediaFileService.cs b/src/NzbDrone.Core/MediaFiles/UpgradeMediaFileService.cs index bcfa08604..7da797dda 100644 --- a/src/NzbDrone.Core/MediaFiles/UpgradeMediaFileService.cs +++ b/src/NzbDrone.Core/MediaFiles/UpgradeMediaFileService.cs @@ -55,14 +55,15 @@ namespace NzbDrone.Core.MediaFiles { var movieFilePath = Path.Combine(localMovie.Movie.Path, existingFile.RelativePath); var subfolder = rootFolder.GetRelativePath(_diskProvider.GetParentFolder(movieFilePath)); + string recycleBinPath = null; if (_diskProvider.FileExists(movieFilePath)) { _logger.Debug("Removing existing movie file: {0}", existingFile); - _recycleBinProvider.DeleteFile(movieFilePath, subfolder); + recycleBinPath = _recycleBinProvider.DeleteFile(movieFilePath, subfolder); } - moveFileResult.OldFiles.Add(existingFile); + moveFileResult.OldFiles.Add(new DeletedMovieFile(existingFile, recycleBinPath)); _mediaFileService.Delete(existingFile, DeleteMediaFileReason.Upgrade); } diff --git a/src/NzbDrone.Core/Notifications/CustomScript/CustomScript.cs b/src/NzbDrone.Core/Notifications/CustomScript/CustomScript.cs index 21e9e1249..dc0710f95 100755 --- a/src/NzbDrone.Core/Notifications/CustomScript/CustomScript.cs +++ b/src/NzbDrone.Core/Notifications/CustomScript/CustomScript.cs @@ -143,9 +143,10 @@ namespace NzbDrone.Core.Notifications.CustomScript if (message.OldMovieFiles.Any()) { - environmentVariables.Add("Radarr_DeletedRelativePaths", string.Join("|", message.OldMovieFiles.Select(e => e.RelativePath))); - environmentVariables.Add("Radarr_DeletedPaths", string.Join("|", message.OldMovieFiles.Select(e => Path.Combine(movie.Path, e.RelativePath)))); - environmentVariables.Add("Radarr_DeletedDateAdded", string.Join("|", message.OldMovieFiles.Select(e => e.DateAdded))); + environmentVariables.Add("Radarr_DeletedRelativePaths", string.Join("|", message.OldMovieFiles.Select(e => e.MovieFile.RelativePath))); + environmentVariables.Add("Radarr_DeletedPaths", string.Join("|", message.OldMovieFiles.Select(e => Path.Combine(movie.Path, e.MovieFile.RelativePath)))); + environmentVariables.Add("Radarr_DeletedDateAdded", string.Join("|", message.OldMovieFiles.Select(e => e.MovieFile.DateAdded))); + environmentVariables.Add("Radarr_DeletedRecycleBinPaths", string.Join("|", message.OldMovieFiles.Select(e => e.RecycleBinPath ?? string.Empty))); } ExecuteScript(environmentVariables); diff --git a/src/NzbDrone.Core/Notifications/DownloadMessage.cs b/src/NzbDrone.Core/Notifications/DownloadMessage.cs index 01f988616..90da28b8d 100644 --- a/src/NzbDrone.Core/Notifications/DownloadMessage.cs +++ b/src/NzbDrone.Core/Notifications/DownloadMessage.cs @@ -12,7 +12,7 @@ namespace NzbDrone.Core.Notifications public Movie Movie { get; set; } public LocalMovie MovieInfo { get; set; } public MovieFile MovieFile { get; set; } - public List OldMovieFiles { get; set; } + public List OldMovieFiles { get; set; } public string SourcePath { get; set; } public DownloadClientItemClientInfo DownloadClientInfo { get; set; } public string DownloadId { get; set; } diff --git a/src/NzbDrone.Core/Notifications/Synology/SynologyIndexer.cs b/src/NzbDrone.Core/Notifications/Synology/SynologyIndexer.cs index ea5a699a5..e3d469520 100644 --- a/src/NzbDrone.Core/Notifications/Synology/SynologyIndexer.cs +++ b/src/NzbDrone.Core/Notifications/Synology/SynologyIndexer.cs @@ -29,7 +29,7 @@ namespace NzbDrone.Core.Notifications.Synology { foreach (var oldFile in message.OldMovieFiles) { - var fullPath = Path.Combine(message.Movie.Path, oldFile.RelativePath); + var fullPath = Path.Combine(message.Movie.Path, oldFile.MovieFile.RelativePath); _indexerProxy.DeleteFile(fullPath); } diff --git a/src/NzbDrone.Core/Notifications/Webhook/WebhookBase.cs b/src/NzbDrone.Core/Notifications/Webhook/WebhookBase.cs index 4f23b177f..322f3410c 100644 --- a/src/NzbDrone.Core/Notifications/Webhook/WebhookBase.cs +++ b/src/NzbDrone.Core/Notifications/Webhook/WebhookBase.cs @@ -66,9 +66,10 @@ namespace NzbDrone.Core.Notifications.Webhook if (message.OldMovieFiles.Any()) { payload.DeletedFiles = message.OldMovieFiles.ConvertAll(x => - new WebhookMovieFile(x) + new WebhookMovieFile(x.MovieFile) { - Path = Path.Combine(message.Movie.Path, x.RelativePath) + Path = Path.Combine(message.Movie.Path, x.MovieFile.RelativePath), + RecycleBinPath = x.RecycleBinPath }); } diff --git a/src/NzbDrone.Core/Notifications/Webhook/WebhookMovieFile.cs b/src/NzbDrone.Core/Notifications/Webhook/WebhookMovieFile.cs index 3293a4084..baf00a365 100755 --- a/src/NzbDrone.Core/Notifications/Webhook/WebhookMovieFile.cs +++ b/src/NzbDrone.Core/Notifications/Webhook/WebhookMovieFile.cs @@ -39,5 +39,6 @@ namespace NzbDrone.Core.Notifications.Webhook public long Size { get; set; } public DateTime DateAdded { get; set; } public WebhookMovieFileMediaInfo MediaInfo { get; set; } + public string RecycleBinPath { get; set; } } } diff --git a/src/NzbDrone.Core/Parser/Model/LocalMovie.cs b/src/NzbDrone.Core/Parser/Model/LocalMovie.cs index 8d1d79146..392833a6a 100644 --- a/src/NzbDrone.Core/Parser/Model/LocalMovie.cs +++ b/src/NzbDrone.Core/Parser/Model/LocalMovie.cs @@ -23,7 +23,7 @@ namespace NzbDrone.Core.Parser.Model public DownloadClientItem DownloadItem { get; set; } public ParsedMovieInfo FolderMovieInfo { get; set; } public Movie Movie { get; set; } - public List OldFiles { get; set; } + public List OldFiles { get; set; } public QualityModel Quality { get; set; } public List Languages { get; set; } public MediaInfoModel MediaInfo { get; set; }