2013-03-07 05:49:00 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Common;
|
2013-09-11 08:33:47 +02:00
|
|
|
|
using NzbDrone.Core.Messaging;
|
2013-03-07 05:49:00 +01:00
|
|
|
|
using NzbDrone.Core.Organizer;
|
2013-07-06 23:47:49 +02:00
|
|
|
|
using NzbDrone.Core.Parser.Model;
|
2013-03-07 05:49:00 +01:00
|
|
|
|
using NzbDrone.Core.Tv;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.MediaFiles
|
|
|
|
|
{
|
|
|
|
|
public interface IMoveEpisodeFiles
|
|
|
|
|
{
|
2013-08-30 08:39:41 +02:00
|
|
|
|
string MoveEpisodeFile(EpisodeFile episodeFile, Series series);
|
|
|
|
|
string MoveEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode);
|
2013-03-07 05:49:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class MoveEpisodeFiles : IMoveEpisodeFiles
|
|
|
|
|
{
|
|
|
|
|
private readonly IEpisodeService _episodeService;
|
|
|
|
|
private readonly IBuildFileNames _buildFileNames;
|
2013-04-24 03:56:00 +02:00
|
|
|
|
private readonly IMessageAggregator _messageAggregator;
|
2013-05-11 01:53:50 +02:00
|
|
|
|
private readonly IDiskProvider _diskProvider;
|
2013-03-07 05:49:00 +01:00
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
2013-08-08 07:40:31 +02:00
|
|
|
|
public MoveEpisodeFiles(IEpisodeService episodeService,
|
2013-07-16 04:56:46 +02:00
|
|
|
|
IBuildFileNames buildFileNames,
|
|
|
|
|
IMessageAggregator messageAggregator,
|
|
|
|
|
IDiskProvider diskProvider,
|
|
|
|
|
Logger logger)
|
2013-03-07 05:49:00 +01:00
|
|
|
|
{
|
|
|
|
|
_episodeService = episodeService;
|
|
|
|
|
_buildFileNames = buildFileNames;
|
2013-04-24 03:56:00 +02:00
|
|
|
|
_messageAggregator = messageAggregator;
|
2013-03-07 05:49:00 +01:00
|
|
|
|
_diskProvider = diskProvider;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-30 08:39:41 +02:00
|
|
|
|
public string MoveEpisodeFile(EpisodeFile episodeFile, Series series)
|
2013-07-19 07:23:04 +02:00
|
|
|
|
{
|
|
|
|
|
var episodes = _episodeService.GetEpisodesByFileId(episodeFile.Id);
|
|
|
|
|
var newFileName = _buildFileNames.BuildFilename(episodes, series, episodeFile);
|
2013-08-30 08:39:41 +02:00
|
|
|
|
var filePath = _buildFileNames.BuildFilePath(series, episodes.First().SeasonNumber, newFileName, Path.GetExtension(episodeFile.Path));
|
2013-09-02 06:03:49 +02:00
|
|
|
|
MoveFile(episodeFile, filePath);
|
2013-07-19 07:23:04 +02:00
|
|
|
|
|
2013-08-30 08:39:41 +02:00
|
|
|
|
return filePath;
|
2013-07-19 07:23:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-30 08:39:41 +02:00
|
|
|
|
public string MoveEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode)
|
2013-07-06 23:47:49 +02:00
|
|
|
|
{
|
|
|
|
|
var newFileName = _buildFileNames.BuildFilename(localEpisode.Episodes, localEpisode.Series, episodeFile);
|
2013-08-30 08:39:41 +02:00
|
|
|
|
var filePath = _buildFileNames.BuildFilePath(localEpisode.Series, localEpisode.SeasonNumber, newFileName, Path.GetExtension(episodeFile.Path));
|
2013-09-02 06:03:49 +02:00
|
|
|
|
MoveFile(episodeFile, filePath);
|
|
|
|
|
|
2013-08-30 08:39:41 +02:00
|
|
|
|
return filePath;
|
2013-07-06 23:47:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-30 08:39:41 +02:00
|
|
|
|
private void MoveFile(EpisodeFile episodeFile, string destinationFilename)
|
2013-07-06 23:47:49 +02:00
|
|
|
|
{
|
|
|
|
|
if (!_diskProvider.FileExists(episodeFile.Path))
|
2013-03-07 05:49:00 +01:00
|
|
|
|
{
|
2013-08-13 04:01:15 +02:00
|
|
|
|
throw new FileNotFoundException("Episode file path does not exist", episodeFile.Path);
|
2013-03-07 05:49:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-20 21:28:46 +02:00
|
|
|
|
if (episodeFile.Path.PathEquals(destinationFilename))
|
2013-03-07 05:49:00 +01:00
|
|
|
|
{
|
2013-08-13 04:01:15 +02:00
|
|
|
|
throw new SameFilenameException("File not moved, source and destination are the same", episodeFile.Path);
|
2013-03-07 05:49:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-06 23:47:49 +02:00
|
|
|
|
_diskProvider.CreateFolder(new FileInfo(destinationFilename).DirectoryName);
|
2013-03-07 05:49:00 +01:00
|
|
|
|
|
2013-07-06 23:47:49 +02:00
|
|
|
|
_logger.Debug("Moving [{0}] > [{1}]", episodeFile.Path, destinationFilename);
|
|
|
|
|
_diskProvider.MoveFile(episodeFile.Path, destinationFilename);
|
2013-03-07 05:49:00 +01:00
|
|
|
|
|
|
|
|
|
//Wrapped in Try/Catch to prevent this from causing issues with remote NAS boxes, the move worked, which is more important.
|
|
|
|
|
try
|
|
|
|
|
{
|
2013-07-06 23:47:49 +02:00
|
|
|
|
_diskProvider.InheritFolderPermissions(destinationFilename);
|
2013-03-07 05:49:00 +01:00
|
|
|
|
}
|
|
|
|
|
catch (UnauthorizedAccessException ex)
|
|
|
|
|
{
|
2013-07-06 23:47:49 +02:00
|
|
|
|
_logger.Debug("Unable to apply folder permissions to: ", destinationFilename);
|
2013-03-07 05:49:00 +01:00
|
|
|
|
_logger.TraceException(ex.Message, ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|