From 030deaf6ef749ed7b6711f52166ce4b6867d2aed Mon Sep 17 00:00:00 2001 From: Qstick Date: Sun, 27 May 2018 00:55:43 -0400 Subject: [PATCH] New: Expand OnAlbumDownload, Add Synology handling (#372) * New: Expand OnAlbumDownload, Add Synology handling Fixes #324 * fixup: small naming correction * fixup: Tests for Synology --- .../SynologyIndexerFixture.cs | 16 +++++---- .../MediaFiles/Events/AlbumImportedEvent.cs | 7 ++-- .../TrackImport/ImportApprovedTracks.cs | 36 +++++++++++-------- .../Notifications/AlbumDownloadMessage.cs | 1 + .../CustomScript/CustomScript.cs | 12 +++++++ .../Notifications/NotificationService.cs | 7 ++-- .../Notifications/Synology/SynologyIndexer.cs | 6 ++-- 7 files changed, 58 insertions(+), 27 deletions(-) diff --git a/src/NzbDrone.Core.Test/NotificationTests/SynologyIndexerFixture.cs b/src/NzbDrone.Core.Test/NotificationTests/SynologyIndexerFixture.cs index 2564b1edd..9c834b36b 100644 --- a/src/NzbDrone.Core.Test/NotificationTests/SynologyIndexerFixture.cs +++ b/src/NzbDrone.Core.Test/NotificationTests/SynologyIndexerFixture.cs @@ -14,7 +14,7 @@ namespace NzbDrone.Core.Test.NotificationTests public class SynologyIndexerFixture : CoreTest { private Artist _artist; - private TrackDownloadMessage _upgrade; + private AlbumDownloadMessage _upgrade; [SetUp] public void SetUp() @@ -24,13 +24,17 @@ namespace NzbDrone.Core.Test.NotificationTests Path = @"C:\Test\".AsOsAgnostic() }; - _upgrade = new TrackDownloadMessage() + _upgrade = new AlbumDownloadMessage() { Artist = _artist, - TrackFile = new TrackFile + TrackFiles = new List { - RelativePath = "file1.S01E01E02.mkv" + new TrackFile + { + RelativePath = "file1.S01E01E02.mkv" + } + }, OldFiles = new List @@ -69,7 +73,7 @@ namespace NzbDrone.Core.Test.NotificationTests [Test] public void should_remove_old_episodes_on_upgrade() { - Subject.OnDownload(_upgrade); + Subject.OnAlbumDownload(_upgrade); Mocker.GetMock() .Verify(v => v.DeleteFile(@"C:\Test\file1.S01E01.mkv".AsOsAgnostic()), Times.Once()); @@ -81,7 +85,7 @@ namespace NzbDrone.Core.Test.NotificationTests [Test] public void should_add_new_episode_on_upgrade() { - Subject.OnDownload(_upgrade); + Subject.OnAlbumDownload(_upgrade); Mocker.GetMock() .Verify(v => v.AddFile(@"C:\Test\file1.S01E01E02.mkv".AsOsAgnostic()), Times.Once()); diff --git a/src/NzbDrone.Core/MediaFiles/Events/AlbumImportedEvent.cs b/src/NzbDrone.Core/MediaFiles/Events/AlbumImportedEvent.cs index 1d63a7608..c9fddca50 100644 --- a/src/NzbDrone.Core/MediaFiles/Events/AlbumImportedEvent.cs +++ b/src/NzbDrone.Core/MediaFiles/Events/AlbumImportedEvent.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using NzbDrone.Common.Messaging; using NzbDrone.Core.Download; using NzbDrone.Core.Music; -using NzbDrone.Core.Parser.Model; namespace NzbDrone.Core.MediaFiles.Events { @@ -10,16 +9,18 @@ namespace NzbDrone.Core.MediaFiles.Events { public Artist Artist { get; private set; } public Album Album { get; private set; } - public List ImportedTracks { get; private set; } + public List ImportedTracks { get; private set; } + public List OldFiles { get; private set; } public bool NewDownload { get; private set; } public string DownloadClient { get; private set; } public string DownloadId { get; private set; } - public AlbumImportedEvent(Artist artist, Album album, List importedTracks, bool newDownload, DownloadClientItem downloadClientItem) + public AlbumImportedEvent(Artist artist, Album album, List importedTracks, List oldFiles, bool newDownload, DownloadClientItem downloadClientItem) { Artist = artist; Album = album; ImportedTracks = importedTracks; + OldFiles = oldFiles; NewDownload = newDownload; if (downloadClientItem != null) diff --git a/src/NzbDrone.Core/MediaFiles/TrackImport/ImportApprovedTracks.cs b/src/NzbDrone.Core/MediaFiles/TrackImport/ImportApprovedTracks.cs index bf6501fec..a6612bb4d 100644 --- a/src/NzbDrone.Core/MediaFiles/TrackImport/ImportApprovedTracks.cs +++ b/src/NzbDrone.Core/MediaFiles/TrackImport/ImportApprovedTracks.cs @@ -1,17 +1,15 @@ -using NLog; -using NzbDrone.Common.Disk; -using NzbDrone.Common.Exceptions; -using NzbDrone.Common.Extensions; -using NzbDrone.Core.Download; -using NzbDrone.Core.MediaFiles.Events; -using NzbDrone.Core.Messaging.Events; -using NzbDrone.Core.Qualities; using System; using System.Collections.Generic; using System.Linq; -using System.Text; +using NLog; +using NzbDrone.Common.Disk; +using NzbDrone.Common.Extensions; +using NzbDrone.Core.Download; using NzbDrone.Core.Extras; using NzbDrone.Core.Languages; +using NzbDrone.Core.MediaFiles.Events; +using NzbDrone.Core.Messaging.Events; +using NzbDrone.Core.Qualities; namespace NzbDrone.Core.MediaFiles.TrackImport { @@ -29,17 +27,17 @@ namespace NzbDrone.Core.MediaFiles.TrackImport private readonly IEventAggregator _eventAggregator; private readonly Logger _logger; - public ImportApprovedTracks(IUpgradeMediaFiles episodeFileUpgrader, + public ImportApprovedTracks(IUpgradeMediaFiles trackFileUpgrader, IMediaFileService mediaFileService, IExtraService extraService, IDiskProvider diskProvider, IEventAggregator eventAggregator, Logger logger) { - _trackFileUpgrader = episodeFileUpgrader; + _trackFileUpgrader = trackFileUpgrader; _mediaFileService = mediaFileService; _extraService = extraService; - _diskProvider = diskProvider; + _diskProvider = diskProvider; _eventAggregator = eventAggregator; _logger = logger; } @@ -55,6 +53,8 @@ namespace NzbDrone.Core.MediaFiles.TrackImport .ToList(); var importResults = new List(); + var allImportedTrackFiles = new List(); + var allOldTrackFiles = new List(); foreach (var importDecision in qualifiedImports.OrderBy(e => e.LocalTrack.Tracks.Select(track => track.AbsoluteTrackNumber).MinOrDefault()) .ThenByDescending(e => e.LocalTrack.Size)) @@ -130,6 +130,9 @@ namespace NzbDrone.Core.MediaFiles.TrackImport _extraService.ImportTrack(localTrack, trackFile, copyOnly); } + allImportedTrackFiles.Add(trackFile); + allOldTrackFiles.AddRange(oldFiles); + _eventAggregator.PublishEvent(new TrackImportedEvent(localTrack, trackFile, oldFiles, newDownload, downloadClientItem)); } @@ -157,7 +160,7 @@ namespace NzbDrone.Core.MediaFiles.TrackImport } } - var albumImports = importResults.Where(e =>e.ImportDecision.LocalTrack.Album != null) + var albumImports = importResults.Where(e => e.ImportDecision.LocalTrack.Album != null) .GroupBy(e => e.ImportDecision.LocalTrack.Album.Id).ToList(); foreach (var albumImport in albumImports) @@ -167,7 +170,12 @@ namespace NzbDrone.Core.MediaFiles.TrackImport if (albumImport.Where(e => e.Errors.Count == 0).ToList().Count > 0 && artist != null && album != null) { - _eventAggregator.PublishEvent(new AlbumImportedEvent(artist, album, albumImport.Select(e => e.ImportDecision.LocalTrack).ToList(), newDownload, downloadClientItem)); + _eventAggregator.PublishEvent(new AlbumImportedEvent( + artist, + album, + allImportedTrackFiles.Where(s => s.AlbumId == album.Id).ToList(), + allOldTrackFiles.Where(s => s.AlbumId == album.Id).ToList(), newDownload, + downloadClientItem)); } } diff --git a/src/NzbDrone.Core/Notifications/AlbumDownloadMessage.cs b/src/NzbDrone.Core/Notifications/AlbumDownloadMessage.cs index 7748ca90f..f2f82fc30 100644 --- a/src/NzbDrone.Core/Notifications/AlbumDownloadMessage.cs +++ b/src/NzbDrone.Core/Notifications/AlbumDownloadMessage.cs @@ -10,6 +10,7 @@ namespace NzbDrone.Core.Notifications public Artist Artist { get; set; } public Album Album { get; set; } public List TrackFiles { get; set; } + public List OldFiles { get; set; } public string DownloadClient { get; set; } public string DownloadId { get; set; } diff --git a/src/NzbDrone.Core/Notifications/CustomScript/CustomScript.cs b/src/NzbDrone.Core/Notifications/CustomScript/CustomScript.cs index 9629cc16b..73f39a56f 100644 --- a/src/NzbDrone.Core/Notifications/CustomScript/CustomScript.cs +++ b/src/NzbDrone.Core/Notifications/CustomScript/CustomScript.cs @@ -117,6 +117,18 @@ namespace NzbDrone.Core.Notifications.CustomScript environmentVariables.Add("Lidarr_Download_Client", message.DownloadClient ?? string.Empty); environmentVariables.Add("Lidarr_Download_Id", message.DownloadId ?? string.Empty); + if (message.TrackFiles.Any()) + { + environmentVariables.Add("Lidarr_AddedTrackRelativePaths", string.Join("|", message.TrackFiles.Select(e => e.RelativePath))); + environmentVariables.Add("Lidarr_AddedTrackPaths", string.Join("|", message.TrackFiles.Select(e => Path.Combine(artist.Path, e.RelativePath)))); + } + + if (message.OldFiles.Any()) + { + environmentVariables.Add("Lidarr_DeletedRelativePaths", string.Join("|", message.OldFiles.Select(e => e.RelativePath))); + environmentVariables.Add("Lidarr_DeletedPaths", string.Join("|", message.OldFiles.Select(e => Path.Combine(artist.Path, e.RelativePath)))); + } + ExecuteScript(environmentVariables); } diff --git a/src/NzbDrone.Core/Notifications/NotificationService.cs b/src/NzbDrone.Core/Notifications/NotificationService.cs index 836d309f0..9bf6d9c0b 100644 --- a/src/NzbDrone.Core/Notifications/NotificationService.cs +++ b/src/NzbDrone.Core/Notifications/NotificationService.cs @@ -4,6 +4,7 @@ using System.Linq; using NLog; using NzbDrone.Common.Extensions; using NzbDrone.Core.Download; +using NzbDrone.Core.MediaFiles; using NzbDrone.Core.MediaFiles.Events; using NzbDrone.Core.Messaging.Events; using NzbDrone.Core.Qualities; @@ -64,7 +65,7 @@ namespace NzbDrone.Core.Notifications qualityString); } - private string GetAlbumDownloadMessage(Artist artist, Album album, List tracks) + private string GetAlbumDownloadMessage(Artist artist, Album album, List tracks) { return string.Format("{0} - {1} ({2} Tracks Imported)", artist.Name, @@ -172,7 +173,9 @@ namespace NzbDrone.Core.Notifications Artist = message.Artist, Album = message.Album, DownloadClient = message.DownloadClient, - DownloadId = message.DownloadId + DownloadId = message.DownloadId, + TrackFiles = message.ImportedTracks, + OldFiles = message.OldFiles, }; foreach (var notification in _notificationFactory.OnAlbumDownloadEnabled()) diff --git a/src/NzbDrone.Core/Notifications/Synology/SynologyIndexer.cs b/src/NzbDrone.Core/Notifications/Synology/SynologyIndexer.cs index d89f8227a..a08f66843 100644 --- a/src/NzbDrone.Core/Notifications/Synology/SynologyIndexer.cs +++ b/src/NzbDrone.Core/Notifications/Synology/SynologyIndexer.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.IO; using FluentValidation.Results; @@ -20,7 +21,7 @@ namespace NzbDrone.Core.Notifications.Synology public override string Name => "Synology Indexer"; - public override void OnDownload(TrackDownloadMessage message) + public override void OnAlbumDownload(AlbumDownloadMessage message) { if (Settings.UpdateLibrary) { @@ -31,8 +32,9 @@ namespace NzbDrone.Core.Notifications.Synology _indexerProxy.DeleteFile(fullPath); } + foreach (var newFile in message.TrackFiles) { - var fullPath = Path.Combine(message.Artist.Path, message.TrackFile.RelativePath); + var fullPath = Path.Combine(message.Artist.Path, newFile.RelativePath); _indexerProxy.AddFile(fullPath); }