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
|
|
|
|
|
{
|
|
|
|
|
get { return "Post Download Media File Scan"; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2011-06-07 21:19:11 +00:00
|
|
|
|
Logger.Debug("Starting New Download Scan Job");
|
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-06-21 05:44:01 +00:00
|
|
|
|
Logger.Debug("Skipping drop folder scan. No drop folder is defined.");
|
2011-06-07 06:29:07 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!_diskProvider.FolderExists(dropFolder))
|
|
|
|
|
{
|
2011-06-07 21:19:11 +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-06-21 05:44:01 +00:00
|
|
|
|
var subfolderInfo = new DirectoryInfo(subfolder);
|
2011-06-07 06:29:07 +00:00
|
|
|
|
|
2011-06-21 05:44:01 +00:00
|
|
|
|
if (subfolderInfo.Name.StartsWith("_UNPACK_", StringComparison.CurrentCultureIgnoreCase))
|
2011-06-07 06:29:07 +00:00
|
|
|
|
{
|
2011-06-21 05:44:01 +00:00
|
|
|
|
Logger.Info("Folder [{0}] is still being unpacked. skipping.", subfolder);
|
2011-06-07 06:29:07 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-21 05:44:01 +00:00
|
|
|
|
if (subfolderInfo.Name.StartsWith("_FAILED_", StringComparison.CurrentCultureIgnoreCase))
|
2011-06-07 06:29:07 +00:00
|
|
|
|
{
|
2011-06-21 05:44:01 +00:00
|
|
|
|
Logger.Info("Folder [{0}] is marked as failed. skipping.", subfolder);
|
2011-06-07 06:29:07 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Parse the Folder name
|
2011-06-21 05:44:01 +00:00
|
|
|
|
var seriesName = Parser.ParseSeriesName(subfolderInfo.Name);
|
2011-06-07 06:29:07 +00:00
|
|
|
|
var series = _seriesProvider.FindSeries(seriesName);
|
|
|
|
|
|
|
|
|
|
if (series == null)
|
|
|
|
|
{
|
|
|
|
|
Logger.Warn("Unable to Import new download, series is not being watched");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-21 05:44:01 +00:00
|
|
|
|
var importedFiles = _diskScanProvider.Scan(series, subfolder);
|
2011-06-22 01:12:20 +00:00
|
|
|
|
importedFiles.ForEach(file => _diskScanProvider.MoveEpisodeFile(file));
|
2011-06-26 02:50:18 +00:00
|
|
|
|
|
|
|
|
|
//Delete the folder only if all files were removed
|
|
|
|
|
if (_diskProvider.GetFiles(subfolder, "*.*", SearchOption.AllDirectories).Length == 0)
|
|
|
|
|
_diskProvider.DeleteFolder(subfolder, false);
|
2011-06-07 06:29:07 +00:00
|
|
|
|
}
|
2011-06-21 05:44:01 +00:00
|
|
|
|
|
2011-06-07 21:19:11 +00:00
|
|
|
|
Logger.Debug("New Download Scan Job completed successfully");
|
2011-06-07 06:29:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|