2011-10-13 02:47:28 +03:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
2011-10-23 02:28:57 +03:00
|
|
|
using System.Text.RegularExpressions;
|
2011-10-13 02:47:28 +03:00
|
|
|
using NLog;
|
|
|
|
using Ninject;
|
2011-11-13 07:07:06 +03:00
|
|
|
using NzbDrone.Common;
|
2011-10-13 02:47:28 +03:00
|
|
|
using NzbDrone.Core.Model;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
{
|
|
|
|
public class PostDownloadProvider
|
|
|
|
{
|
2011-10-23 05:34:31 +03:00
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
2011-10-13 02:47:28 +03:00
|
|
|
|
2011-10-23 05:34:31 +03:00
|
|
|
private static readonly Regex StatusRegex = new Regex(@"^_[\w_]*_", RegexOptions.Compiled);
|
2011-10-13 02:47:28 +03:00
|
|
|
private readonly DiskProvider _diskProvider;
|
|
|
|
private readonly DiskScanProvider _diskScanProvider;
|
|
|
|
private readonly SeriesProvider _seriesProvider;
|
2011-10-23 02:28:57 +03:00
|
|
|
|
2011-10-13 02:47:28 +03:00
|
|
|
[Inject]
|
2011-10-23 05:34:31 +03:00
|
|
|
public PostDownloadProvider(DiskProvider diskProvider, DiskScanProvider diskScanProvider,
|
|
|
|
SeriesProvider seriesProvider)
|
2011-10-13 02:47:28 +03:00
|
|
|
{
|
|
|
|
_diskProvider = diskProvider;
|
|
|
|
_diskScanProvider = diskScanProvider;
|
|
|
|
_seriesProvider = seriesProvider;
|
|
|
|
}
|
|
|
|
|
2011-10-17 23:05:38 +03:00
|
|
|
public PostDownloadProvider()
|
2011-10-13 02:47:28 +03:00
|
|
|
{
|
2011-10-14 18:37:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void ProcessDropFolder(string dropFolder)
|
|
|
|
{
|
2011-10-13 02:47:28 +03:00
|
|
|
foreach (var subfolder in _diskProvider.GetDirectories(dropFolder))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2011-12-10 21:54:16 +03:00
|
|
|
if (!_seriesProvider.SeriesPathExists(subfolder))
|
|
|
|
{
|
|
|
|
ProcessDownload(new DirectoryInfo(subfolder));
|
|
|
|
}
|
2011-10-13 02:47:28 +03:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2011-10-23 05:31:28 +03:00
|
|
|
Logger.ErrorException("An error has occurred while importing folder" + subfolder, e);
|
2011-10-13 02:47:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-10-14 18:37:19 +03:00
|
|
|
|
|
|
|
public virtual void ProcessDownload(DirectoryInfo subfolderInfo)
|
|
|
|
{
|
2011-10-23 05:31:28 +03:00
|
|
|
if (subfolderInfo.Name.StartsWith("_") && subfolderInfo.LastWriteTimeUtc.AddMinutes(1) > DateTime.UtcNow)
|
|
|
|
{
|
|
|
|
Logger.Trace("[{0}] is too fresh. skipping", subfolderInfo.Name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-23 05:34:31 +03:00
|
|
|
string seriesName = Parser.ParseSeriesName(RemoveStatusFromFolderName(subfolderInfo.Name));
|
2011-10-14 18:37:19 +03:00
|
|
|
var series = _seriesProvider.FindSeries(seriesName);
|
|
|
|
|
|
|
|
if (series == null)
|
|
|
|
{
|
2011-10-23 05:31:28 +03:00
|
|
|
TagFolder(subfolderInfo, PostDownloadStatusType.UnknownSeries);
|
2011-10-14 18:37:19 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var importedFiles = _diskScanProvider.Scan(series, subfolderInfo.FullName);
|
|
|
|
importedFiles.ForEach(file => _diskScanProvider.MoveEpisodeFile(file, true));
|
|
|
|
|
|
|
|
//Delete the folder only if folder is small enough
|
2011-10-25 08:15:20 +03:00
|
|
|
if (_diskProvider.GetDirectorySize(subfolderInfo.FullName) < Constants.IgnoreFileSize)
|
2011-10-23 05:31:28 +03:00
|
|
|
{
|
2011-10-14 18:37:19 +03:00
|
|
|
_diskProvider.DeleteFolder(subfolderInfo.FullName, true);
|
2011-10-23 05:31:28 +03:00
|
|
|
}
|
2011-10-14 18:37:19 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (importedFiles.Count == 0)
|
|
|
|
{
|
2011-10-23 05:31:28 +03:00
|
|
|
TagFolder(subfolderInfo, PostDownloadStatusType.ParseError);
|
2011-10-14 18:37:19 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-10-23 05:34:31 +03:00
|
|
|
//Unknown Error Importing (Possibly a lesser quality than episode currently on disk)
|
2011-10-23 05:31:28 +03:00
|
|
|
TagFolder(subfolderInfo, PostDownloadStatusType.Unknown);
|
2011-10-17 23:05:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-10-18 06:08:26 +03:00
|
|
|
|
2011-10-25 07:34:48 +03:00
|
|
|
private void TagFolder(DirectoryInfo directory, PostDownloadStatusType status)
|
2011-10-18 06:08:26 +03:00
|
|
|
{
|
2012-01-15 06:17:23 +03:00
|
|
|
//Turning off tagging folder for now, to stop messing people's series folders.
|
|
|
|
return;
|
2011-10-25 07:34:48 +03:00
|
|
|
var target = GetTaggedFolderName(directory, status);
|
|
|
|
|
2011-12-10 22:22:47 +03:00
|
|
|
if (!DiskProvider.PathEquals(target, directory.FullName))
|
2011-10-25 07:34:48 +03:00
|
|
|
{
|
2011-11-26 09:38:22 +03:00
|
|
|
Logger.Warn("Unable to download [{0}]. Status: {1}",directory.Name, status);
|
2011-10-25 07:34:48 +03:00
|
|
|
_diskProvider.MoveDirectory(directory.FullName, target);
|
|
|
|
}
|
2011-11-26 09:38:22 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Logger.Debug("Unable to download [{0}], {1}", directory.Name, status);
|
|
|
|
}
|
2011-10-18 06:08:26 +03:00
|
|
|
}
|
|
|
|
|
2011-10-25 07:34:48 +03:00
|
|
|
public static string GetTaggedFolderName(DirectoryInfo directoryInfo, PostDownloadStatusType status)
|
2011-10-18 06:08:26 +03:00
|
|
|
{
|
2011-10-23 05:31:28 +03:00
|
|
|
if (status == PostDownloadStatusType.NoError)
|
|
|
|
throw new InvalidOperationException("Can't tag a folder with a None-error status. " + status);
|
2011-10-18 06:08:26 +03:00
|
|
|
|
2011-10-23 05:34:31 +03:00
|
|
|
string cleanName = RemoveStatusFromFolderName(directoryInfo.Name);
|
|
|
|
string newName = string.Format("_{0}_{1}", status, cleanName);
|
2011-10-18 06:08:26 +03:00
|
|
|
|
2011-10-23 05:31:28 +03:00
|
|
|
return Path.Combine(directoryInfo.Parent.FullName, newName);
|
2011-10-18 06:08:26 +03:00
|
|
|
}
|
2011-10-23 02:28:57 +03:00
|
|
|
|
|
|
|
public static string RemoveStatusFromFolderName(string folderName)
|
|
|
|
{
|
|
|
|
return StatusRegex.Replace(folderName, string.Empty);
|
|
|
|
}
|
2011-10-13 02:47:28 +03:00
|
|
|
}
|
2011-10-23 05:34:31 +03:00
|
|
|
}
|