Fixed: Ignore '.unwanted' directory when importing.

fixes #2072
This commit is contained in:
Taloth Saldono 2017-07-27 15:27:54 +02:00
parent 90a42bcbad
commit 6fc4ea614d
10 changed files with 45 additions and 30 deletions

View File

@ -1,15 +1,17 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading;
using FluentAssertions; using FluentAssertions;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Common.Disk; using NzbDrone.Common.Disk;
using NzbDrone.Core.Download; using NzbDrone.Core.Download;
using NzbDrone.Test.Common;
using System.Threading;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Download.Clients.Blackhole; using NzbDrone.Core.Download.Clients.Blackhole;
using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Test.Common;
namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole
{ {
@ -18,7 +20,7 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole
{ {
protected readonly string _title = "Droned.S01E01.Pilot.1080p.WEB-DL-DRONE"; protected readonly string _title = "Droned.S01E01.Pilot.1080p.WEB-DL-DRONE";
protected string _completedDownloadFolder = @"c:\blackhole\completed".AsOsAgnostic(); protected string _completedDownloadFolder = @"c:\blackhole\completed".AsOsAgnostic();
protected void GivenCompletedItem() protected void GivenCompletedItem()
{ {
var targetDir = Path.Combine(_completedDownloadFolder, _title); var targetDir = Path.Combine(_completedDownloadFolder, _title);
@ -33,6 +35,9 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole
Mocker.GetMock<IDiskProvider>() Mocker.GetMock<IDiskProvider>()
.Setup(c => c.GetFileSize(It.IsAny<string>())) .Setup(c => c.GetFileSize(It.IsAny<string>()))
.Returns(1000000); .Returns(1000000);
Mocker.GetMock<IDiskScanService>().Setup(c => c.FilterFiles(It.IsAny<string>(), It.IsAny<IEnumerable<string>>()))
.Returns<string, IEnumerable<string>>((b, s) => s.ToList());
} }
protected void GivenChangedItem() protected void GivenChangedItem()
@ -43,7 +48,7 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole
.Setup(c => c.GetFileSize(It.IsAny<string>())) .Setup(c => c.GetFileSize(It.IsAny<string>()))
.Returns(currentSize + 1); .Returns(currentSize + 1);
} }
private void VerifySingleItem(DownloadItemStatus status) private void VerifySingleItem(DownloadItemStatus status)
{ {
var items = Subject.GetItems(_completedDownloadFolder, TimeSpan.FromMilliseconds(50)).ToList(); var items = Subject.GetItems(_completedDownloadFolder, TimeSpan.FromMilliseconds(50)).ToList();

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
@ -10,6 +11,7 @@ using NzbDrone.Common.Http;
using NzbDrone.Core.Download; using NzbDrone.Core.Download;
using NzbDrone.Core.Download.Clients.Blackhole; using NzbDrone.Core.Download.Clients.Blackhole;
using NzbDrone.Core.Exceptions; using NzbDrone.Core.Exceptions;
using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.MediaFiles.TorrentInfo; using NzbDrone.Core.MediaFiles.TorrentInfo;
using NzbDrone.Core.Parser.Model; using NzbDrone.Core.Parser.Model;
using NzbDrone.Test.Common; using NzbDrone.Test.Common;
@ -48,6 +50,9 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole
Mocker.GetMock<ITorrentFileInfoReader>() Mocker.GetMock<ITorrentFileInfoReader>()
.Setup(c => c.GetHashFromTorrentFile(It.IsAny<byte[]>())) .Setup(c => c.GetHashFromTorrentFile(It.IsAny<byte[]>()))
.Returns("myhash"); .Returns("myhash");
Mocker.GetMock<IDiskScanService>().Setup(c => c.FilterFiles(It.IsAny<string>(), It.IsAny<IEnumerable<string>>()))
.Returns<string, IEnumerable<string>>((b, s) => s.ToList());
} }
protected void GivenFailedDownload() protected void GivenFailedDownload()

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
@ -10,6 +11,7 @@ using NzbDrone.Common.Disk;
using NzbDrone.Common.Http; using NzbDrone.Common.Http;
using NzbDrone.Core.Download; using NzbDrone.Core.Download;
using NzbDrone.Core.Download.Clients.Blackhole; using NzbDrone.Core.Download.Clients.Blackhole;
using NzbDrone.Core.MediaFiles;
using NzbDrone.Test.Common; using NzbDrone.Test.Common;
namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole
@ -41,6 +43,9 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole
Mocker.GetMock<IDiskProvider>() Mocker.GetMock<IDiskProvider>()
.Setup(c => c.OpenWriteStream(It.IsAny<string>())) .Setup(c => c.OpenWriteStream(It.IsAny<string>()))
.Returns(() => new FileStream(GetTempFilePath(), FileMode.Create)); .Returns(() => new FileStream(GetTempFilePath(), FileMode.Create));
Mocker.GetMock<IDiskScanService>().Setup(c => c.FilterFiles(It.IsAny<string>(), It.IsAny<IEnumerable<string>>()))
.Returns<string, IEnumerable<string>>((b, s) => s.ToList());
} }
protected void GivenFailedDownload() protected void GivenFailedDownload()

View File

@ -2,9 +2,11 @@
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using FizzWare.NBuilder; using FizzWare.NBuilder;
using FluentAssertions;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Common.Disk; using NzbDrone.Common.Disk;
using NzbDrone.Core.Download;
using NzbDrone.Core.MediaFiles; using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.MediaFiles.EpisodeImport; using NzbDrone.Core.MediaFiles.EpisodeImport;
using NzbDrone.Core.Parser; using NzbDrone.Core.Parser;
@ -13,8 +15,6 @@ using NzbDrone.Core.Qualities;
using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Tv; using NzbDrone.Core.Tv;
using NzbDrone.Test.Common; using NzbDrone.Test.Common;
using FluentAssertions;
using NzbDrone.Core.Download;
namespace NzbDrone.Core.Test.MediaFiles namespace NzbDrone.Core.Test.MediaFiles
{ {
@ -31,6 +31,9 @@ namespace NzbDrone.Core.Test.MediaFiles
Mocker.GetMock<IDiskScanService>().Setup(c => c.GetVideoFiles(It.IsAny<string>(), It.IsAny<bool>())) Mocker.GetMock<IDiskScanService>().Setup(c => c.GetVideoFiles(It.IsAny<string>(), It.IsAny<bool>()))
.Returns(_videoFiles); .Returns(_videoFiles);
Mocker.GetMock<IDiskScanService>().Setup(c => c.FilterFiles(It.IsAny<string>(), It.IsAny<IEnumerable<string>>()))
.Returns<string, IEnumerable<string>>((b,s) => s.ToList());
Mocker.GetMock<IDiskProvider>().Setup(c => c.GetDirectories(It.IsAny<string>())) Mocker.GetMock<IDiskProvider>().Setup(c => c.GetDirectories(It.IsAny<string>()))
.Returns(_subFolders); .Returns(_subFolders);

View File

@ -92,6 +92,7 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskScanProviderTests
[TestCase("Plex Versions")] [TestCase("Plex Versions")]
[TestCase(".secret")] [TestCase(".secret")]
[TestCase(".hidden")] [TestCase(".hidden")]
[TestCase(".unwanted")]
public void should_filter_certain_sub_folders(string subFolder) public void should_filter_certain_sub_folders(string subFolder)
{ {
var path = @"C:\Test\"; var path = @"C:\Test\";
@ -99,11 +100,7 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskScanProviderTests
var specialFiles = GetFiles(path, subFolder).ToList(); var specialFiles = GetFiles(path, subFolder).ToList();
var allFiles = files.Concat(specialFiles); var allFiles = files.Concat(specialFiles);
var series = Builder<Series>.CreateNew() var filteredFiles = Subject.FilterFiles(path, allFiles);
.With(s => s.Path = path)
.Build();
var filteredFiles = Subject.FilterFiles(series, allFiles);
filteredFiles.Should().NotContain(specialFiles); filteredFiles.Should().NotContain(specialFiles);
filteredFiles.Count.Should().BeGreaterThan(0); filteredFiles.Count.Should().BeGreaterThan(0);
} }

View File

@ -1,15 +1,15 @@
using NLog; using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using NLog;
using NzbDrone.Common.Cache; using NzbDrone.Common.Cache;
using NzbDrone.Common.Crypto; using NzbDrone.Common.Crypto;
using NzbDrone.Common.Disk; using NzbDrone.Common.Disk;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Core.MediaFiles; using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.Organizer; using NzbDrone.Core.Organizer;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace NzbDrone.Core.Download.Clients.Blackhole namespace NzbDrone.Core.Download.Clients.Blackhole
{ {
@ -37,7 +37,7 @@ namespace NzbDrone.Core.Download.Clients.Blackhole
{ {
var newWatchItems = new Dictionary<string, WatchFolderItem>(); var newWatchItems = new Dictionary<string, WatchFolderItem>();
var lastWatchItems = _watchFolderItemCache.Get(watchFolder, () => newWatchItems); var lastWatchItems = _watchFolderItemCache.Get(watchFolder, () => newWatchItems);
foreach (var newWatchItem in GetDownloadItems(watchFolder, lastWatchItems, waitPeriod)) foreach (var newWatchItem in GetDownloadItems(watchFolder, lastWatchItems, waitPeriod))
{ {
newWatchItems[newWatchItem.DownloadId] = newWatchItem; newWatchItems[newWatchItem.DownloadId] = newWatchItem;
@ -50,7 +50,7 @@ namespace NzbDrone.Core.Download.Clients.Blackhole
private IEnumerable<WatchFolderItem> GetDownloadItems(string watchFolder, Dictionary<string, WatchFolderItem> lastWatchItems, TimeSpan waitPeriod) private IEnumerable<WatchFolderItem> GetDownloadItems(string watchFolder, Dictionary<string, WatchFolderItem> lastWatchItems, TimeSpan waitPeriod)
{ {
foreach (var folder in _diskProvider.GetDirectories(watchFolder)) foreach (var folder in _diskScanService.FilterFiles(watchFolder, _diskProvider.GetDirectories(watchFolder)))
{ {
var title = FileNameBuilder.CleanFileName(Path.GetFileName(folder)); var title = FileNameBuilder.CleanFileName(Path.GetFileName(folder));
@ -86,7 +86,7 @@ namespace NzbDrone.Core.Download.Clients.Blackhole
yield return newWatchItem; yield return newWatchItem;
} }
foreach (var videoFile in _diskScanService.GetVideoFiles(watchFolder, false)) foreach (var videoFile in _diskScanService.FilterFiles(watchFolder, _diskScanService.GetVideoFiles(watchFolder, false)))
{ {
var title = FileNameBuilder.CleanFileName(Path.GetFileName(videoFile)); var title = FileNameBuilder.CleanFileName(Path.GetFileName(videoFile));

View File

@ -41,7 +41,7 @@ namespace NzbDrone.Core.Extras
_logger.Debug("Looking for existing extra files in {0}", series.Path); _logger.Debug("Looking for existing extra files in {0}", series.Path);
var filesOnDisk = _diskScanService.GetNonVideoFiles(series.Path); var filesOnDisk = _diskScanService.GetNonVideoFiles(series.Path);
var possibleExtraFiles = _diskScanService.FilterFiles(series, filesOnDisk); var possibleExtraFiles = _diskScanService.FilterFiles(series.Path, filesOnDisk);
var filteredFiles = possibleExtraFiles; var filteredFiles = possibleExtraFiles;
var importedFiles = new List<string>(); var importedFiles = new List<string>();

View File

@ -24,7 +24,7 @@ namespace NzbDrone.Core.MediaFiles
void Scan(Series series); void Scan(Series series);
string[] GetVideoFiles(string path, bool allDirectories = true); string[] GetVideoFiles(string path, bool allDirectories = true);
string[] GetNonVideoFiles(string path, bool allDirectories = true); string[] GetNonVideoFiles(string path, bool allDirectories = true);
List<string> FilterFiles(Series series, IEnumerable<string> files); List<string> FilterFiles(string basePath, IEnumerable<string> files);
} }
public class DiskScanService : public class DiskScanService :
@ -60,8 +60,8 @@ namespace NzbDrone.Core.MediaFiles
_logger = logger; _logger = logger;
} }
private static readonly Regex ExcludedSubFoldersRegex = new Regex(@"(?:\\|\/|^)(extras|@eadir|extrafanart|plex\sversions|\..+)(?:\\|\/)", RegexOptions.Compiled | RegexOptions.IgnoreCase); private static readonly Regex ExcludedSubFoldersRegex = new Regex(@"(?:\\|\/|^)(?:extras|@eadir|extrafanart|plex versions|\.\w+)(?:\\|\/|$)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex ExcludedFilesRegex = new Regex(@"^\._|Thumbs\.db", RegexOptions.Compiled | RegexOptions.IgnoreCase); private static readonly Regex ExcludedFilesRegex = new Regex(@"^\._|^Thumbs\.db$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
public void Scan(Series series) public void Scan(Series series)
{ {
@ -101,7 +101,7 @@ namespace NzbDrone.Core.MediaFiles
} }
var videoFilesStopwatch = Stopwatch.StartNew(); var videoFilesStopwatch = Stopwatch.StartNew();
var mediaFileList = FilterFiles(series, GetVideoFiles(series.Path)).ToList(); var mediaFileList = FilterFiles(series.Path, GetVideoFiles(series.Path)).ToList();
videoFilesStopwatch.Stop(); videoFilesStopwatch.Stop();
_logger.Trace("Finished getting episode files for: {0} [{1}]", series, videoFilesStopwatch.Elapsed); _logger.Trace("Finished getting episode files for: {0} [{1}]", series, videoFilesStopwatch.Elapsed);
@ -158,9 +158,9 @@ namespace NzbDrone.Core.MediaFiles
return mediaFileList.ToArray(); return mediaFileList.ToArray();
} }
public List<string> FilterFiles(Series series, IEnumerable<string> files) public List<string> FilterFiles(string basePath, IEnumerable<string> files)
{ {
return files.Where(file => !ExcludedSubFoldersRegex.IsMatch(series.Path.GetRelativePath(file))) return files.Where(file => !ExcludedSubFoldersRegex.IsMatch(basePath.GetRelativePath(file)))
.Where(file => !ExcludedFilesRegex.IsMatch(Path.GetFileName(file))) .Where(file => !ExcludedFilesRegex.IsMatch(Path.GetFileName(file)))
.ToList(); .ToList();
} }

View File

@ -164,7 +164,7 @@ namespace NzbDrone.Core.MediaFiles
_logger.Debug("{0} folder quality: {1}", cleanedUpName, folderInfo.Quality); _logger.Debug("{0} folder quality: {1}", cleanedUpName, folderInfo.Quality);
} }
var videoFiles = _diskScanService.GetVideoFiles(directoryInfo.FullName); var videoFiles = _diskScanService.FilterFiles(directoryInfo.FullName, _diskScanService.GetVideoFiles(directoryInfo.FullName));
if (downloadClientItem == null) if (downloadClientItem == null)
{ {

View File

@ -111,7 +111,7 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport.Manual
if (series == null) if (series == null)
{ {
var files = _diskScanService.GetVideoFiles(folder); var files = _diskScanService.FilterFiles(folder, _diskScanService.GetVideoFiles(folder));
return files.Select(file => ProcessFile(file, downloadId, folder)).Where(i => i != null).ToList(); return files.Select(file => ProcessFile(file, downloadId, folder)).Where(i => i != null).ToList();
} }