2014-04-30 03:03:14 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Diagnostics;
|
2014-03-26 02:25:45 +03:00
|
|
|
using System.IO;
|
2011-06-20 04:59:31 +03:00
|
|
|
using System.Linq;
|
|
|
|
using NLog;
|
2014-01-06 09:20:08 +03:00
|
|
|
using NzbDrone.Common.Disk;
|
2013-11-19 09:25:02 +03:00
|
|
|
using NzbDrone.Core.Configuration;
|
2014-03-13 23:12:42 +03:00
|
|
|
using NzbDrone.Core.Instrumentation.Extensions;
|
2013-05-13 03:36:23 +03:00
|
|
|
using NzbDrone.Core.MediaFiles.Commands;
|
2013-07-07 00:47:49 +03:00
|
|
|
using NzbDrone.Core.MediaFiles.EpisodeImport;
|
2014-03-08 21:35:48 +03:00
|
|
|
using NzbDrone.Core.MediaFiles.Events;
|
2013-09-14 09:42:09 +03:00
|
|
|
using NzbDrone.Core.Messaging.Commands;
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
2013-02-19 09:01:03 +03:00
|
|
|
using NzbDrone.Core.Tv;
|
2013-05-21 01:45:16 +03:00
|
|
|
using NzbDrone.Core.Tv.Events;
|
2011-06-20 04:59:31 +03:00
|
|
|
|
2013-05-13 03:36:23 +03:00
|
|
|
namespace NzbDrone.Core.MediaFiles
|
2011-06-20 04:59:31 +03:00
|
|
|
{
|
2013-04-15 08:27:32 +03:00
|
|
|
public interface IDiskScanService
|
|
|
|
{
|
2014-03-22 08:32:57 +03:00
|
|
|
void Scan(Series series);
|
2013-04-15 08:27:32 +03:00
|
|
|
string[] GetVideoFiles(string path, bool allDirectories = true);
|
|
|
|
}
|
|
|
|
|
2013-07-13 23:21:34 +03:00
|
|
|
public class DiskScanService :
|
|
|
|
IDiskScanService,
|
2014-02-05 08:29:57 +03:00
|
|
|
IHandle<SeriesUpdatedEvent>,
|
|
|
|
IExecute<RescanSeriesCommand>
|
2011-06-20 04:59:31 +03:00
|
|
|
{
|
2013-05-11 02:53:50 +03:00
|
|
|
private readonly IDiskProvider _diskProvider;
|
2013-07-07 00:47:49 +03:00
|
|
|
private readonly IMakeImportDecision _importDecisionMaker;
|
|
|
|
private readonly IImportApprovedEpisodes _importApprovedEpisodes;
|
2013-09-14 09:36:07 +03:00
|
|
|
private readonly ICommandExecutor _commandExecutor;
|
2013-11-19 09:25:02 +03:00
|
|
|
private readonly IConfigService _configService;
|
2014-02-05 08:29:57 +03:00
|
|
|
private readonly ISeriesService _seriesService;
|
2014-03-08 21:35:48 +03:00
|
|
|
private readonly IEventAggregator _eventAggregator;
|
2013-08-18 07:28:34 +03:00
|
|
|
private readonly Logger _logger;
|
2011-06-20 04:59:31 +03:00
|
|
|
|
2013-07-07 00:47:49 +03:00
|
|
|
public DiskScanService(IDiskProvider diskProvider,
|
2014-03-08 21:35:48 +03:00
|
|
|
IMakeImportDecision importDecisionMaker,
|
|
|
|
IImportApprovedEpisodes importApprovedEpisodes,
|
|
|
|
ICommandExecutor commandExecutor,
|
|
|
|
IConfigService configService,
|
|
|
|
ISeriesService seriesService,
|
|
|
|
IEventAggregator eventAggregator,
|
|
|
|
Logger logger)
|
2011-06-20 04:59:31 +03:00
|
|
|
{
|
|
|
|
_diskProvider = diskProvider;
|
2013-07-07 00:47:49 +03:00
|
|
|
_importDecisionMaker = importDecisionMaker;
|
|
|
|
_importApprovedEpisodes = importApprovedEpisodes;
|
2013-09-14 09:36:07 +03:00
|
|
|
_commandExecutor = commandExecutor;
|
2013-11-19 09:25:02 +03:00
|
|
|
_configService = configService;
|
2014-02-05 08:29:57 +03:00
|
|
|
_seriesService = seriesService;
|
2014-03-08 21:35:48 +03:00
|
|
|
_eventAggregator = eventAggregator;
|
2013-08-18 07:28:34 +03:00
|
|
|
_logger = logger;
|
2011-06-20 04:59:31 +03:00
|
|
|
}
|
|
|
|
|
2014-03-22 08:32:57 +03:00
|
|
|
public void Scan(Series series)
|
2011-06-20 04:59:31 +03:00
|
|
|
{
|
2014-05-26 08:14:15 +03:00
|
|
|
var rootFolder = _diskProvider.GetParentFolder(series.Path);
|
|
|
|
|
|
|
|
if (!_diskProvider.FolderExists(rootFolder))
|
|
|
|
{
|
|
|
|
_logger.Warn("Series' root folder ({0}) doesn't exist.", rootFolder);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-11 09:33:47 +03:00
|
|
|
_logger.ProgressInfo("Scanning disk for {0}", series.Title);
|
2013-09-14 09:36:07 +03:00
|
|
|
_commandExecutor.PublishCommand(new CleanMediaFileDb(series.Id));
|
2013-07-13 23:21:34 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
if (!_diskProvider.FolderExists(series.Path))
|
2011-10-22 22:03:54 +03:00
|
|
|
{
|
2013-11-19 09:25:02 +03:00
|
|
|
if (_configService.CreateEmptySeriesFolders &&
|
2014-05-26 08:14:15 +03:00
|
|
|
_diskProvider.FolderExists(rootFolder))
|
2013-11-19 09:25:02 +03:00
|
|
|
{
|
|
|
|
_logger.Debug("Creating missing series folder: {0}", series.Path);
|
|
|
|
_diskProvider.CreateFolder(series.Path);
|
2013-12-01 03:33:59 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_logger.Debug("Series folder doesn't exist: {0}", series.Path);
|
2013-11-19 09:25:02 +03:00
|
|
|
}
|
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
return;
|
2011-06-20 04:59:31 +03:00
|
|
|
}
|
|
|
|
|
2014-03-26 02:25:45 +03:00
|
|
|
var videoFilesStopwatch = Stopwatch.StartNew();
|
2014-04-30 03:03:14 +03:00
|
|
|
var mediaFileList = GetVideoFiles(series.Path).Where(file => !file.StartsWith(Path.Combine(series.Path, "EXTRAS"))).ToList();
|
2014-03-26 02:25:45 +03:00
|
|
|
videoFilesStopwatch.Stop();
|
|
|
|
_logger.Trace("Finished getting episode files for: {0} [{1}]", series, videoFilesStopwatch.Elapsed);
|
2011-06-20 04:59:31 +03:00
|
|
|
|
2014-03-26 02:25:45 +03:00
|
|
|
var decisionsStopwatch = Stopwatch.StartNew();
|
2013-07-23 08:50:32 +03:00
|
|
|
var decisions = _importDecisionMaker.GetImportDecisions(mediaFileList, series, false);
|
2014-03-26 02:25:45 +03:00
|
|
|
decisionsStopwatch.Stop();
|
|
|
|
_logger.Trace("Import decisions complete for: {0} [{1}]", series, decisionsStopwatch.Elapsed);
|
|
|
|
|
2014-04-19 18:09:22 +03:00
|
|
|
_importApprovedEpisodes.Import(decisions, false);
|
2014-03-08 21:35:48 +03:00
|
|
|
|
2013-11-08 04:52:50 +03:00
|
|
|
_logger.Info("Completed scanning disk for {0}", series.Title);
|
2014-03-08 21:35:48 +03:00
|
|
|
_eventAggregator.PublishEvent(new SeriesScannedEvent(series));
|
2011-06-20 04:59:31 +03:00
|
|
|
}
|
|
|
|
|
2013-05-13 03:36:23 +03:00
|
|
|
public string[] GetVideoFiles(string path, bool allDirectories = true)
|
2011-06-20 04:59:31 +03:00
|
|
|
{
|
2013-08-18 07:28:34 +03:00
|
|
|
_logger.Debug("Scanning '{0}' for video files", path);
|
2011-06-20 04:59:31 +03:00
|
|
|
|
2012-08-27 13:15:22 +03:00
|
|
|
var searchOption = allDirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
|
|
|
|
var filesOnDisk = _diskProvider.GetFiles(path, searchOption);
|
2011-06-20 04:59:31 +03:00
|
|
|
|
2013-09-12 09:33:28 +03:00
|
|
|
var mediaFileList = filesOnDisk.Where(c => MediaFileExtensions.Extensions.Contains(Path.GetExtension(c).ToLower())).ToList();
|
2011-06-20 04:59:31 +03:00
|
|
|
|
2013-11-08 04:52:50 +03:00
|
|
|
_logger.Debug("{0} video files were found in {1}", mediaFileList.Count, path);
|
2013-04-15 04:41:39 +03:00
|
|
|
return mediaFileList.ToArray();
|
2011-06-20 04:59:31 +03:00
|
|
|
}
|
2013-05-13 03:36:23 +03:00
|
|
|
|
2013-07-13 23:21:34 +03:00
|
|
|
public void Handle(SeriesUpdatedEvent message)
|
2013-05-21 01:45:16 +03:00
|
|
|
{
|
|
|
|
Scan(message.Series);
|
|
|
|
}
|
2014-02-05 08:29:57 +03:00
|
|
|
|
|
|
|
public void Execute(RescanSeriesCommand message)
|
|
|
|
{
|
2014-04-10 18:56:18 +03:00
|
|
|
if (message.SeriesId.HasValue)
|
|
|
|
{
|
|
|
|
var series = _seriesService.GetSeries(message.SeriesId.Value);
|
|
|
|
Scan(series);
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var allSeries = _seriesService.GetAllSeries();
|
2014-02-05 08:29:57 +03:00
|
|
|
|
2014-04-10 18:56:18 +03:00
|
|
|
foreach (var series in allSeries)
|
|
|
|
{
|
|
|
|
Scan(series);
|
|
|
|
}
|
|
|
|
}
|
2014-02-05 08:29:57 +03:00
|
|
|
}
|
2011-06-20 04:59:31 +03:00
|
|
|
}
|
2011-06-20 06:25:04 +03:00
|
|
|
}
|