From 5041ff550c39f462d3e9b26b0842bb9cc42873bf Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Tue, 7 Jun 2011 14:19:11 -0700 Subject: [PATCH] Post Download Import Job created and in effect. Scans every minute to import files from the users configured "SabDropDirectory". --- NzbDrone.Core/CentralDispatch.cs | 1 + NzbDrone.Core/NzbDrone.Core.csproj | 2 +- NzbDrone.Core/Providers/Core/DiskProvider.cs | 5 +++ .../PostDownloadScanJob.cs} | 44 +++++++++++-------- NzbDrone.Core/Providers/MediaFileProvider.cs | 19 ++++---- 5 files changed, 44 insertions(+), 27 deletions(-) rename NzbDrone.Core/Providers/{PostProcessingProvider.cs => Jobs/PostDownloadScanJob.cs} (72%) diff --git a/NzbDrone.Core/CentralDispatch.cs b/NzbDrone.Core/CentralDispatch.cs index b545a19d1..50b96583c 100644 --- a/NzbDrone.Core/CentralDispatch.cs +++ b/NzbDrone.Core/CentralDispatch.cs @@ -121,6 +121,7 @@ private static void BindJobs() _kernel.Bind().To().InTransientScope(); _kernel.Bind().To().InTransientScope(); _kernel.Bind().To().InTransientScope(); + _kernel.Bind().To().InTransientScope(); _kernel.Get().Initialize(); _kernel.Get().StartTimer(30); diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 4a24a4a95..9532a4d4a 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -184,6 +184,7 @@ + @@ -195,7 +196,6 @@ - diff --git a/NzbDrone.Core/Providers/Core/DiskProvider.cs b/NzbDrone.Core/Providers/Core/DiskProvider.cs index 48123050d..436800c47 100644 --- a/NzbDrone.Core/Providers/Core/DiskProvider.cs +++ b/NzbDrone.Core/Providers/Core/DiskProvider.cs @@ -51,5 +51,10 @@ public virtual string GetExtension(string path) { return Path.GetExtension(path); } + + public virtual void DeleteFolder(string path, bool recursive) + { + Directory.Delete(path, recursive); + } } } \ No newline at end of file diff --git a/NzbDrone.Core/Providers/PostProcessingProvider.cs b/NzbDrone.Core/Providers/Jobs/PostDownloadScanJob.cs similarity index 72% rename from NzbDrone.Core/Providers/PostProcessingProvider.cs rename to NzbDrone.Core/Providers/Jobs/PostDownloadScanJob.cs index f644d4052..b744d8b10 100644 --- a/NzbDrone.Core/Providers/PostProcessingProvider.cs +++ b/NzbDrone.Core/Providers/Jobs/PostDownloadScanJob.cs @@ -4,39 +4,46 @@ using System.Linq; using System.Text; using NLog; +using NzbDrone.Core.Model.Notification; using NzbDrone.Core.Providers.Core; using NzbDrone.Core.Repository; -namespace NzbDrone.Core.Providers +namespace NzbDrone.Core.Providers.Jobs { - public class PostProcessingProvider + public class PostDownloadScanJob : IJob { - private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); - private readonly ConfigProvider _configProvider; private readonly DiskProvider _diskProvider; - private readonly SeriesProvider _seriesProvider; private readonly MediaFileProvider _mediaFileProvider; - private readonly EpisodeProvider _episodeProvider; + private readonly SeriesProvider _seriesProvider; + private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); - public PostProcessingProvider(ConfigProvider configProvider, DiskProvider diskProvider, - SeriesProvider seriesProvider, MediaFileProvider mediaFileProvider, - EpisodeProvider episodeProvider) + public PostDownloadScanJob(ConfigProvider configProvider, DiskProvider diskProvider, + MediaFileProvider mediaFileProvider, SeriesProvider seriesProvider) { _configProvider = configProvider; _diskProvider = diskProvider; - _seriesProvider = seriesProvider; _mediaFileProvider = mediaFileProvider; - _episodeProvider = episodeProvider; + _seriesProvider = seriesProvider; } - //Scan folder - //Delete Existing episode(s) - //Move file(s) - //Import file(s) - - public virtual void Scan() + public PostDownloadScanJob() { + } + + public string Name + { + get { return "Post Download Media File Scan"; } + } + + public int DefaultInterval + { + get { return 1; } + } + + public virtual void Start(ProgressNotification notification, int targetId) + { + Logger.Debug("Starting New Download Scan Job"); var dropFolder = _configProvider.SabDropDirectory; if (String.IsNullOrEmpty(dropFolder)) @@ -47,7 +54,7 @@ public virtual void Scan() if (!_diskProvider.FolderExists(dropFolder)) { - Logger.Warn("Unable to Scan for New Downloads - Folder Doesn't Exist"); + Logger.Warn("Unable to Scan for New Downloads - folder Doesn't exist: {0}", dropFolder); return; } @@ -81,6 +88,7 @@ public virtual void Scan() _mediaFileProvider.ImportNewFiles(subfolder, series); } + Logger.Debug("New Download Scan Job completed successfully"); } } } diff --git a/NzbDrone.Core/Providers/MediaFileProvider.cs b/NzbDrone.Core/Providers/MediaFileProvider.cs index 62acb5532..527e6b411 100644 --- a/NzbDrone.Core/Providers/MediaFileProvider.cs +++ b/NzbDrone.Core/Providers/MediaFileProvider.cs @@ -226,17 +226,12 @@ private List GetMediaFileList(string path) public virtual List ImportNewFiles(string path, Series series) { var result = new List(); - var files = GetMediaFileList(path); + + //Get all the files except those that are considered samples + var files = GetMediaFileList(path).Where(f => _diskProvider.GetSize(f) > 40000000 && !f.ToLower().Contains("sample")).ToList(); foreach (var file in files) { - //If Size is less than 40MB and contains sample. Check for Size to ensure its not an episode with sample in the title - if (_diskProvider.GetSize(file) < 40000000 && file.ToLower().Contains("sample")) - { - Logger.Trace("[{0}] appears to be a sample. Skipping.", file); - continue; - } - //Parse the filename var parseResult = Parser.ParseEpisodeInfo(Path.GetFileName(file)); parseResult.Series = series; @@ -284,6 +279,14 @@ public virtual List ImportNewFiles(string path, Series series) //Import into DB result.Add(ImportFile(series, folder + filename)); } + + //If we have imported all the non-sample files, delete the folder, requires a minimum of 1 file to be imported. + if (files.Count() > 0 && files.Count() == result.Count) + { + Logger.Debug("All non-sample files have been processed, deleting folder: {0}", path); + _diskProvider.DeleteFolder(path, true); + } + return result; }