2013-03-07 05:49:00 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Common;
|
2013-04-24 03:56:00 +02:00
|
|
|
|
using NzbDrone.Common.Messaging;
|
2013-04-07 20:13:09 +02:00
|
|
|
|
using NzbDrone.Core.MediaFiles.Events;
|
2013-03-07 05:49:00 +01:00
|
|
|
|
using NzbDrone.Core.Organizer;
|
|
|
|
|
using NzbDrone.Core.Tv;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.MediaFiles
|
|
|
|
|
{
|
|
|
|
|
public interface IMoveEpisodeFiles
|
|
|
|
|
{
|
|
|
|
|
EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile, bool newDownload = false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class MoveEpisodeFiles : IMoveEpisodeFiles
|
|
|
|
|
{
|
|
|
|
|
private readonly ISeriesRepository _seriesRepository;
|
|
|
|
|
private readonly IEpisodeService _episodeService;
|
|
|
|
|
private readonly IBuildFileNames _buildFileNames;
|
|
|
|
|
private readonly IMediaFileService _mediaFileService;
|
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-05-11 01:53:50 +02:00
|
|
|
|
public MoveEpisodeFiles(ISeriesRepository seriesRepository, IEpisodeService episodeService, IBuildFileNames buildFileNames, IMediaFileService mediaFileService, IMessageAggregator messageAggregator, IDiskProvider diskProvider, Logger logger)
|
2013-03-07 05:49:00 +01:00
|
|
|
|
{
|
|
|
|
|
_seriesRepository = seriesRepository;
|
|
|
|
|
_episodeService = episodeService;
|
|
|
|
|
_buildFileNames = buildFileNames;
|
|
|
|
|
_mediaFileService = mediaFileService;
|
2013-04-24 03:56:00 +02:00
|
|
|
|
_messageAggregator = messageAggregator;
|
2013-03-07 05:49:00 +01:00
|
|
|
|
_diskProvider = diskProvider;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile, bool newDownload = false)
|
|
|
|
|
{
|
|
|
|
|
if (episodeFile == null)
|
|
|
|
|
throw new ArgumentNullException("episodeFile");
|
|
|
|
|
|
|
|
|
|
var series = _seriesRepository.Get(episodeFile.SeriesId);
|
|
|
|
|
var episodes = _episodeService.GetEpisodesByFileId(episodeFile.Id);
|
|
|
|
|
string newFileName = _buildFileNames.BuildFilename(episodes, series, episodeFile);
|
|
|
|
|
var newFile = _buildFileNames.BuildFilePath(series, episodes.First().SeasonNumber, newFileName, Path.GetExtension(episodeFile.Path));
|
|
|
|
|
|
|
|
|
|
//Only rename if existing and new filenames don't match
|
|
|
|
|
if (DiskProvider.PathEquals(episodeFile.Path, newFile))
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Skipping file rename, source and destination are the same: {0}", episodeFile.Path);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!_diskProvider.FileExists(episodeFile.Path))
|
|
|
|
|
{
|
|
|
|
|
_logger.Error("Episode file path does not exist, {0}", episodeFile.Path);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-15 03:41:39 +02:00
|
|
|
|
_diskProvider.CreateFolder(new FileInfo(newFile).DirectoryName);
|
2013-03-07 05:49:00 +01:00
|
|
|
|
|
|
|
|
|
_logger.Debug("Moving [{0}] > [{1}]", episodeFile.Path, newFile);
|
|
|
|
|
_diskProvider.MoveFile(episodeFile.Path, newFile);
|
|
|
|
|
|
|
|
|
|
//Wrapped in Try/Catch to prevent this from causing issues with remote NAS boxes, the move worked, which is more important.
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_diskProvider.InheritFolderPermissions(newFile);
|
|
|
|
|
}
|
|
|
|
|
catch (UnauthorizedAccessException ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Unable to apply folder permissions to: ", newFile);
|
|
|
|
|
_logger.TraceException(ex.Message, ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
episodeFile.Path = newFile;
|
|
|
|
|
_mediaFileService.Update(episodeFile);
|
|
|
|
|
|
2013-04-23 08:14:55 +02:00
|
|
|
|
var parsedEpisodeInfo = Parser.Parser.ParsePath(episodeFile.Path);
|
|
|
|
|
parsedEpisodeInfo.Quality = episodeFile.Quality;
|
2013-03-07 05:49:00 +01:00
|
|
|
|
|
|
|
|
|
if (newDownload)
|
|
|
|
|
{
|
2013-04-27 04:03:34 +02:00
|
|
|
|
_messageAggregator.PublishEvent(new EpisodeDownloadedEvent(parsedEpisodeInfo, series));
|
2013-03-07 05:49:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return episodeFile;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|