Import will only delete folder if files were imported

pull/3113/head
Mark McDowall 11 years ago
parent d5ca1259b2
commit e92b273d75

File diff suppressed because it is too large Load Diff

@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using FizzWare.NBuilder;
using Moq;
using NUnit.Framework;
@ -9,7 +11,9 @@ using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.MediaFiles.Commands;
using NzbDrone.Core.MediaFiles.EpisodeImport;
using NzbDrone.Core.Parser;
using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Tv;
using NzbDrone.Test.Common;
namespace NzbDrone.Core.Test.MediaFileTests
@ -33,22 +37,22 @@ namespace NzbDrone.Core.Test.MediaFileTests
Mocker.GetMock<IDiskProvider>().Setup(c => c.GetDirectories(It.IsAny<string>()))
.Returns(_subFolders);
Mocker.GetMock<IDiskProvider>().Setup(c => c.FolderExists(It.IsAny<string>()))
.Returns(true);
Mocker.GetMock<IConfigService>().SetupGet(c => c.DownloadedEpisodesFolder)
.Returns("c:\\drop\\");
Mocker.GetMock<IImportApprovedEpisodes>()
.Setup(s => s.Import(It.IsAny<List<ImportDecision>>(), true))
.Returns(new List<ImportDecision>());
}
[Test]
public void should_import_file()
private void GivenValidSeries()
{
Subject.Execute(new DownloadedEpisodesScanCommand());
VerifyImport();
Mocker.GetMock<IParsingService>()
.Setup(s => s.GetSeries(It.IsAny<String>()))
.Returns(Builder<Series>.CreateNew().Build());
}
[Test]
@ -84,6 +88,60 @@ namespace NzbDrone.Core.Test.MediaFileTests
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()
{
Mocker.GetMock<IImportApprovedEpisodes>().Verify(c => c.Import(It.IsAny<List<ImportDecision>>(), true),

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

Loading…
Cancel
Save