Import will only delete folder if files were imported

This commit is contained in:
Mark McDowall 2013-07-17 21:50:58 -07:00
parent d5ca1259b2
commit e92b273d75
3 changed files with 803 additions and 715 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,7 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using FizzWare.NBuilder; using FizzWare.NBuilder;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
@ -9,7 +11,9 @@ using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.MediaFiles.Commands; using NzbDrone.Core.MediaFiles.Commands;
using NzbDrone.Core.MediaFiles.EpisodeImport; using NzbDrone.Core.MediaFiles.EpisodeImport;
using NzbDrone.Core.Parser; using NzbDrone.Core.Parser;
using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Tv;
using NzbDrone.Test.Common; using NzbDrone.Test.Common;
namespace NzbDrone.Core.Test.MediaFileTests namespace NzbDrone.Core.Test.MediaFileTests
@ -33,22 +37,22 @@ namespace NzbDrone.Core.Test.MediaFileTests
Mocker.GetMock<IDiskProvider>().Setup(c => c.GetDirectories(It.IsAny<string>())) Mocker.GetMock<IDiskProvider>().Setup(c => c.GetDirectories(It.IsAny<string>()))
.Returns(_subFolders); .Returns(_subFolders);
Mocker.GetMock<IDiskProvider>().Setup(c => c.FolderExists(It.IsAny<string>())) Mocker.GetMock<IDiskProvider>().Setup(c => c.FolderExists(It.IsAny<string>()))
.Returns(true); .Returns(true);
Mocker.GetMock<IConfigService>().SetupGet(c => c.DownloadedEpisodesFolder) Mocker.GetMock<IConfigService>().SetupGet(c => c.DownloadedEpisodesFolder)
.Returns("c:\\drop\\"); .Returns("c:\\drop\\");
Mocker.GetMock<IImportApprovedEpisodes>()
.Setup(s => s.Import(It.IsAny<List<ImportDecision>>(), true))
.Returns(new List<ImportDecision>());
} }
[Test] private void GivenValidSeries()
public void should_import_file()
{ {
Subject.Execute(new DownloadedEpisodesScanCommand()); Mocker.GetMock<IParsingService>()
.Setup(s => s.GetSeries(It.IsAny<String>()))
VerifyImport(); .Returns(Builder<Series>.CreateNew().Build());
} }
[Test] [Test]
@ -84,6 +88,60 @@ namespace NzbDrone.Core.Test.MediaFileTests
VerifyNoImport(); VerifyNoImport();
} }
[Test]
public void should_not_import_if_folder_is_a_series_path()
{
Mocker.GetMock<ISeriesService>()
.Setup(s => s.SeriesPathExists(It.IsAny<String>()))
.Returns(true);
Mocker.GetMock<IDiskScanService>()
.Setup(c => c.GetVideoFiles(It.IsAny<string>(), It.IsAny<bool>()))
.Returns(new string[0]);
Subject.Execute(new DownloadedEpisodesScanCommand());
Mocker.GetMock<IParsingService>()
.Verify(v => v.GetSeries(It.IsAny<String>()), Times.Never());
}
[Test]
public void should_not_delete_folder_if_no_files_were_imported()
{
Mocker.GetMock<IImportApprovedEpisodes>()
.Setup(s => s.Import(It.IsAny<List<ImportDecision>>(), false))
.Returns(new List<ImportDecision>());
Subject.Execute(new DownloadedEpisodesScanCommand());
Mocker.GetMock<DiskProvider>()
.Verify(v => v.GetFolderSize(It.IsAny<String>()), Times.Never());
}
[Test]
public void should_delete_folder_if_files_were_imported()
{
GivenValidSeries();
var localEpisode = new LocalEpisode();
var imported = new List<ImportDecision>();
imported.Add(new ImportDecision(localEpisode));
Mocker.GetMock<IMakeImportDecision>()
.Setup(s => s.GetImportDecisions(It.IsAny<IEnumerable<String>>(), It.IsAny<Series>()))
.Returns(imported);
Mocker.GetMock<IImportApprovedEpisodes>()
.Setup(s => s.Import(It.IsAny<List<ImportDecision>>(), true))
.Returns(imported);
Subject.Execute(new DownloadedEpisodesScanCommand());
Mocker.GetMock<IDiskProvider>()
.Verify(v => v.DeleteFolder(It.IsAny<String>(), true), Times.Once());
}
private void VerifyNoImport() private void VerifyNoImport()
{ {
Mocker.GetMock<IImportApprovedEpisodes>().Verify(c => c.Import(It.IsAny<List<ImportDecision>>(), true), Mocker.GetMock<IImportApprovedEpisodes>().Verify(c => c.Import(It.IsAny<List<ImportDecision>>(), true),

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using NLog; using NLog;
using NzbDrone.Common; using NzbDrone.Common;
using NzbDrone.Common.Messaging; using NzbDrone.Common.Messaging;
@ -64,11 +65,15 @@ namespace NzbDrone.Core.MediaFiles
{ {
try try
{ {
if (!_seriesService.SeriesPathExists(subFolder)) if (_seriesService.SeriesPathExists(subFolder))
{ {
ProcessSubFolder(new DirectoryInfo(subFolder)); continue;
}
//Todo: We should make sure the file(s) are actually imported var importedFiles = ProcessSubFolder(new DirectoryInfo(subFolder));
if (importedFiles.Any())
{
if (_diskProvider.GetFolderSize(subFolder) < NotSampleSpecification.SampleSizeLimit) if (_diskProvider.GetFolderSize(subFolder) < NotSampleSpecification.SampleSizeLimit)
{ {
_diskProvider.DeleteFolder(subFolder, true); _diskProvider.DeleteFolder(subFolder, true);
@ -85,14 +90,7 @@ namespace NzbDrone.Core.MediaFiles
{ {
try try
{ {
var series = _parsingService.GetSeries(Path.GetFileNameWithoutExtension(videoFile)); ProcessVideoFile(videoFile);
if (series == null)
{
_logger.Debug("Unknown Series for file: {0}", videoFile);
}
ProcessVideoFile(videoFile, series);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -101,23 +99,31 @@ namespace NzbDrone.Core.MediaFiles
} }
} }
private void ProcessSubFolder(DirectoryInfo subfolderInfo) private List<ImportDecision> ProcessSubFolder(DirectoryInfo subfolderInfo)
{ {
var series = _parsingService.GetSeries(subfolderInfo.Name); var series = _parsingService.GetSeries(subfolderInfo.Name);
if (series == null) if (series == null)
{ {
_logger.Debug("Unknown Series {0}", subfolderInfo.Name); _logger.Debug("Unknown Series {0}", subfolderInfo.Name);
return; return new List<ImportDecision>();
} }
var videoFiles = _diskScanService.GetVideoFiles(subfolderInfo.FullName); var videoFiles = _diskScanService.GetVideoFiles(subfolderInfo.FullName);
ProcessFiles(videoFiles, series); return ProcessFiles(videoFiles, series);
} }
private void ProcessVideoFile(string videoFile, Series series) private void ProcessVideoFile(string videoFile)
{ {
var series = _parsingService.GetSeries(Path.GetFileNameWithoutExtension(videoFile));
if (series == null)
{
_logger.Debug("Unknown Series for file: {0}", videoFile);
return;
}
if (_diskProvider.IsFileLocked(new FileInfo(videoFile))) if (_diskProvider.IsFileLocked(new FileInfo(videoFile)))
{ {
_logger.Debug("[{0}] is currently locked by another process, skipping", videoFile); _logger.Debug("[{0}] is currently locked by another process, skipping", videoFile);
@ -127,10 +133,10 @@ namespace NzbDrone.Core.MediaFiles
ProcessFiles(new[] { videoFile }, series); ProcessFiles(new[] { videoFile }, series);
} }
private void ProcessFiles(IEnumerable<string> videoFiles, Series series) private List<ImportDecision> ProcessFiles(IEnumerable<string> videoFiles, Series series)
{ {
var decisions = _importDecisionMaker.GetImportDecisions(videoFiles, series); var decisions = _importDecisionMaker.GetImportDecisions(videoFiles, series);
_importApprovedEpisodes.Import(decisions, true); return _importApprovedEpisodes.Import(decisions, true);
} }
public void Execute(DownloadedEpisodesScanCommand message) public void Execute(DownloadedEpisodesScanCommand message)