2013-05-13 00:36:23 +00:00
|
|
|
using System;
|
|
|
|
using System.Linq;
|
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Common;
|
|
|
|
using NzbDrone.Common.Messaging;
|
|
|
|
using NzbDrone.Core.MediaFiles.Commands;
|
2013-07-23 00:50:37 +00:00
|
|
|
using NzbDrone.Core.Parser;
|
2013-05-13 00:36:23 +00:00
|
|
|
using NzbDrone.Core.Tv;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.MediaFiles
|
|
|
|
{
|
|
|
|
|
|
|
|
public class MediaFileTableCleanupService : IExecute<CleanMediaFileDb>
|
|
|
|
{
|
|
|
|
private readonly IMediaFileService _mediaFileService;
|
|
|
|
private readonly IDiskProvider _diskProvider;
|
|
|
|
private readonly IEpisodeService _episodeService;
|
2013-07-23 00:50:37 +00:00
|
|
|
private readonly ISeriesService _seriesService;
|
|
|
|
private readonly IParsingService _parsingService;
|
2013-05-13 00:36:23 +00:00
|
|
|
private readonly Logger _logger;
|
|
|
|
|
2013-07-23 00:50:37 +00:00
|
|
|
public MediaFileTableCleanupService(IMediaFileService mediaFileService,
|
|
|
|
IDiskProvider diskProvider,
|
|
|
|
IEpisodeService episodeService,
|
|
|
|
ISeriesService seriesService,
|
|
|
|
IParsingService parsingService,
|
|
|
|
Logger logger)
|
2013-05-13 00:36:23 +00:00
|
|
|
{
|
|
|
|
_mediaFileService = mediaFileService;
|
|
|
|
_diskProvider = diskProvider;
|
|
|
|
_episodeService = episodeService;
|
2013-07-23 00:50:37 +00:00
|
|
|
_seriesService = seriesService;
|
|
|
|
_parsingService = parsingService;
|
2013-05-13 00:36:23 +00:00
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Execute(CleanMediaFileDb message)
|
|
|
|
{
|
|
|
|
var seriesFile = _mediaFileService.GetFilesBySeries(message.SeriesId);
|
2013-07-23 00:50:37 +00:00
|
|
|
var series = _seriesService.GetSeries(message.SeriesId);
|
2013-05-13 00:36:23 +00:00
|
|
|
|
|
|
|
foreach (var episodeFile in seriesFile)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (!_diskProvider.FileExists(episodeFile.Path))
|
|
|
|
{
|
2013-07-23 00:50:37 +00:00
|
|
|
_logger.Trace("File [{0}] no longer exists on disk, removing from db", episodeFile.Path);
|
2013-05-13 00:36:23 +00:00
|
|
|
_mediaFileService.Delete(episodeFile);
|
2013-07-23 00:50:37 +00:00
|
|
|
continue;
|
2013-05-13 00:36:23 +00:00
|
|
|
}
|
|
|
|
|
2013-07-23 00:50:37 +00:00
|
|
|
if (!_diskProvider.IsParent(series.Path, episodeFile.Path))
|
2013-05-13 00:36:23 +00:00
|
|
|
{
|
2013-07-23 00:50:37 +00:00
|
|
|
_logger.Trace("File [{0}] does not belong to this series, removing from db", episodeFile.Path);
|
2013-05-13 00:36:23 +00:00
|
|
|
_mediaFileService.Delete(episodeFile);
|
2013-07-23 00:50:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var episodes = _episodeService.GetEpisodesByFileId(episodeFile.Id);
|
|
|
|
|
|
|
|
if (!episodes.Any())
|
|
|
|
{
|
|
|
|
_logger.Trace("File [{0}] is not assigned to any episodes, removing from db", episodeFile.Path);
|
|
|
|
_mediaFileService.Delete(episodeFile);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var localEpsiode = _parsingService.GetEpisodes(episodeFile.Path, series);
|
|
|
|
|
|
|
|
if (localEpsiode == null || episodes.Count != localEpsiode.Episodes.Count)
|
|
|
|
{
|
|
|
|
_logger.Trace("File [{0}] parsed episodes has changed, removing from db", episodeFile.Path);
|
|
|
|
_mediaFileService.Delete(episodeFile);
|
|
|
|
continue;
|
2013-05-13 00:36:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
var errorMessage = String.Format("Unable to cleanup EpisodeFile in DB: {0}", episodeFile.Id);
|
|
|
|
_logger.ErrorException(errorMessage, ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|