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