2013-05-13 03:36:23 +03:00
|
|
|
using System;
|
|
|
|
using System.Linq;
|
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Common;
|
|
|
|
using NzbDrone.Core.MediaFiles.Commands;
|
2013-09-11 09:33:47 +03:00
|
|
|
using NzbDrone.Core.Messaging;
|
2013-05-13 03:36:23 +03: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 03:50:37 +03:00
|
|
|
private readonly ISeriesService _seriesService;
|
2013-05-13 03:36:23 +03:00
|
|
|
private readonly Logger _logger;
|
|
|
|
|
2013-07-23 03:50:37 +03:00
|
|
|
public MediaFileTableCleanupService(IMediaFileService mediaFileService,
|
|
|
|
IDiskProvider diskProvider,
|
|
|
|
IEpisodeService episodeService,
|
|
|
|
ISeriesService seriesService,
|
|
|
|
Logger logger)
|
2013-05-13 03:36:23 +03:00
|
|
|
{
|
|
|
|
_mediaFileService = mediaFileService;
|
|
|
|
_diskProvider = diskProvider;
|
|
|
|
_episodeService = episodeService;
|
2013-07-23 03:50:37 +03:00
|
|
|
_seriesService = seriesService;
|
2013-05-13 03:36:23 +03:00
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Execute(CleanMediaFileDb message)
|
|
|
|
{
|
|
|
|
var seriesFile = _mediaFileService.GetFilesBySeries(message.SeriesId);
|
2013-07-23 03:50:37 +03:00
|
|
|
var series = _seriesService.GetSeries(message.SeriesId);
|
2013-08-03 09:44:10 +03:00
|
|
|
var episodes = _episodeService.GetEpisodeBySeries(message.SeriesId);
|
2013-05-13 03:36:23 +03:00
|
|
|
|
|
|
|
foreach (var episodeFile in seriesFile)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (!_diskProvider.FileExists(episodeFile.Path))
|
|
|
|
{
|
2013-07-23 03:50:37 +03:00
|
|
|
_logger.Trace("File [{0}] no longer exists on disk, removing from db", episodeFile.Path);
|
2013-05-13 03:36:23 +03:00
|
|
|
_mediaFileService.Delete(episodeFile);
|
2013-07-23 03:50:37 +03:00
|
|
|
continue;
|
2013-05-13 03:36:23 +03:00
|
|
|
}
|
|
|
|
|
2013-07-23 03:50:37 +03:00
|
|
|
if (!_diskProvider.IsParent(series.Path, episodeFile.Path))
|
2013-05-13 03:36:23 +03:00
|
|
|
{
|
2013-07-23 03:50:37 +03:00
|
|
|
_logger.Trace("File [{0}] does not belong to this series, removing from db", episodeFile.Path);
|
2013-05-13 03:36:23 +03:00
|
|
|
_mediaFileService.Delete(episodeFile);
|
2013-08-03 09:44:10 +03:00
|
|
|
continue;
|
2013-07-23 03:50:37 +03:00
|
|
|
}
|
|
|
|
|
2013-08-03 09:44:10 +03:00
|
|
|
if (!episodes.Any(e => e.EpisodeFileId == episodeFile.Id))
|
2013-07-23 03:50:37 +03:00
|
|
|
{
|
|
|
|
_logger.Trace("File [{0}] is not assigned to any episodes, removing from db", episodeFile.Path);
|
|
|
|
_mediaFileService.Delete(episodeFile);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-07-23 04:37:52 +03:00
|
|
|
// 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 03:36:23 +03:00
|
|
|
}
|
2013-08-03 09:44:10 +03:00
|
|
|
|
2013-05-13 03:36:23 +03:00
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
var errorMessage = String.Format("Unable to cleanup EpisodeFile in DB: {0}", episodeFile.Id);
|
|
|
|
_logger.ErrorException(errorMessage, ex);
|
|
|
|
}
|
|
|
|
}
|
2013-08-03 09:44:10 +03:00
|
|
|
|
|
|
|
foreach (var episode in episodes)
|
|
|
|
{
|
2013-08-03 21:18:01 +03:00
|
|
|
if (episode.EpisodeFileId > 0 && !seriesFile.Any(f => f.Id == episode.EpisodeFileId))
|
2013-08-03 09:44:10 +03:00
|
|
|
{
|
|
|
|
episode.EpisodeFileId = 0;
|
|
|
|
_episodeService.UpdateEpisode(episode);
|
|
|
|
}
|
|
|
|
}
|
2013-05-13 03:36:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|