Fixed: Update file paths correctly when moving artist

Fixes #1088
This commit is contained in:
ta264 2020-03-30 19:58:11 +01:00 committed by Qstick
commit 4f5a0b7afd
7 changed files with 61 additions and 23 deletions

View file

@ -81,12 +81,13 @@ namespace Lidarr.Api.V1.Artist
} }
} }
if (resource.MoveFiles && artistToMove.Any()) if (artistToMove.Any())
{ {
_commandQueueManager.Push(new BulkMoveArtistCommand _commandQueueManager.Push(new BulkMoveArtistCommand
{ {
DestinationRootFolder = resource.RootFolderPath, DestinationRootFolder = resource.RootFolderPath,
Artist = artistToMove Artist = artistToMove,
MoveFiles = resource.MoveFiles
}); });
} }

View file

@ -145,19 +145,17 @@ namespace Lidarr.Api.V1.Artist
var moveFiles = Request.GetBooleanQueryParameter("moveFiles"); var moveFiles = Request.GetBooleanQueryParameter("moveFiles");
var artist = _artistService.GetArtist(artistResource.Id); 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 _commandQueueManager.Push(new MoveArtistCommand
{ {
ArtistId = artist.Id, ArtistId = artist.Id,
SourcePath = sourcePath, SourcePath = sourcePath,
DestinationPath = destinationPath, DestinationPath = destinationPath,
Trigger = CommandTrigger.Manual MoveFiles = moveFiles,
}); Trigger = CommandTrigger.Manual
} });
var model = artistResource.ToModel(artist); var model = artistResource.ToModel(artist);

View file

@ -5,8 +5,10 @@ using FizzWare.NBuilder;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Common.Disk; using NzbDrone.Common.Disk;
using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Music; using NzbDrone.Core.Music;
using NzbDrone.Core.Music.Commands; using NzbDrone.Core.Music.Commands;
using NzbDrone.Core.Music.Events;
using NzbDrone.Core.Organizer; using NzbDrone.Core.Organizer;
using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Test.Framework;
using NzbDrone.Test.Common; using NzbDrone.Test.Common;
@ -29,9 +31,10 @@ namespace NzbDrone.Core.Test.MusicTests
_command = new MoveArtistCommand _command = new MoveArtistCommand
{ {
ArtistId = 1, ArtistId = _artist.Id,
SourcePath = @"C:\Test\Music\Artist".AsOsAgnostic(), SourcePath = @"C:\Test\Music\Artist".AsOsAgnostic(),
DestinationPath = @"C:\Test\Music2\Artist".AsOsAgnostic() DestinationPath = @"C:\Test\Music2\Artist".AsOsAgnostic(),
MoveFiles = true
}; };
_bulkCommand = new BulkMoveArtistCommand _bulkCommand = new BulkMoveArtistCommand
@ -40,11 +43,12 @@ namespace NzbDrone.Core.Test.MusicTests
{ {
new BulkMoveArtist new BulkMoveArtist
{ {
ArtistId = 1, ArtistId = _artist.Id,
SourcePath = @"C:\Test\Music\Artist".AsOsAgnostic() SourcePath = @"C:\Test\Music\Artist".AsOsAgnostic()
} }
}, },
DestinationRootFolder = @"C:\Test\Music2".AsOsAgnostic() DestinationRootFolder = @"C:\Test\Music2".AsOsAgnostic(),
MoveFiles = true
}; };
Mocker.GetMock<IArtistService>() Mocker.GetMock<IArtistService>()
@ -143,5 +147,33 @@ namespace NzbDrone.Core.Test.MusicTests
Mocker.GetMock<IBuildFileNames>() Mocker.GetMock<IBuildFileNames>()
.Verify(v => v.GetArtistFolder(It.IsAny<Artist>(), null), Times.Never()); .Verify(v => v.GetArtistFolder(It.IsAny<Artist>(), null), Times.Never());
} }
[Test]
public void should_raise_artist_moved_event_when_move_files_false()
{
_command.MoveFiles = false;
Subject.Execute(_command);
Mocker.GetMock<IEventAggregator>()
.Verify(v => v.PublishEvent(It.Is<ArtistMovedEvent>(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<IBuildFileNames>()
.Setup(s => s.GetArtistFolder(It.IsAny<Artist>(), null))
.Returns(artistFolder);
Subject.Execute(_bulkCommand);
Mocker.GetMock<IEventAggregator>()
.Verify(v => v.PublishEvent(It.Is<ArtistMovedEvent>(c => c.Artist.Id == _artist.Id)), Times.Once());
}
} }
} }

View file

@ -203,6 +203,7 @@ namespace NzbDrone.Core.MediaFiles
public void Handle(ArtistMovedEvent message) public void Handle(ArtistMovedEvent message)
{ {
// TODO: Be more careful when arbitrary artist paths are allowed
var files = _mediaFileRepository.GetFilesWithBasePath(message.SourcePath); var files = _mediaFileRepository.GetFilesWithBasePath(message.SourcePath);
foreach (var file in files) foreach (var file in files)

View file

@ -8,6 +8,7 @@ namespace NzbDrone.Core.Music.Commands
{ {
public List<BulkMoveArtist> Artist { get; set; } public List<BulkMoveArtist> Artist { get; set; }
public string DestinationRootFolder { get; set; } public string DestinationRootFolder { get; set; }
public bool MoveFiles { get; set; }
public override bool SendUpdatesToClient => true; public override bool SendUpdatesToClient => true;
public override bool RequiresDiskAccess => true; public override bool RequiresDiskAccess => true;

View file

@ -7,6 +7,7 @@ namespace NzbDrone.Core.Music.Commands
public int ArtistId { get; set; } public int ArtistId { get; set; }
public string SourcePath { get; set; } public string SourcePath { get; set; }
public string DestinationPath { get; set; } public string DestinationPath { get; set; }
public bool MoveFiles { get; set; }
public override bool SendUpdatesToClient => true; public override bool SendUpdatesToClient => true;
public override bool RequiresDiskAccess => true; public override bool RequiresDiskAccess => true;

View file

@ -34,7 +34,7 @@ namespace NzbDrone.Core.Music
_logger = logger; _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)) if (!_diskProvider.FolderExists(sourcePath))
{ {
@ -53,9 +53,12 @@ namespace NzbDrone.Core.Music
try 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)); _eventAggregator.PublishEvent(new ArtistMovedEvent(artist, sourcePath, destinationPath));
} }
@ -78,7 +81,8 @@ namespace NzbDrone.Core.Music
public void Execute(MoveArtistCommand message) public void Execute(MoveArtistCommand message)
{ {
var artist = _artistService.GetArtist(message.ArtistId); 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) public void Execute(BulkMoveArtistCommand message)
@ -94,7 +98,7 @@ namespace NzbDrone.Core.Music
var artist = _artistService.GetArtist(s.ArtistId); var artist = _artistService.GetArtist(s.ArtistId);
var destinationPath = Path.Combine(destinationRootFolder, _filenameBuilder.GetArtistFolder(artist)); 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); _logger.ProgressInfo("Finished moving {0} artist to '{1}'", artistToMove.Count, destinationRootFolder);