Sonarr/src/NzbDrone.Core/MediaFiles/EpisodeFileMovingService.cs

159 lines
5.9 KiB
C#
Raw Normal View History

using System;
using System.IO;
using System.Linq;
using NLog;
using NzbDrone.Common;
using NzbDrone.Common.Disk;
2013-11-27 06:52:11 +00:00
using NzbDrone.Common.EnsureThat;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Organizer;
using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.MediaFiles
{
public interface IMoveEpisodeFiles
{
2013-08-30 06:39:41 +00:00
string MoveEpisodeFile(EpisodeFile episodeFile, Series series);
string MoveEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode);
}
public class EpisodeFileMovingService : IMoveEpisodeFiles
{
private readonly IEpisodeService _episodeService;
private readonly IBuildFileNames _buildFileNames;
2013-05-10 23:53:50 +00:00
private readonly IDiskProvider _diskProvider;
private readonly IConfigService _configService;
private readonly Logger _logger;
public EpisodeFileMovingService(IEpisodeService episodeService,
IBuildFileNames buildFileNames,
IDiskProvider diskProvider,
IConfigService configService,
Logger logger)
{
_episodeService = episodeService;
_buildFileNames = buildFileNames;
_diskProvider = diskProvider;
_configService = configService;
_logger = logger;
}
2013-08-30 06:39:41 +00:00
public string MoveEpisodeFile(EpisodeFile episodeFile, Series series)
2013-07-19 05:23:04 +00:00
{
var episodes = _episodeService.GetEpisodesByFileId(episodeFile.Id);
var newFileName = _buildFileNames.BuildFilename(episodes, series, episodeFile);
2013-08-30 06:39:41 +00:00
var filePath = _buildFileNames.BuildFilePath(series, episodes.First().SeasonNumber, newFileName, Path.GetExtension(episodeFile.Path));
_logger.Trace("Renaming episode file: {0} to {1}", episodeFile, filePath);
2013-09-28 22:25:22 +00:00
MoveFile(episodeFile, series, filePath);
2013-07-19 05:23:04 +00:00
2013-08-30 06:39:41 +00:00
return filePath;
2013-07-19 05:23:04 +00:00
}
2013-08-30 06:39:41 +00:00
public string MoveEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode)
{
var newFileName = _buildFileNames.BuildFilename(localEpisode.Episodes, localEpisode.Series, episodeFile);
2013-08-30 06:39:41 +00:00
var filePath = _buildFileNames.BuildFilePath(localEpisode.Series, localEpisode.SeasonNumber, newFileName, Path.GetExtension(episodeFile.Path));
_logger.Trace("Moving episode file: {0} to {1}", episodeFile, filePath);
2013-09-28 22:25:22 +00:00
MoveFile(episodeFile, localEpisode.Series, filePath);
2013-08-30 06:39:41 +00:00
return filePath;
}
2013-09-28 22:25:22 +00:00
private void MoveFile(EpisodeFile episodeFile, Series series, string destinationFilename)
{
2013-11-30 23:53:07 +00:00
Ensure.That(episodeFile, () => episodeFile).IsNotNull();
Ensure.That(series,() => series).IsNotNull();
Ensure.That(destinationFilename, () => destinationFilename).IsValidPath();
2013-11-27 06:52:11 +00:00
if (!_diskProvider.FileExists(episodeFile.Path))
{
2013-08-13 02:01:15 +00:00
throw new FileNotFoundException("Episode file path does not exist", episodeFile.Path);
}
if (episodeFile.Path.PathEquals(destinationFilename))
{
2013-08-13 02:01:15 +00:00
throw new SameFilenameException("File not moved, source and destination are the same", episodeFile.Path);
}
_diskProvider.CreateFolder(new FileInfo(destinationFilename).DirectoryName);
_logger.Debug("Moving [{0}] > [{1}]", episodeFile.Path, destinationFilename);
_diskProvider.MoveFile(episodeFile.Path, destinationFilename);
try
2013-09-28 22:25:22 +00:00
{
_logger.Trace("Setting last write time on series folder: {0}", series.Path);
_diskProvider.SetFolderWriteTime(series.Path, episodeFile.DateAdded);
SetFolderPermissions(series.Path);
if (series.SeasonFolder)
{
var seasonFolder = Path.GetDirectoryName(destinationFilename);
2013-09-28 22:25:22 +00:00
_logger.Trace("Setting last write time on season folder: {0}", seasonFolder);
_diskProvider.SetFolderWriteTime(seasonFolder, episodeFile.DateAdded);
SetFolderPermissions(seasonFolder);
}
}
catch (Exception ex)
{
_logger.WarnException("Unable to set last write time", ex);
2013-09-28 22:25:22 +00:00
}
//We should only run this on Windows
if (OsInfo.IsWindows)
{
//Wrapped in Try/Catch to prevent this from causing issues with remote NAS boxes, the move worked, which is more important.
try
{
_diskProvider.InheritFolderPermissions(destinationFilename);
}
catch (Exception ex)
{
if (ex is UnauthorizedAccessException || ex is InvalidOperationException)
{
_logger.Debug("Unable to apply folder permissions to: ", destinationFilename);
_logger.TraceException(ex.Message, ex);
}
else
{
throw;
}
}
}
else
{
SetPermissions(destinationFilename, _configService.FileChmod);
}
}
private void SetPermissions(string path, string permissions)
{
try
{
_diskProvider.SetPermissions(path, permissions);
}
catch (Exception ex)
{
if (ex is UnauthorizedAccessException || ex is InvalidOperationException)
{
_logger.Debug("Unable to apply permissions to: ", path);
_logger.TraceException(ex.Message, ex);
}
}
}
private void SetFolderPermissions(string path)
{
SetPermissions(path, _configService.FolderChmod);
}
}
}