2013-03-07 04:49:00 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Common;
|
2013-04-24 01:56:00 +00:00
|
|
|
|
using NzbDrone.Common.Messaging;
|
2013-04-07 18:13:09 +00:00
|
|
|
|
using NzbDrone.Core.MediaFiles.Events;
|
2013-03-07 04:49:00 +00:00
|
|
|
|
using NzbDrone.Core.Organizer;
|
2013-07-06 21:47:49 +00:00
|
|
|
|
using NzbDrone.Core.Parser.Model;
|
2013-03-07 04:49:00 +00:00
|
|
|
|
using NzbDrone.Core.Tv;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.MediaFiles
|
|
|
|
|
{
|
|
|
|
|
public interface IMoveEpisodeFiles
|
|
|
|
|
{
|
2013-07-19 05:23:04 +00:00
|
|
|
|
EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile, Series series);
|
2013-07-06 21:47:49 +00:00
|
|
|
|
EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode);
|
2013-08-08 05:40:31 +00:00
|
|
|
|
EpisodeFile PreMoveEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode);
|
2013-03-07 04:49:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class MoveEpisodeFiles : IMoveEpisodeFiles
|
|
|
|
|
{
|
|
|
|
|
private readonly IEpisodeService _episodeService;
|
|
|
|
|
private readonly IBuildFileNames _buildFileNames;
|
2013-04-24 01:56:00 +00:00
|
|
|
|
private readonly IMessageAggregator _messageAggregator;
|
2013-05-10 23:53:50 +00:00
|
|
|
|
private readonly IDiskProvider _diskProvider;
|
2013-03-07 04:49:00 +00:00
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
2013-08-08 05:40:31 +00:00
|
|
|
|
public MoveEpisodeFiles(IEpisodeService episodeService,
|
2013-07-16 02:56:46 +00:00
|
|
|
|
IBuildFileNames buildFileNames,
|
|
|
|
|
IMessageAggregator messageAggregator,
|
|
|
|
|
IDiskProvider diskProvider,
|
|
|
|
|
Logger logger)
|
2013-03-07 04:49:00 +00:00
|
|
|
|
{
|
|
|
|
|
_episodeService = episodeService;
|
|
|
|
|
_buildFileNames = buildFileNames;
|
2013-04-24 01:56:00 +00:00
|
|
|
|
_messageAggregator = messageAggregator;
|
2013-03-07 04:49:00 +00:00
|
|
|
|
_diskProvider = diskProvider;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-19 05:23:04 +00:00
|
|
|
|
public EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile, Series series)
|
|
|
|
|
{
|
|
|
|
|
var episodes = _episodeService.GetEpisodesByFileId(episodeFile.Id);
|
|
|
|
|
var newFileName = _buildFileNames.BuildFilename(episodes, series, episodeFile);
|
|
|
|
|
var destinationFilename = _buildFileNames.BuildFilePath(series, episodes.First().SeasonNumber, newFileName, Path.GetExtension(episodeFile.Path));
|
|
|
|
|
|
|
|
|
|
return MoveFile(episodeFile, destinationFilename);
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-06 21:47:49 +00:00
|
|
|
|
public EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode)
|
|
|
|
|
{
|
|
|
|
|
var newFileName = _buildFileNames.BuildFilename(localEpisode.Episodes, localEpisode.Series, episodeFile);
|
|
|
|
|
var destinationFilename = _buildFileNames.BuildFilePath(localEpisode.Series, localEpisode.SeasonNumber, newFileName, Path.GetExtension(episodeFile.Path));
|
|
|
|
|
episodeFile = MoveFile(episodeFile, destinationFilename);
|
|
|
|
|
|
2013-08-01 07:11:42 +00:00
|
|
|
|
_messageAggregator.PublishEvent(new EpisodeDownloadedEvent(localEpisode));
|
2013-07-06 21:47:49 +00:00
|
|
|
|
|
|
|
|
|
return episodeFile;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-08 05:40:31 +00:00
|
|
|
|
public EpisodeFile PreMoveEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode)
|
|
|
|
|
{
|
|
|
|
|
var newFileName = Path.GetFileNameWithoutExtension(episodeFile.Path);
|
|
|
|
|
var destinationFilename = _buildFileNames.BuildFilePath(localEpisode.Series, localEpisode.SeasonNumber, newFileName, Path.GetExtension(episodeFile.Path));
|
|
|
|
|
episodeFile = MoveFile(episodeFile, destinationFilename);
|
|
|
|
|
|
|
|
|
|
return episodeFile;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-06 21:47:49 +00:00
|
|
|
|
private EpisodeFile MoveFile(EpisodeFile episodeFile, string destinationFilename)
|
|
|
|
|
{
|
|
|
|
|
if (!_diskProvider.FileExists(episodeFile.Path))
|
2013-03-07 04:49:00 +00:00
|
|
|
|
{
|
2013-07-06 21:47:49 +00:00
|
|
|
|
_logger.Error("Episode file path does not exist, {0}", episodeFile.Path);
|
2013-03-07 04:49:00 +00:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-06 21:47:49 +00:00
|
|
|
|
//Only rename if existing and new filenames don't match
|
|
|
|
|
if (DiskProvider.PathEquals(episodeFile.Path, destinationFilename))
|
2013-03-07 04:49:00 +00:00
|
|
|
|
{
|
2013-07-06 21:47:49 +00:00
|
|
|
|
_logger.Debug("Skipping file rename, source and destination are the same: {0}", episodeFile.Path);
|
2013-03-07 04:49:00 +00:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-06 21:47:49 +00:00
|
|
|
|
_diskProvider.CreateFolder(new FileInfo(destinationFilename).DirectoryName);
|
2013-03-07 04:49:00 +00:00
|
|
|
|
|
2013-07-06 21:47:49 +00:00
|
|
|
|
_logger.Debug("Moving [{0}] > [{1}]", episodeFile.Path, destinationFilename);
|
|
|
|
|
_diskProvider.MoveFile(episodeFile.Path, destinationFilename);
|
2013-03-07 04:49:00 +00: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 21:47:49 +00:00
|
|
|
|
_diskProvider.InheritFolderPermissions(destinationFilename);
|
2013-03-07 04:49:00 +00:00
|
|
|
|
}
|
|
|
|
|
catch (UnauthorizedAccessException ex)
|
|
|
|
|
{
|
2013-07-06 21:47:49 +00:00
|
|
|
|
_logger.Debug("Unable to apply folder permissions to: ", destinationFilename);
|
2013-03-07 04:49:00 +00:00
|
|
|
|
_logger.TraceException(ex.Message, ex);
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-06 21:47:49 +00:00
|
|
|
|
episodeFile.Path = destinationFilename;
|
|
|
|
|
|
2013-03-07 04:49:00 +00:00
|
|
|
|
return episodeFile;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|