2011-06-07 08:29:07 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2011-06-14 03:23:04 +02:00
|
|
|
|
using Ninject;
|
2011-06-07 08:29:07 +02:00
|
|
|
|
using NLog;
|
2011-06-07 23:19:11 +02:00
|
|
|
|
using NzbDrone.Core.Model.Notification;
|
2011-06-07 08:29:07 +02:00
|
|
|
|
using NzbDrone.Core.Providers.Core;
|
|
|
|
|
|
2011-06-07 23:19:11 +02:00
|
|
|
|
namespace NzbDrone.Core.Providers.Jobs
|
2011-06-07 08:29:07 +02:00
|
|
|
|
{
|
2011-06-07 23:19:11 +02:00
|
|
|
|
public class PostDownloadScanJob : IJob
|
2011-06-07 08:29:07 +02:00
|
|
|
|
{
|
|
|
|
|
private readonly ConfigProvider _configProvider;
|
|
|
|
|
private readonly DiskProvider _diskProvider;
|
2011-06-20 05:04:08 +02:00
|
|
|
|
private readonly DiskScanProvider _diskScanProvider;
|
2011-06-07 23:19:11 +02:00
|
|
|
|
private readonly SeriesProvider _seriesProvider;
|
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
2011-06-07 08:29:07 +02:00
|
|
|
|
|
2011-06-14 03:23:04 +02:00
|
|
|
|
[Inject]
|
2011-06-07 23:19:11 +02:00
|
|
|
|
public PostDownloadScanJob(ConfigProvider configProvider, DiskProvider diskProvider,
|
2011-06-20 05:04:08 +02:00
|
|
|
|
DiskScanProvider diskScanProvider, SeriesProvider seriesProvider)
|
2011-06-07 08:29:07 +02:00
|
|
|
|
{
|
|
|
|
|
_configProvider = configProvider;
|
|
|
|
|
_diskProvider = diskProvider;
|
2011-06-20 05:04:08 +02:00
|
|
|
|
_diskScanProvider = diskScanProvider;
|
2011-06-07 23:19:11 +02:00
|
|
|
|
_seriesProvider = seriesProvider;
|
2011-06-07 08:29:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-06-07 23:19:11 +02:00
|
|
|
|
public PostDownloadScanJob()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
2011-07-05 08:52:06 +02:00
|
|
|
|
get { return "Drop folder monitor"; }
|
2011-06-07 23:19:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int DefaultInterval
|
|
|
|
|
{
|
|
|
|
|
get { return 1; }
|
|
|
|
|
}
|
2011-06-07 08:29:07 +02:00
|
|
|
|
|
2011-08-22 02:48:37 +02:00
|
|
|
|
public virtual void Start(ProgressNotification notification, int targetId, int secondaryTargetId)
|
2011-06-07 08:29:07 +02:00
|
|
|
|
{
|
|
|
|
|
var dropFolder = _configProvider.SabDropDirectory;
|
|
|
|
|
|
2011-06-21 07:44:01 +02:00
|
|
|
|
if (String.IsNullOrWhiteSpace(dropFolder))
|
2011-06-07 08:29:07 +02:00
|
|
|
|
{
|
2011-07-06 08:17:21 +02:00
|
|
|
|
Logger.Debug("No drop folder is defined. Skipping.");
|
2011-06-07 08:29:07 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!_diskProvider.FolderExists(dropFolder))
|
|
|
|
|
{
|
2011-07-06 08:17:21 +02:00
|
|
|
|
Logger.Warn("Unable to Scan for New Downloads - folder Doesn't exist: [{0}]", dropFolder);
|
2011-06-07 08:29:07 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-21 07:44:01 +02:00
|
|
|
|
foreach (var subfolder in _diskProvider.GetDirectories(dropFolder))
|
2011-06-07 08:29:07 +02:00
|
|
|
|
{
|
2011-07-05 08:52:06 +02:00
|
|
|
|
try
|
2011-06-07 08:29:07 +02:00
|
|
|
|
{
|
2011-07-05 08:52:06 +02:00
|
|
|
|
var subfolderInfo = new DirectoryInfo(subfolder);
|
|
|
|
|
|
|
|
|
|
if (subfolderInfo.Name.StartsWith("_UNPACK_", StringComparison.CurrentCultureIgnoreCase))
|
|
|
|
|
{
|
2011-07-06 08:17:21 +02:00
|
|
|
|
Logger.Debug("Folder [{0}] is still being unpacked. skipping.", subfolder);
|
2011-07-05 08:52:06 +02:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (subfolderInfo.Name.StartsWith("_FAILED_", StringComparison.CurrentCultureIgnoreCase))
|
|
|
|
|
{
|
2011-07-06 08:17:21 +02:00
|
|
|
|
Logger.Debug("Folder [{0}] is marked as failed. skipping.", subfolder);
|
2011-07-05 08:52:06 +02:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-26 08:23:21 +02:00
|
|
|
|
if (subfolderInfo.Name.StartsWith("_NzbDrone_", StringComparison.CurrentCultureIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Folder [{0}] is marked as already processedby NzbDrone. skipping.", subfolder);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-05 08:52:06 +02:00
|
|
|
|
//Parse the Folder name
|
|
|
|
|
var seriesName = Parser.ParseSeriesName(subfolderInfo.Name);
|
|
|
|
|
var series = _seriesProvider.FindSeries(seriesName);
|
|
|
|
|
|
|
|
|
|
if (series == null)
|
|
|
|
|
{
|
2011-08-05 06:22:42 +02:00
|
|
|
|
Logger.Warn("Unable to Import new download [{0}], series doesn't exist in database.", subfolder);
|
2011-09-18 22:53:05 +02:00
|
|
|
|
|
|
|
|
|
//Rename the Directory so it's not processed again.
|
|
|
|
|
_diskProvider.MoveDirectory(subfolderInfo.FullName, Path.Combine(subfolderInfo.Parent.FullName, "_NzbDrone_" + subfolderInfo.Name));
|
2011-08-05 06:22:42 +02:00
|
|
|
|
continue;
|
2011-07-05 08:52:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var importedFiles = _diskScanProvider.Scan(series, subfolder);
|
2011-07-28 09:21:49 +02:00
|
|
|
|
importedFiles.ForEach(file => _diskScanProvider.MoveEpisodeFile(file, true));
|
2011-07-05 08:52:06 +02:00
|
|
|
|
|
2011-07-18 01:15:37 +02:00
|
|
|
|
//Delete the folder only if folder is small enough
|
|
|
|
|
if (_diskProvider.GetDirectorySize(subfolder) < 10.Megabytes())
|
2011-07-17 21:32:58 +02:00
|
|
|
|
_diskProvider.DeleteFolder(subfolder, true);
|
2011-08-26 08:23:21 +02:00
|
|
|
|
|
|
|
|
|
//Otherwise rename the folder to say it was already processed once by NzbDrone so it will not be continually processed
|
|
|
|
|
else
|
2011-08-26 19:45:59 +02:00
|
|
|
|
_diskProvider.MoveDirectory(subfolderInfo.FullName,Path.Combine(subfolderInfo.Parent.FullName, "_NzbDrone_" + subfolderInfo.Name));
|
2011-06-07 08:29:07 +02:00
|
|
|
|
}
|
2011-08-26 08:23:21 +02:00
|
|
|
|
|
2011-07-05 08:52:06 +02:00
|
|
|
|
catch (Exception e)
|
2011-06-07 08:29:07 +02:00
|
|
|
|
{
|
2011-07-05 08:52:06 +02:00
|
|
|
|
Logger.ErrorException("An error has occurred while importing " + subfolder, e);
|
2011-06-07 08:29:07 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-08-26 08:23:21 +02:00
|
|
|
|
}
|