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