1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-14 06:52:36 +01:00
Radarr/NzbDrone.Core/Providers/Jobs/PostDownloadScanJob.cs

111 lines
4.2 KiB
C#
Raw Normal View History

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