From 4f5a0b7afd495ca34e9f879ab423a305461247cf Mon Sep 17 00:00:00 2001 From: ta264 Date: Mon, 30 Mar 2020 19:58:11 +0100 Subject: [PATCH] Fixed: Update file paths correctly when moving artist Fixes #1088 --- .../Artist/ArtistEditorModule.cs | 5 ++- src/Lidarr.Api.V1/Artist/ArtistModule.cs | 22 +++++----- .../MusicTests/MoveArtistServiceFixture.cs | 40 +++++++++++++++++-- .../MediaFiles/MediaFileService.cs | 1 + .../Music/Commands/BulkMoveArtistCommand.cs | 1 + .../Music/Commands/MoveArtistCommand.cs | 1 + .../Music/Services/MoveArtistService.cs | 14 ++++--- 7 files changed, 61 insertions(+), 23 deletions(-) diff --git a/src/Lidarr.Api.V1/Artist/ArtistEditorModule.cs b/src/Lidarr.Api.V1/Artist/ArtistEditorModule.cs index e20761675..ba23a0843 100644 --- a/src/Lidarr.Api.V1/Artist/ArtistEditorModule.cs +++ b/src/Lidarr.Api.V1/Artist/ArtistEditorModule.cs @@ -81,12 +81,13 @@ namespace Lidarr.Api.V1.Artist } } - if (resource.MoveFiles && artistToMove.Any()) + if (artistToMove.Any()) { _commandQueueManager.Push(new BulkMoveArtistCommand { DestinationRootFolder = resource.RootFolderPath, - Artist = artistToMove + Artist = artistToMove, + MoveFiles = resource.MoveFiles }); } diff --git a/src/Lidarr.Api.V1/Artist/ArtistModule.cs b/src/Lidarr.Api.V1/Artist/ArtistModule.cs index 362f155d0..52528dd6f 100644 --- a/src/Lidarr.Api.V1/Artist/ArtistModule.cs +++ b/src/Lidarr.Api.V1/Artist/ArtistModule.cs @@ -145,19 +145,17 @@ namespace Lidarr.Api.V1.Artist var moveFiles = Request.GetBooleanQueryParameter("moveFiles"); var artist = _artistService.GetArtist(artistResource.Id); - if (moveFiles) - { - var sourcePath = artist.Path; - var destinationPath = artistResource.Path; + var sourcePath = artist.Path; + var destinationPath = artistResource.Path; - _commandQueueManager.Push(new MoveArtistCommand - { - ArtistId = artist.Id, - SourcePath = sourcePath, - DestinationPath = destinationPath, - Trigger = CommandTrigger.Manual - }); - } + _commandQueueManager.Push(new MoveArtistCommand + { + ArtistId = artist.Id, + SourcePath = sourcePath, + DestinationPath = destinationPath, + MoveFiles = moveFiles, + Trigger = CommandTrigger.Manual + }); var model = artistResource.ToModel(artist); diff --git a/src/NzbDrone.Core.Test/MusicTests/MoveArtistServiceFixture.cs b/src/NzbDrone.Core.Test/MusicTests/MoveArtistServiceFixture.cs index bd448cc16..0b58817cd 100644 --- a/src/NzbDrone.Core.Test/MusicTests/MoveArtistServiceFixture.cs +++ b/src/NzbDrone.Core.Test/MusicTests/MoveArtistServiceFixture.cs @@ -5,8 +5,10 @@ using FizzWare.NBuilder; using Moq; using NUnit.Framework; using NzbDrone.Common.Disk; +using NzbDrone.Core.Messaging.Events; using NzbDrone.Core.Music; using NzbDrone.Core.Music.Commands; +using NzbDrone.Core.Music.Events; using NzbDrone.Core.Organizer; using NzbDrone.Core.Test.Framework; using NzbDrone.Test.Common; @@ -29,9 +31,10 @@ namespace NzbDrone.Core.Test.MusicTests _command = new MoveArtistCommand { - ArtistId = 1, + ArtistId = _artist.Id, SourcePath = @"C:\Test\Music\Artist".AsOsAgnostic(), - DestinationPath = @"C:\Test\Music2\Artist".AsOsAgnostic() + DestinationPath = @"C:\Test\Music2\Artist".AsOsAgnostic(), + MoveFiles = true }; _bulkCommand = new BulkMoveArtistCommand @@ -40,11 +43,12 @@ namespace NzbDrone.Core.Test.MusicTests { new BulkMoveArtist { - ArtistId = 1, + ArtistId = _artist.Id, SourcePath = @"C:\Test\Music\Artist".AsOsAgnostic() } }, - DestinationRootFolder = @"C:\Test\Music2".AsOsAgnostic() + DestinationRootFolder = @"C:\Test\Music2".AsOsAgnostic(), + MoveFiles = true }; Mocker.GetMock() @@ -143,5 +147,33 @@ namespace NzbDrone.Core.Test.MusicTests Mocker.GetMock() .Verify(v => v.GetArtistFolder(It.IsAny(), null), Times.Never()); } + + [Test] + public void should_raise_artist_moved_event_when_move_files_false() + { + _command.MoveFiles = false; + Subject.Execute(_command); + + Mocker.GetMock() + .Verify(v => v.PublishEvent(It.Is(c => c.Artist.Id == _artist.Id)), Times.Once()); + } + + [Test] + public void should_raise_artist_moved_event_when_move_files_false_bulk() + { + _bulkCommand.MoveFiles = false; + + var artistFolder = "Artist"; + var expectedPath = Path.Combine(_bulkCommand.DestinationRootFolder, artistFolder); + + Mocker.GetMock() + .Setup(s => s.GetArtistFolder(It.IsAny(), null)) + .Returns(artistFolder); + + Subject.Execute(_bulkCommand); + + Mocker.GetMock() + .Verify(v => v.PublishEvent(It.Is(c => c.Artist.Id == _artist.Id)), Times.Once()); + } } } diff --git a/src/NzbDrone.Core/MediaFiles/MediaFileService.cs b/src/NzbDrone.Core/MediaFiles/MediaFileService.cs index d34ceced2..ff4188497 100644 --- a/src/NzbDrone.Core/MediaFiles/MediaFileService.cs +++ b/src/NzbDrone.Core/MediaFiles/MediaFileService.cs @@ -203,6 +203,7 @@ namespace NzbDrone.Core.MediaFiles public void Handle(ArtistMovedEvent message) { + // TODO: Be more careful when arbitrary artist paths are allowed var files = _mediaFileRepository.GetFilesWithBasePath(message.SourcePath); foreach (var file in files) diff --git a/src/NzbDrone.Core/Music/Commands/BulkMoveArtistCommand.cs b/src/NzbDrone.Core/Music/Commands/BulkMoveArtistCommand.cs index 8f035792b..23e0e11c7 100644 --- a/src/NzbDrone.Core/Music/Commands/BulkMoveArtistCommand.cs +++ b/src/NzbDrone.Core/Music/Commands/BulkMoveArtistCommand.cs @@ -8,6 +8,7 @@ namespace NzbDrone.Core.Music.Commands { public List Artist { get; set; } public string DestinationRootFolder { get; set; } + public bool MoveFiles { get; set; } public override bool SendUpdatesToClient => true; public override bool RequiresDiskAccess => true; diff --git a/src/NzbDrone.Core/Music/Commands/MoveArtistCommand.cs b/src/NzbDrone.Core/Music/Commands/MoveArtistCommand.cs index c120eddd4..1f46f2f6b 100644 --- a/src/NzbDrone.Core/Music/Commands/MoveArtistCommand.cs +++ b/src/NzbDrone.Core/Music/Commands/MoveArtistCommand.cs @@ -7,6 +7,7 @@ namespace NzbDrone.Core.Music.Commands public int ArtistId { get; set; } public string SourcePath { get; set; } public string DestinationPath { get; set; } + public bool MoveFiles { get; set; } public override bool SendUpdatesToClient => true; public override bool RequiresDiskAccess => true; diff --git a/src/NzbDrone.Core/Music/Services/MoveArtistService.cs b/src/NzbDrone.Core/Music/Services/MoveArtistService.cs index 21ee8a2e1..bb8f9643d 100644 --- a/src/NzbDrone.Core/Music/Services/MoveArtistService.cs +++ b/src/NzbDrone.Core/Music/Services/MoveArtistService.cs @@ -34,7 +34,7 @@ namespace NzbDrone.Core.Music _logger = logger; } - private void MoveSingleArtist(Artist artist, string sourcePath, string destinationPath, int? index = null, int? total = null) + private void MoveSingleArtist(Artist artist, string sourcePath, string destinationPath, bool moveFiles, int? index = null, int? total = null) { if (!_diskProvider.FolderExists(sourcePath)) { @@ -53,9 +53,12 @@ namespace NzbDrone.Core.Music try { - _diskTransferService.TransferFolder(sourcePath, destinationPath, TransferMode.Move); + if (moveFiles) + { + _diskTransferService.TransferFolder(sourcePath, destinationPath, TransferMode.Move); - _logger.ProgressInfo("{0} moved successfully to {1}", artist.Name, artist.Path); + _logger.ProgressInfo("{0} moved successfully to {1}", artist.Name, artist.Path); + } _eventAggregator.PublishEvent(new ArtistMovedEvent(artist, sourcePath, destinationPath)); } @@ -78,7 +81,8 @@ namespace NzbDrone.Core.Music public void Execute(MoveArtistCommand message) { var artist = _artistService.GetArtist(message.ArtistId); - MoveSingleArtist(artist, message.SourcePath, message.DestinationPath); + + MoveSingleArtist(artist, message.SourcePath, message.DestinationPath, message.MoveFiles); } public void Execute(BulkMoveArtistCommand message) @@ -94,7 +98,7 @@ namespace NzbDrone.Core.Music var artist = _artistService.GetArtist(s.ArtistId); var destinationPath = Path.Combine(destinationRootFolder, _filenameBuilder.GetArtistFolder(artist)); - MoveSingleArtist(artist, s.SourcePath, destinationPath, index, artistToMove.Count); + MoveSingleArtist(artist, s.SourcePath, destinationPath, message.MoveFiles, index, artistToMove.Count); } _logger.ProgressInfo("Finished moving {0} artist to '{1}'", artistToMove.Count, destinationRootFolder);