1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-23 02:05:27 +02:00
Sonarr/src/NzbDrone.Core/MediaFiles/EpisodeFileMovingService.cs

214 lines
8.1 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NLog;
using NzbDrone.Common;
using NzbDrone.Common.Disk;
2013-11-27 09:52:11 +03: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
{
EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile, Series series);
EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode);
EpisodeFile CopyEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode);
}
public class EpisodeFileMovingService : IMoveEpisodeFiles
{
private readonly IEpisodeService _episodeService;
private readonly IUpdateEpisodeFileService _updateEpisodeFileService;
private readonly IBuildFileNames _buildFileNames;
2013-05-11 02:53:50 +03:00
private readonly IDiskProvider _diskProvider;
private readonly IConfigService _configService;
private readonly Logger _logger;
public EpisodeFileMovingService(IEpisodeService episodeService,
IUpdateEpisodeFileService updateEpisodeFileService,
IBuildFileNames buildFileNames,
IDiskProvider diskProvider,
IConfigService configService,
Logger logger)
{
_episodeService = episodeService;
_updateEpisodeFileService = updateEpisodeFileService;
_buildFileNames = buildFileNames;
_diskProvider = diskProvider;
_configService = configService;
_logger = logger;
}
public EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile, Series series)
2013-07-19 08:23:04 +03:00
{
var episodes = _episodeService.GetEpisodesByFileId(episodeFile.Id);
var newFileName = _buildFileNames.BuildFileName(episodes, series, episodeFile);
2014-07-24 02:43:54 +03:00
var filePath = _buildFileNames.BuildFilePath(series, episodes.First().SeasonNumber, newFileName, Path.GetExtension(episodeFile.RelativePath));
2014-03-13 23:12:42 +03:00
_logger.Debug("Renaming episode file: {0} to {1}", episodeFile, filePath);
2014-02-28 03:42:04 +03:00
return TransferFile(episodeFile, series, episodes, filePath, false);
2013-07-19 08:23:04 +03:00
}
public EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode)
{
var newFileName = _buildFileNames.BuildFileName(localEpisode.Episodes, localEpisode.Series, episodeFile);
2014-07-24 02:43:54 +03:00
var filePath = _buildFileNames.BuildFilePath(localEpisode.Series, localEpisode.SeasonNumber, newFileName, Path.GetExtension(localEpisode.Path));
2014-03-13 23:12:42 +03:00
_logger.Debug("Moving episode file: {0} to {1}", episodeFile, filePath);
2013-09-29 01:25:22 +03:00
return TransferFile(episodeFile, localEpisode.Series, localEpisode.Episodes, filePath, false);
}
public EpisodeFile CopyEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode)
{
var newFileName = _buildFileNames.BuildFileName(localEpisode.Episodes, localEpisode.Series, episodeFile);
2014-07-24 02:43:54 +03:00
var filePath = _buildFileNames.BuildFilePath(localEpisode.Series, localEpisode.SeasonNumber, newFileName, Path.GetExtension(localEpisode.Path));
_logger.Debug("Copying episode file: {0} to {1}", episodeFile, filePath);
return TransferFile(episodeFile, localEpisode.Series, localEpisode.Episodes, filePath, true);
}
2014-07-24 02:43:54 +03:00
private EpisodeFile TransferFile(EpisodeFile episodeFile, Series series, List<Episode> episodes, String destinationFilename, Boolean copyOnly)
{
2013-12-01 02:53:07 +03:00
Ensure.That(episodeFile, () => episodeFile).IsNotNull();
Ensure.That(series,() => series).IsNotNull();
Ensure.That(destinationFilename, () => destinationFilename).IsValidPath();
2013-11-27 09:52:11 +03:00
2014-07-24 02:43:54 +03:00
var episodeFilePath = episodeFile.Path ?? Path.Combine(series.Path, episodeFile.RelativePath);
if (!_diskProvider.FileExists(episodeFilePath))
{
2014-07-24 02:43:54 +03:00
throw new FileNotFoundException("Episode file path does not exist", episodeFilePath);
}
2014-07-24 02:43:54 +03:00
if (episodeFilePath.PathEquals(destinationFilename))
{
2014-07-24 02:43:54 +03:00
throw new SameFilenameException("File not moved, source and destination are the same", episodeFilePath);
}
2014-01-26 11:57:14 +03:00
var directoryName = new FileInfo(destinationFilename).DirectoryName;
2014-01-27 01:58:12 +03:00
if (!_diskProvider.FolderExists(directoryName))
2014-01-26 11:57:14 +03:00
{
2014-03-07 09:28:06 +03:00
try
{
_diskProvider.CreateFolder(directoryName);
}
catch (IOException ex)
{
_logger.ErrorException("Unable to create directory: " + directoryName, ex);
}
2014-01-26 11:57:14 +03:00
SetFolderPermissions(directoryName);
if (!directoryName.PathEquals(series.Path))
{
SetFolderPermissions(series.Path);
}
}
if (copyOnly)
{
2014-07-24 02:43:54 +03:00
_logger.Debug("Copying [{0}] > [{1}]", episodeFilePath, destinationFilename);
_diskProvider.CopyFile(episodeFilePath, destinationFilename);
}
else
{
2014-07-24 02:43:54 +03:00
_logger.Debug("Moving [{0}] > [{1}]", episodeFilePath, destinationFilename);
_diskProvider.MoveFile(episodeFilePath, destinationFilename);
}
2014-07-24 02:43:54 +03:00
episodeFile.RelativePath = series.Path.GetRelativePath(destinationFilename);
_updateEpisodeFileService.ChangeFileDateForFile(episodeFile, series, episodes);
try
2013-09-29 01:25:22 +03:00
{
2014-08-01 10:17:20 +03:00
SetFolderLastWriteTime(series.Path, episodeFile.DateAdded);
if (series.SeasonFolder)
{
var seasonFolder = Path.GetDirectoryName(destinationFilename);
2013-09-29 01:25:22 +03:00
2014-08-01 10:17:20 +03:00
SetFolderLastWriteTime(seasonFolder, episodeFile.DateAdded);
}
}
catch (Exception ex)
{
_logger.WarnException("Unable to set last write time", ex);
2013-09-29 01:25:22 +03: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);
2014-03-13 23:12:42 +03:00
_logger.DebugException(ex.Message, ex);
}
else
{
throw;
}
}
}
else
{
SetPermissions(destinationFilename, _configService.FileChmod);
}
return episodeFile;
}
private void SetPermissions(string path, string permissions)
{
2014-01-26 11:57:14 +03:00
if (!_configService.SetPermissionsLinux)
{
return;
}
try
{
_diskProvider.SetPermissions(path, permissions, _configService.ChownUser, _configService.ChownGroup);
}
catch (Exception ex)
{
_logger.WarnException("Unable to apply permissions to: " + path, ex);
_logger.DebugException(ex.Message, ex);
}
}
private void SetFolderPermissions(string path)
{
SetPermissions(path, _configService.FolderChmod);
}
2014-08-01 10:17:20 +03:00
private void SetFolderLastWriteTime(String path, DateTime time)
{
if (OsInfo.IsMono) return;
_logger.Debug("Setting last write time on series folder: {0}", path);
_diskProvider.FolderSetLastWriteTimeUtc(path, time);
}
}
}