Lidarr/src/NzbDrone.Core/Music/MoveArtistService.cs

70 lines
2.6 KiB
C#
Raw Normal View History

2016-12-23 21:45:24 +00:00
using System.IO;
2014-07-23 23:43:54 +00:00
using NLog;
using NzbDrone.Common.Disk;
using NzbDrone.Common.Extensions;
2014-07-23 23:43:54 +00:00
using NzbDrone.Common.Instrumentation.Extensions;
using NzbDrone.Core.Messaging.Commands;
using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Organizer;
using NzbDrone.Core.Music.Commands;
using NzbDrone.Core.Music.Events;
2014-07-23 23:43:54 +00:00
namespace NzbDrone.Core.Music
2014-07-23 23:43:54 +00:00
{
public class MoveArtistService : IExecute<MoveArtistCommand>
2014-07-23 23:43:54 +00:00
{
private readonly IArtistService _artistService;
2014-07-23 23:43:54 +00:00
private readonly IBuildFileNames _filenameBuilder;
private readonly IDiskTransferService _diskTransferService;
2014-07-23 23:43:54 +00:00
private readonly IEventAggregator _eventAggregator;
private readonly Logger _logger;
public MoveArtistService(IArtistService artistService,
2014-07-23 23:43:54 +00:00
IBuildFileNames filenameBuilder,
IDiskTransferService diskTransferService,
2014-07-23 23:43:54 +00:00
IEventAggregator eventAggregator,
Logger logger)
{
_artistService = artistService;
2014-07-23 23:43:54 +00:00
_filenameBuilder = filenameBuilder;
_diskTransferService = diskTransferService;
2014-07-23 23:43:54 +00:00
_eventAggregator = eventAggregator;
_logger = logger;
}
public void Execute(MoveArtistCommand message)
2014-07-23 23:43:54 +00:00
{
var artist = _artistService.GetArtist(message.ArtistId);
2014-07-23 23:43:54 +00:00
var source = message.SourcePath;
var destination = message.DestinationPath;
if (!message.DestinationRootFolder.IsNullOrWhiteSpace())
{
_logger.Debug("Buiding destination path using root folder: {0} and the artist name", message.DestinationRootFolder);
destination = Path.Combine(message.DestinationRootFolder, _filenameBuilder.GetArtistFolder(artist));
2014-07-23 23:43:54 +00:00
}
_logger.ProgressInfo("Moving {0} from '{1}' to '{2}'", artist.Name, source, destination);
2014-07-23 23:43:54 +00:00
//TODO: Move to transactional disk operations
try
{
_diskTransferService.TransferFolder(source, destination, TransferMode.Move);
2014-07-23 23:43:54 +00:00
}
catch (IOException ex)
{
_logger.Error(ex, "Unable to move artist from '{0}' to '{1}'", source, destination);
2014-07-23 23:43:54 +00:00
throw;
}
_logger.ProgressInfo("{0} moved successfully to {1}", artist.Name, artist.Path);
2014-07-23 23:43:54 +00:00
//Update the artist path to the new path
artist.Path = destination;
artist = _artistService.UpdateArtist(artist);
2014-07-23 23:43:54 +00:00
_eventAggregator.PublishEvent(new ArtistMovedEvent(artist, source, destination));
2014-07-23 23:43:54 +00:00
}
}
}