1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-11-01 08:22:35 +01:00
Sonarr/NzbDrone.Core/MediaFiles/DownloadedEpisodesImportService.cs

128 lines
4.3 KiB
C#
Raw Normal View History

using System;
using System.IO;
using NLog;
using NzbDrone.Common;
2013-05-15 04:57:57 +02:00
using NzbDrone.Common.Messaging;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.MediaFiles.Commands;
using NzbDrone.Core.Parser;
using NzbDrone.Core.Tv;
2013-05-15 04:57:57 +02:00
namespace NzbDrone.Core.MediaFiles
{
2013-05-15 04:57:57 +02:00
public class DownloadedEpisodesImportService : IExecute<DownloadedEpisodesScanCommand>
{
2013-05-11 01:53:50 +02:00
private readonly IDiskProvider _diskProvider;
2013-04-15 07:27:32 +02:00
private readonly IDiskScanService _diskScanService;
private readonly ISeriesService _seriesService;
private readonly IMoveEpisodeFiles _episodeFileMover;
private readonly IParsingService _parsingService;
2013-05-15 04:57:57 +02:00
private readonly IConfigService _configService;
private readonly Logger _logger;
2013-05-15 04:57:57 +02:00
public DownloadedEpisodesImportService(IDiskProvider diskProvider,
2013-04-15 07:27:32 +02:00
IDiskScanService diskScanService,
ISeriesService seriesService,
IMoveEpisodeFiles episodeFileMover,
IParsingService parsingService,
2013-05-15 04:57:57 +02:00
IConfigService configService,
Logger logger)
{
_diskProvider = diskProvider;
2013-04-15 07:27:32 +02:00
_diskScanService = diskScanService;
_seriesService = seriesService;
_episodeFileMover = episodeFileMover;
_parsingService = parsingService;
2013-05-15 04:57:57 +02:00
_configService = configService;
_logger = logger;
}
public void ProcessSubFolder(DirectoryInfo subfolderInfo)
{
if (_diskProvider.GetLastFolderWrite(subfolderInfo.FullName).AddMinutes(2) > DateTime.UtcNow)
{
_logger.Trace("[{0}] is too fresh. skipping", subfolderInfo.FullName);
return;
}
var series = _parsingService.GetSeries(subfolderInfo.Name);
if (series == null)
{
_logger.Trace("Unknown Series {0}", subfolderInfo.Name);
return;
}
2013-04-15 07:27:32 +02:00
var files = _diskScanService.GetVideoFiles(subfolderInfo.FullName);
foreach (var file in files)
{
ProcessVideoFile(file, series);
}
}
public void ProcessVideoFile(string videoFile, Series series)
{
if (_diskProvider.GetLastFileWrite(videoFile).AddMinutes(2) > DateTime.UtcNow)
{
_logger.Trace("[{0}] is too fresh. skipping", videoFile);
return;
}
if (_diskProvider.IsFileLocked(new FileInfo(videoFile)))
{
_logger.Trace("[{0}] is currently locked by another process, skipping", videoFile);
return;
}
2013-04-15 07:27:32 +02:00
var episodeFile = _diskScanService.ImportFile(series, videoFile);
if (episodeFile != null)
{
_episodeFileMover.MoveEpisodeFile(episodeFile, true);
}
}
2013-05-15 04:57:57 +02:00
public void Execute(DownloadedEpisodesScanCommand message)
{
//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));
}
}
catch (Exception e)
{
_logger.ErrorException("An error has occurred while importing folder: " + subfolder, e);
}
}
foreach (var videoFile in _diskScanService.GetVideoFiles(downloadedEpisodesFolder, false))
{
try
{
var series = _parsingService.GetSeries(videoFile);
ProcessVideoFile(videoFile, series);
}
catch (Exception ex)
{
_logger.ErrorException("An error has occurred while importing video file" + videoFile, ex);
}
}
//TODO: cleanup empty folders
}
}
}