2013-03-07 04:49:00 +00:00
|
|
|
|
using System;
|
2014-03-13 05:27:36 +00:00
|
|
|
|
using System.Collections.Generic;
|
2013-03-07 04:49:00 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Common;
|
2014-01-06 06:20:08 +00:00
|
|
|
|
using NzbDrone.Common.Disk;
|
2013-11-27 06:52:11 +00:00
|
|
|
|
using NzbDrone.Common.EnsureThat;
|
2013-10-17 00:20:28 +00:00
|
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
2014-01-14 19:21:20 +00:00
|
|
|
|
using NzbDrone.Core.Configuration;
|
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
|
|
|
|
|
{
|
2014-02-28 00:32:08 +00:00
|
|
|
|
EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile, Series series);
|
|
|
|
|
EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode);
|
2013-03-07 04:49:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-11-28 08:03:58 +00:00
|
|
|
|
public class EpisodeFileMovingService : IMoveEpisodeFiles
|
2013-03-07 04:49:00 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly IEpisodeService _episodeService;
|
2014-03-08 19:01:51 +00:00
|
|
|
|
private readonly IUpdateEpisodeFileService _updateEpisodeFileService;
|
2013-03-07 04:49:00 +00:00
|
|
|
|
private readonly IBuildFileNames _buildFileNames;
|
2013-05-10 23:53:50 +00:00
|
|
|
|
private readonly IDiskProvider _diskProvider;
|
2014-01-14 19:21:20 +00:00
|
|
|
|
private readonly IConfigService _configService;
|
2013-03-07 04:49:00 +00:00
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
2013-11-28 08:03:58 +00:00
|
|
|
|
public EpisodeFileMovingService(IEpisodeService episodeService,
|
2014-03-08 19:01:51 +00:00
|
|
|
|
IUpdateEpisodeFileService updateEpisodeFileService,
|
2013-07-16 02:56:46 +00:00
|
|
|
|
IBuildFileNames buildFileNames,
|
|
|
|
|
IDiskProvider diskProvider,
|
2014-01-14 19:21:20 +00:00
|
|
|
|
IConfigService configService,
|
2013-07-16 02:56:46 +00:00
|
|
|
|
Logger logger)
|
2013-03-07 04:49:00 +00:00
|
|
|
|
{
|
|
|
|
|
_episodeService = episodeService;
|
2014-03-08 19:01:51 +00:00
|
|
|
|
_updateEpisodeFileService = updateEpisodeFileService;
|
2013-03-07 04:49:00 +00:00
|
|
|
|
_buildFileNames = buildFileNames;
|
|
|
|
|
_diskProvider = diskProvider;
|
2014-01-14 19:21:20 +00:00
|
|
|
|
_configService = configService;
|
2013-03-07 04:49:00 +00:00
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-28 00:32:08 +00:00
|
|
|
|
public EpisodeFile 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));
|
2014-01-22 05:35:01 +00:00
|
|
|
|
|
|
|
|
|
_logger.Trace("Renaming episode file: {0} to {1}", episodeFile, filePath);
|
2014-02-28 00:42:04 +00:00
|
|
|
|
|
2014-03-13 05:27:36 +00:00
|
|
|
|
return MoveFile(episodeFile, series, episodes, filePath);
|
2013-07-19 05:23:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-28 00:32:08 +00:00
|
|
|
|
public EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode)
|
2013-07-06 21:47:49 +00:00
|
|
|
|
{
|
|
|
|
|
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));
|
2014-01-22 05:35:01 +00:00
|
|
|
|
|
|
|
|
|
_logger.Trace("Moving episode file: {0} to {1}", episodeFile, filePath);
|
2013-09-28 22:25:22 +00:00
|
|
|
|
|
2014-03-13 05:27:36 +00:00
|
|
|
|
return MoveFile(episodeFile, localEpisode.Series, localEpisode.Episodes, filePath);
|
2013-07-06 21:47:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-13 05:27:36 +00:00
|
|
|
|
private EpisodeFile MoveFile(EpisodeFile episodeFile, Series series, List<Episode> episodes, string destinationFilename)
|
2013-07-06 21:47:49 +00:00
|
|
|
|
{
|
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
|
|
|
|
|
2013-07-06 21:47:49 +00:00
|
|
|
|
if (!_diskProvider.FileExists(episodeFile.Path))
|
2013-03-07 04:49:00 +00:00
|
|
|
|
{
|
2013-08-13 02:01:15 +00:00
|
|
|
|
throw new FileNotFoundException("Episode file path does not exist", episodeFile.Path);
|
2013-03-07 04:49:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-20 19:28:46 +00:00
|
|
|
|
if (episodeFile.Path.PathEquals(destinationFilename))
|
2013-03-07 04:49:00 +00:00
|
|
|
|
{
|
2013-08-13 02:01:15 +00:00
|
|
|
|
throw new SameFilenameException("File not moved, source and destination are the same", episodeFile.Path);
|
2013-03-07 04:49:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-26 08:57:14 +00:00
|
|
|
|
var directoryName = new FileInfo(destinationFilename).DirectoryName;
|
|
|
|
|
|
2014-01-26 22:58:12 +00:00
|
|
|
|
if (!_diskProvider.FolderExists(directoryName))
|
2014-01-26 08:57:14 +00:00
|
|
|
|
{
|
2014-03-07 06:28:06 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_diskProvider.CreateFolder(directoryName);
|
|
|
|
|
}
|
|
|
|
|
catch (IOException ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Unable to create directory: " + directoryName, ex);
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-26 08:57:14 +00:00
|
|
|
|
SetFolderPermissions(directoryName);
|
|
|
|
|
|
|
|
|
|
if (!directoryName.PathEquals(series.Path))
|
|
|
|
|
{
|
|
|
|
|
SetFolderPermissions(series.Path);
|
|
|
|
|
}
|
|
|
|
|
}
|
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);
|
2014-02-28 00:32:08 +00:00
|
|
|
|
episodeFile.Path = destinationFilename;
|
2013-03-07 04:49:00 +00:00
|
|
|
|
|
2014-03-13 05:27:36 +00:00
|
|
|
|
_updateEpisodeFileService.ChangeFileDateForFile(episodeFile, series, episodes);
|
2014-03-08 19:01:51 +00:00
|
|
|
|
|
2013-10-20 18:17:56 +00:00
|
|
|
|
try
|
2013-09-28 22:25:22 +00:00
|
|
|
|
{
|
2013-10-20 18:17:56 +00:00
|
|
|
|
_logger.Trace("Setting last write time on series folder: {0}", series.Path);
|
2014-02-26 15:55:13 +00:00
|
|
|
|
_diskProvider.FolderSetLastWriteTimeUtc(series.Path, episodeFile.DateAdded);
|
2013-10-20 18:17:56 +00:00
|
|
|
|
|
|
|
|
|
if (series.SeasonFolder)
|
|
|
|
|
{
|
|
|
|
|
var seasonFolder = Path.GetDirectoryName(destinationFilename);
|
2013-09-28 22:25:22 +00:00
|
|
|
|
|
2013-10-20 18:17:56 +00:00
|
|
|
|
_logger.Trace("Setting last write time on season folder: {0}", seasonFolder);
|
2014-02-26 15:55:13 +00:00
|
|
|
|
_diskProvider.FolderSetLastWriteTimeUtc(seasonFolder, episodeFile.DateAdded);
|
2013-10-20 18:17:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.WarnException("Unable to set last write time", ex);
|
2013-09-28 22:25:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-17 00:20:28 +00:00
|
|
|
|
//We should only run this on Windows
|
|
|
|
|
if (OsInfo.IsWindows)
|
2013-03-07 04:49:00 +00:00
|
|
|
|
{
|
2013-10-17 00:20:28 +00:00
|
|
|
|
//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);
|
|
|
|
|
}
|
2013-11-28 08:03:58 +00:00
|
|
|
|
catch (Exception ex)
|
2013-10-17 00:20:28 +00:00
|
|
|
|
{
|
2013-11-28 08:03:58 +00:00
|
|
|
|
if (ex is UnauthorizedAccessException || ex is InvalidOperationException)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Unable to apply folder permissions to: ", destinationFilename);
|
|
|
|
|
_logger.TraceException(ex.Message, ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2013-10-17 00:20:28 +00:00
|
|
|
|
}
|
2013-03-07 04:49:00 +00:00
|
|
|
|
}
|
2014-01-14 19:21:20 +00:00
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
SetPermissions(destinationFilename, _configService.FileChmod);
|
|
|
|
|
}
|
2014-02-28 00:32:08 +00:00
|
|
|
|
|
|
|
|
|
return episodeFile;
|
2014-01-14 19:21:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetPermissions(string path, string permissions)
|
|
|
|
|
{
|
2014-01-26 08:57:14 +00:00
|
|
|
|
if (!_configService.SetPermissionsLinux)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-14 19:21:20 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2014-01-31 05:24:32 +00:00
|
|
|
|
_diskProvider.SetPermissions(path, permissions, _configService.ChownUser, _configService.ChownGroup);
|
2014-01-14 19:21:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
if (ex is UnauthorizedAccessException || ex is InvalidOperationException)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Unable to apply permissions to: ", path);
|
|
|
|
|
_logger.TraceException(ex.Message, ex);
|
|
|
|
|
}
|
2014-01-31 05:24:32 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2014-01-14 19:21:20 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetFolderPermissions(string path)
|
|
|
|
|
{
|
|
|
|
|
SetPermissions(path, _configService.FolderChmod);
|
2013-03-07 04:49:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|