2011-10-13 01:47:28 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2012-08-09 17:42:38 +02:00
|
|
|
|
using System.Linq;
|
2011-10-23 01:28:57 +02:00
|
|
|
|
using System.Text.RegularExpressions;
|
2011-10-13 01:47:28 +02:00
|
|
|
|
using NLog;
|
2011-11-13 05:07:06 +01:00
|
|
|
|
using NzbDrone.Common;
|
2013-02-19 07:01:03 +01:00
|
|
|
|
using NzbDrone.Core.Tv;
|
2011-10-13 01:47:28 +02:00
|
|
|
|
using NzbDrone.Core.Model;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
|
{
|
|
|
|
|
public class PostDownloadProvider
|
|
|
|
|
{
|
2011-10-23 04:34:31 +02:00
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
2011-10-13 01:47:28 +02:00
|
|
|
|
|
2011-10-23 04:34:31 +02:00
|
|
|
|
private static readonly Regex StatusRegex = new Regex(@"^_[\w_]*_", RegexOptions.Compiled);
|
2011-10-13 01:47:28 +02:00
|
|
|
|
private readonly DiskProvider _diskProvider;
|
|
|
|
|
private readonly DiskScanProvider _diskScanProvider;
|
|
|
|
|
private readonly SeriesProvider _seriesProvider;
|
2012-07-12 23:08:21 +02:00
|
|
|
|
private readonly MetadataProvider _metadataProvider;
|
2011-10-23 01:28:57 +02:00
|
|
|
|
|
2011-10-23 04:34:31 +02:00
|
|
|
|
public PostDownloadProvider(DiskProvider diskProvider, DiskScanProvider diskScanProvider,
|
2012-07-12 23:08:21 +02:00
|
|
|
|
SeriesProvider seriesProvider, MetadataProvider metadataProvider)
|
2011-10-13 01:47:28 +02:00
|
|
|
|
{
|
|
|
|
|
_diskProvider = diskProvider;
|
|
|
|
|
_diskScanProvider = diskScanProvider;
|
|
|
|
|
_seriesProvider = seriesProvider;
|
2012-07-12 23:08:21 +02:00
|
|
|
|
_metadataProvider = metadataProvider;
|
2011-10-13 01:47:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-17 22:05:38 +02:00
|
|
|
|
public PostDownloadProvider()
|
2011-10-13 01:47:28 +02:00
|
|
|
|
{
|
2011-10-14 17:37:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void ProcessDropFolder(string dropFolder)
|
|
|
|
|
{
|
2011-10-13 01:47:28 +02:00
|
|
|
|
foreach (var subfolder in _diskProvider.GetDirectories(dropFolder))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2011-12-10 19:54:16 +01:00
|
|
|
|
if (!_seriesProvider.SeriesPathExists(subfolder))
|
|
|
|
|
{
|
|
|
|
|
ProcessDownload(new DirectoryInfo(subfolder));
|
|
|
|
|
}
|
2011-10-13 01:47:28 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2012-09-24 00:04:53 +02:00
|
|
|
|
Logger.ErrorException("An error has occurred while importing folder: " + subfolder, e);
|
2011-10-13 01:47:28 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-08-27 12:15:22 +02:00
|
|
|
|
|
|
|
|
|
foreach(var videoFile in _diskScanProvider.GetVideoFiles(dropFolder, false))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
ProcessVideoFile(videoFile);
|
|
|
|
|
}
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.ErrorException("An error has occurred while importing video file" + videoFile, ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-10-13 01:47:28 +02:00
|
|
|
|
}
|
2011-10-14 17:37:19 +02:00
|
|
|
|
|
|
|
|
|
public virtual void ProcessDownload(DirectoryInfo subfolderInfo)
|
|
|
|
|
{
|
2012-01-23 05:59:23 +01:00
|
|
|
|
if (subfolderInfo.Name.StartsWith("_") && _diskProvider.GetLastDirectoryWrite(subfolderInfo.FullName).AddMinutes(2) > DateTime.UtcNow)
|
2011-10-23 04:31:28 +02:00
|
|
|
|
{
|
|
|
|
|
Logger.Trace("[{0}] is too fresh. skipping", subfolderInfo.Name);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-20 10:01:47 +02:00
|
|
|
|
if (_diskProvider.IsFolderLocked(subfolderInfo.FullName))
|
|
|
|
|
{
|
|
|
|
|
Logger.Trace("[{0}] is currently locked by another process, skipping", subfolderInfo.Name);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-23 04:34:31 +02:00
|
|
|
|
string seriesName = Parser.ParseSeriesName(RemoveStatusFromFolderName(subfolderInfo.Name));
|
2011-10-14 17:37:19 +02:00
|
|
|
|
var series = _seriesProvider.FindSeries(seriesName);
|
|
|
|
|
|
|
|
|
|
if (series == null)
|
|
|
|
|
{
|
2012-02-11 07:33:27 +01:00
|
|
|
|
Logger.Trace("Unknown Series on Import: {0}", subfolderInfo.Name);
|
2011-10-23 04:31:28 +02:00
|
|
|
|
TagFolder(subfolderInfo, PostDownloadStatusType.UnknownSeries);
|
2011-10-14 17:37:19 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-20 08:46:12 +02:00
|
|
|
|
if (!_diskProvider.FolderExists(series.Path))
|
|
|
|
|
{
|
|
|
|
|
Logger.Warn("Series Folder doesn't exist: {0}", series.Path);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-09 03:17:44 +02:00
|
|
|
|
var size = _diskProvider.GetDirectorySize(subfolderInfo.FullName);
|
2013-02-17 05:33:56 +01:00
|
|
|
|
var freeSpace = _diskProvider.FreeDiskSpace(series.Path);
|
2012-08-09 03:17:44 +02:00
|
|
|
|
|
|
|
|
|
if (Convert.ToUInt64(size) > freeSpace)
|
|
|
|
|
{
|
|
|
|
|
Logger.Error("Not enough free disk space for series: {0}, {1}", series.Title, series.Path);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-17 23:44:20 +02:00
|
|
|
|
_diskScanProvider.CleanUpDropFolder(subfolderInfo.FullName);
|
|
|
|
|
|
2011-10-14 17:37:19 +02:00
|
|
|
|
var importedFiles = _diskScanProvider.Scan(series, subfolderInfo.FullName);
|
|
|
|
|
importedFiles.ForEach(file => _diskScanProvider.MoveEpisodeFile(file, true));
|
|
|
|
|
|
2012-07-12 23:08:21 +02:00
|
|
|
|
//Create Metadata for all the episode files found
|
2012-08-09 17:42:38 +02:00
|
|
|
|
if (importedFiles.Any())
|
|
|
|
|
_metadataProvider.CreateForEpisodeFiles(importedFiles);
|
2012-07-12 23:08:21 +02:00
|
|
|
|
|
2011-10-14 17:37:19 +02:00
|
|
|
|
//Delete the folder only if folder is small enough
|
2011-10-25 07:15:20 +02:00
|
|
|
|
if (_diskProvider.GetDirectorySize(subfolderInfo.FullName) < Constants.IgnoreFileSize)
|
2011-10-23 04:31:28 +02:00
|
|
|
|
{
|
2012-02-11 07:33:27 +01:00
|
|
|
|
Logger.Trace("Episode(s) imported, deleting folder: {0}", subfolderInfo.Name);
|
2011-10-14 17:37:19 +02:00
|
|
|
|
_diskProvider.DeleteFolder(subfolderInfo.FullName, true);
|
2011-10-23 04:31:28 +02:00
|
|
|
|
}
|
2011-10-14 17:37:19 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (importedFiles.Count == 0)
|
|
|
|
|
{
|
2012-02-11 07:33:27 +01:00
|
|
|
|
Logger.Trace("No Imported files: {0}", subfolderInfo.Name);
|
2011-10-23 04:31:28 +02:00
|
|
|
|
TagFolder(subfolderInfo, PostDownloadStatusType.ParseError);
|
2011-10-14 17:37:19 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2011-10-23 04:34:31 +02:00
|
|
|
|
//Unknown Error Importing (Possibly a lesser quality than episode currently on disk)
|
2012-02-11 07:33:27 +01:00
|
|
|
|
Logger.Trace("Unable to import series (Unknown): {0}", subfolderInfo.Name);
|
2011-10-23 04:31:28 +02:00
|
|
|
|
TagFolder(subfolderInfo, PostDownloadStatusType.Unknown);
|
2011-10-17 22:05:38 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-10-18 05:08:26 +02:00
|
|
|
|
|
2012-08-27 12:15:22 +02:00
|
|
|
|
public virtual void ProcessVideoFile(string videoFile)
|
|
|
|
|
{
|
2012-08-29 17:34:51 +02:00
|
|
|
|
if (_diskProvider.GetLastFileWrite(videoFile).AddMinutes(2) > DateTime.UtcNow)
|
|
|
|
|
{
|
|
|
|
|
Logger.Trace("[{0}] is too fresh. skipping", videoFile);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-20 10:01:47 +02:00
|
|
|
|
if (_diskProvider.IsFileLocked(new FileInfo(videoFile)))
|
|
|
|
|
{
|
|
|
|
|
Logger.Trace("[{0}] is currently locked by another process, skipping", videoFile);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-27 12:15:22 +02:00
|
|
|
|
var seriesName = Parser.ParseSeriesName(Path.GetFileNameWithoutExtension(videoFile));
|
|
|
|
|
var series = _seriesProvider.FindSeries(seriesName);
|
|
|
|
|
|
|
|
|
|
if (series == null)
|
|
|
|
|
{
|
|
|
|
|
Logger.Trace("Unknown Series on Import: {0}", videoFile);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2012-10-20 08:46:12 +02:00
|
|
|
|
|
|
|
|
|
if (!_diskProvider.FolderExists(series.Path))
|
|
|
|
|
{
|
|
|
|
|
Logger.Warn("Series Folder doesn't exist: {0}", series.Path);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2012-08-27 12:15:22 +02:00
|
|
|
|
|
2012-10-20 03:42:42 +02:00
|
|
|
|
var size = _diskProvider.GetSize(videoFile);
|
2013-02-17 05:33:56 +01:00
|
|
|
|
var freeSpace = _diskProvider.FreeDiskSpace(series.Path);
|
2012-08-29 17:34:51 +02:00
|
|
|
|
|
|
|
|
|
if (Convert.ToUInt64(size) > freeSpace)
|
|
|
|
|
{
|
|
|
|
|
Logger.Error("Not enough free disk space for series: {0}, {1}", series.Title, series.Path);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-27 12:15:22 +02:00
|
|
|
|
var episodeFile = _diskScanProvider.ImportFile(series, videoFile);
|
|
|
|
|
if (episodeFile != null)
|
|
|
|
|
{
|
|
|
|
|
_diskScanProvider.MoveEpisodeFile(episodeFile, true);
|
|
|
|
|
_metadataProvider.CreateForEpisodeFile(episodeFile);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-25 06:34:48 +02:00
|
|
|
|
private void TagFolder(DirectoryInfo directory, PostDownloadStatusType status)
|
2011-10-18 05:08:26 +02:00
|
|
|
|
{
|
2012-01-15 04:17:23 +01:00
|
|
|
|
//Turning off tagging folder for now, to stop messing people's series folders.
|
|
|
|
|
return;
|
2011-10-25 06:34:48 +02:00
|
|
|
|
var target = GetTaggedFolderName(directory, status);
|
|
|
|
|
|
2011-12-10 20:22:47 +01:00
|
|
|
|
if (!DiskProvider.PathEquals(target, directory.FullName))
|
2011-10-25 06:34:48 +02:00
|
|
|
|
{
|
2011-11-26 07:38:22 +01:00
|
|
|
|
Logger.Warn("Unable to download [{0}]. Status: {1}",directory.Name, status);
|
2011-10-25 06:34:48 +02:00
|
|
|
|
_diskProvider.MoveDirectory(directory.FullName, target);
|
|
|
|
|
}
|
2011-11-26 07:38:22 +01:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Unable to download [{0}], {1}", directory.Name, status);
|
|
|
|
|
}
|
2011-10-18 05:08:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-25 06:34:48 +02:00
|
|
|
|
public static string GetTaggedFolderName(DirectoryInfo directoryInfo, PostDownloadStatusType status)
|
2011-10-18 05:08:26 +02:00
|
|
|
|
{
|
2011-10-23 04:31:28 +02:00
|
|
|
|
if (status == PostDownloadStatusType.NoError)
|
|
|
|
|
throw new InvalidOperationException("Can't tag a folder with a None-error status. " + status);
|
2011-10-18 05:08:26 +02:00
|
|
|
|
|
2011-10-23 04:34:31 +02:00
|
|
|
|
string cleanName = RemoveStatusFromFolderName(directoryInfo.Name);
|
|
|
|
|
string newName = string.Format("_{0}_{1}", status, cleanName);
|
2011-10-18 05:08:26 +02:00
|
|
|
|
|
2011-10-23 04:31:28 +02:00
|
|
|
|
return Path.Combine(directoryInfo.Parent.FullName, newName);
|
2011-10-18 05:08:26 +02:00
|
|
|
|
}
|
2011-10-23 01:28:57 +02:00
|
|
|
|
|
|
|
|
|
public static string RemoveStatusFromFolderName(string folderName)
|
|
|
|
|
{
|
|
|
|
|
return StatusRegex.Replace(folderName, string.Empty);
|
|
|
|
|
}
|
2011-10-13 01:47:28 +02:00
|
|
|
|
}
|
2011-10-23 04:34:31 +02:00
|
|
|
|
}
|