2018-03-14 20:41:36 +00:00
|
|
|
using System.IO;
|
2014-07-23 23:43:54 +00:00
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Common.Disk;
|
2014-12-02 06:26:25 +00:00
|
|
|
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;
|
2018-03-14 20:41:36 +00:00
|
|
|
using NzbDrone.Core.Movies.Commands;
|
|
|
|
using NzbDrone.Core.Movies.Events;
|
2014-07-23 23:43:54 +00:00
|
|
|
using NzbDrone.Core.Organizer;
|
|
|
|
|
2018-03-14 20:41:36 +00:00
|
|
|
namespace NzbDrone.Core.Movies
|
2014-07-23 23:43:54 +00:00
|
|
|
{
|
2018-03-14 20:41:36 +00:00
|
|
|
public class MoveMovieService : IExecute<MoveMovieCommand>
|
2014-07-23 23:43:54 +00:00
|
|
|
{
|
2018-03-14 20:41:36 +00:00
|
|
|
private readonly IMovieService _movieService;
|
2014-07-23 23:43:54 +00:00
|
|
|
private readonly IBuildFileNames _filenameBuilder;
|
2014-11-15 00:42:22 +00:00
|
|
|
private readonly IDiskTransferService _diskTransferService;
|
2014-07-23 23:43:54 +00:00
|
|
|
private readonly IEventAggregator _eventAggregator;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
2018-03-14 20:41:36 +00:00
|
|
|
public MoveMovieService(IMovieService movieService,
|
2014-07-23 23:43:54 +00:00
|
|
|
IBuildFileNames filenameBuilder,
|
2014-11-15 00:42:22 +00:00
|
|
|
IDiskTransferService diskTransferService,
|
2014-07-23 23:43:54 +00:00
|
|
|
IEventAggregator eventAggregator,
|
|
|
|
Logger logger)
|
|
|
|
{
|
2018-03-14 20:41:36 +00:00
|
|
|
_movieService = movieService;
|
2014-07-23 23:43:54 +00:00
|
|
|
_filenameBuilder = filenameBuilder;
|
2014-11-15 00:42:22 +00:00
|
|
|
_diskTransferService = diskTransferService;
|
2014-07-23 23:43:54 +00:00
|
|
|
_eventAggregator = eventAggregator;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2018-03-14 20:41:36 +00:00
|
|
|
public void Execute(MoveMovieCommand message)
|
2014-07-23 23:43:54 +00:00
|
|
|
{
|
2018-03-14 20:41:36 +00:00
|
|
|
var movie = _movieService.GetMovie(message.MovieId);
|
2014-07-23 23:43:54 +00:00
|
|
|
var source = message.SourcePath;
|
|
|
|
var destination = message.DestinationPath;
|
|
|
|
|
|
|
|
if (!message.DestinationRootFolder.IsNullOrWhiteSpace())
|
|
|
|
{
|
2018-03-14 20:41:36 +00:00
|
|
|
_logger.Debug("Buiding destination path using root folder: {0} and the movie title", message.DestinationRootFolder);
|
|
|
|
destination = Path.Combine(message.DestinationRootFolder, _filenameBuilder.GetMovieFolder(movie));
|
2014-07-23 23:43:54 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 20:41:36 +00:00
|
|
|
_logger.ProgressInfo("Moving {0} from '{1}' to '{2}'", movie.Title, source, destination);
|
2014-07-23 23:43:54 +00:00
|
|
|
|
|
|
|
//TODO: Move to transactional disk operations
|
|
|
|
try
|
|
|
|
{
|
2014-11-15 00:42:22 +00:00
|
|
|
_diskTransferService.TransferFolder(source, destination, TransferMode.Move);
|
2014-07-23 23:43:54 +00:00
|
|
|
}
|
|
|
|
catch (IOException ex)
|
|
|
|
{
|
2018-03-14 20:41:36 +00:00
|
|
|
var errorMessage = string.Format("Unable to move movie from '{0}' to '{1}'", source, destination);
|
2014-07-23 23:43:54 +00:00
|
|
|
|
2016-02-11 21:13:42 +00:00
|
|
|
_logger.Error(ex, errorMessage);
|
2014-07-23 23:43:54 +00:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2018-03-14 20:41:36 +00:00
|
|
|
_logger.ProgressInfo("{0} moved successfully to {1}", movie.Title, movie.Path);
|
2014-07-23 23:43:54 +00:00
|
|
|
|
2018-03-14 20:41:36 +00:00
|
|
|
//Update the movie path to the new path
|
|
|
|
movie.Path = destination;
|
|
|
|
movie = _movieService.UpdateMovie(movie);
|
2014-07-23 23:43:54 +00:00
|
|
|
|
2018-03-14 20:41:36 +00:00
|
|
|
_eventAggregator.PublishEvent(new MovieMovedEvent(movie, source, destination));
|
2014-07-23 23:43:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|