2013-04-15 01:41:39 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Common;
|
2013-05-15 02:57:57 +00:00
|
|
|
|
using NzbDrone.Common.Messaging;
|
|
|
|
|
using NzbDrone.Core.Configuration;
|
|
|
|
|
using NzbDrone.Core.MediaFiles.Commands;
|
2013-06-09 06:20:38 +00:00
|
|
|
|
using NzbDrone.Core.MediaFiles.Events;
|
2013-04-15 01:41:39 +00:00
|
|
|
|
using NzbDrone.Core.Parser;
|
|
|
|
|
using NzbDrone.Core.Tv;
|
|
|
|
|
|
2013-05-15 02:57:57 +00:00
|
|
|
|
namespace NzbDrone.Core.MediaFiles
|
2013-04-15 01:41:39 +00:00
|
|
|
|
{
|
2013-05-15 02:57:57 +00:00
|
|
|
|
public class DownloadedEpisodesImportService : IExecute<DownloadedEpisodesScanCommand>
|
2013-04-15 01:41:39 +00:00
|
|
|
|
{
|
2013-05-10 23:53:50 +00:00
|
|
|
|
private readonly IDiskProvider _diskProvider;
|
2013-04-15 05:27:32 +00:00
|
|
|
|
private readonly IDiskScanService _diskScanService;
|
2013-04-15 01:41:39 +00:00
|
|
|
|
private readonly ISeriesService _seriesService;
|
|
|
|
|
private readonly IMoveEpisodeFiles _episodeFileMover;
|
|
|
|
|
private readonly IParsingService _parsingService;
|
2013-05-15 02:57:57 +00:00
|
|
|
|
private readonly IConfigService _configService;
|
2013-06-09 06:20:38 +00:00
|
|
|
|
private readonly IMessageAggregator _messageAggregator;
|
2013-04-15 01:41:39 +00:00
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
2013-05-15 02:57:57 +00:00
|
|
|
|
public DownloadedEpisodesImportService(IDiskProvider diskProvider,
|
2013-04-15 05:27:32 +00:00
|
|
|
|
IDiskScanService diskScanService,
|
2013-04-15 01:41:39 +00:00
|
|
|
|
ISeriesService seriesService,
|
|
|
|
|
IMoveEpisodeFiles episodeFileMover,
|
|
|
|
|
IParsingService parsingService,
|
2013-05-15 02:57:57 +00:00
|
|
|
|
IConfigService configService,
|
2013-06-09 06:20:38 +00:00
|
|
|
|
IMessageAggregator messageAggregator,
|
2013-04-15 01:41:39 +00:00
|
|
|
|
Logger logger)
|
|
|
|
|
{
|
|
|
|
|
_diskProvider = diskProvider;
|
2013-04-15 05:27:32 +00:00
|
|
|
|
_diskScanService = diskScanService;
|
2013-04-15 01:41:39 +00:00
|
|
|
|
_seriesService = seriesService;
|
|
|
|
|
_episodeFileMover = episodeFileMover;
|
|
|
|
|
_parsingService = parsingService;
|
2013-05-15 02:57:57 +00:00
|
|
|
|
_configService = configService;
|
2013-06-09 06:20:38 +00:00
|
|
|
|
_messageAggregator = messageAggregator;
|
2013-04-15 01:41:39 +00:00
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-09 06:20:38 +00:00
|
|
|
|
public void ProcessDownloadedEpisodesFolder()
|
2013-05-16 00:25:51 +00:00
|
|
|
|
{
|
|
|
|
|
//TODO: We should also process the download client's category folder
|
|
|
|
|
var downloadedEpisodesFolder = _configService.DownloadedEpisodesFolder;
|
|
|
|
|
|
|
|
|
|
if (String.IsNullOrEmpty(downloadedEpisodesFolder))
|
|
|
|
|
{
|
|
|
|
|
_logger.Warn("Downloaded Episodes Folder is not configured");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var subfolder in _diskProvider.GetDirectories(downloadedEpisodesFolder))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (!_seriesService.SeriesPathExists(subfolder))
|
|
|
|
|
{
|
|
|
|
|
ProcessSubFolder(new DirectoryInfo(subfolder));
|
2013-05-16 00:29:26 +00:00
|
|
|
|
|
|
|
|
|
if (_diskProvider.GetDirectorySize(subfolder) < 50.Megabytes())
|
|
|
|
|
{
|
|
|
|
|
_diskProvider.DeleteFolder(subfolder, true);
|
|
|
|
|
}
|
2013-05-16 00:25:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("An error has occurred while importing folder: " + subfolder, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var videoFile in _diskScanService.GetVideoFiles(downloadedEpisodesFolder, false))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2013-06-05 03:13:57 +00:00
|
|
|
|
var series = _parsingService.GetSeries(Path.GetFileNameWithoutExtension(videoFile));
|
|
|
|
|
|
|
|
|
|
if (series == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.Trace("Unknown Series for file: {0}", videoFile);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-16 00:25:51 +00:00
|
|
|
|
ProcessVideoFile(videoFile, series);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("An error has occurred while importing video file" + videoFile, ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-15 01:41:39 +00:00
|
|
|
|
public void ProcessSubFolder(DirectoryInfo subfolderInfo)
|
|
|
|
|
{
|
2013-05-20 04:52:06 +00:00
|
|
|
|
var lastFolderWrite = _diskProvider.GetLastFolderWrite(subfolderInfo.FullName);
|
|
|
|
|
if (lastFolderWrite.AddMinutes(2) > DateTime.UtcNow)
|
2013-04-15 01:41:39 +00:00
|
|
|
|
{
|
2013-05-20 04:52:06 +00:00
|
|
|
|
_logger.Trace("[{0}] is too fresh. skipping. Last write {1} UTC", subfolderInfo.FullName, lastFolderWrite);
|
2013-04-15 01:41:39 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var series = _parsingService.GetSeries(subfolderInfo.Name);
|
|
|
|
|
|
|
|
|
|
if (series == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.Trace("Unknown Series {0}", subfolderInfo.Name);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-15 05:27:32 +00:00
|
|
|
|
var files = _diskScanService.GetVideoFiles(subfolderInfo.FullName);
|
2013-04-15 01:41:39 +00:00
|
|
|
|
|
|
|
|
|
foreach (var file in files)
|
|
|
|
|
{
|
|
|
|
|
ProcessVideoFile(file, series);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-09 07:14:38 +00:00
|
|
|
|
private void ProcessVideoFile(string videoFile, Series series)
|
2013-04-15 01:41:39 +00:00
|
|
|
|
{
|
2013-05-20 04:52:06 +00:00
|
|
|
|
var lastWrite = _diskProvider.GetLastFileWrite(videoFile);
|
|
|
|
|
|
|
|
|
|
if (lastWrite.AddMinutes(2) > DateTime.UtcNow)
|
2013-04-15 01:41:39 +00:00
|
|
|
|
{
|
2013-05-20 04:52:06 +00:00
|
|
|
|
_logger.Trace("[{0}] is too fresh. skipping. Last Write {1} UTC", videoFile, lastWrite);
|
2013-04-15 01:41:39 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_diskProvider.IsFileLocked(new FileInfo(videoFile)))
|
|
|
|
|
{
|
|
|
|
|
_logger.Trace("[{0}] is currently locked by another process, skipping", videoFile);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-15 05:27:32 +00:00
|
|
|
|
var episodeFile = _diskScanService.ImportFile(series, videoFile);
|
2013-04-15 01:41:39 +00:00
|
|
|
|
|
|
|
|
|
if (episodeFile != null)
|
|
|
|
|
{
|
|
|
|
|
_episodeFileMover.MoveEpisodeFile(episodeFile, true);
|
2013-06-09 07:14:38 +00:00
|
|
|
|
_messageAggregator.PublishEvent(new EpisodeImportedEvent(episodeFile));
|
2013-04-15 01:41:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-15 02:57:57 +00:00
|
|
|
|
public void Execute(DownloadedEpisodesScanCommand message)
|
|
|
|
|
{
|
2013-06-09 06:20:38 +00:00
|
|
|
|
ProcessDownloadedEpisodesFolder();
|
2013-05-15 02:57:57 +00:00
|
|
|
|
}
|
2013-04-15 01:41:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|