|
|
@ -1,4 +1,4 @@
|
|
|
|
using System;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.IO;
|
|
|
|
using NLog;
|
|
|
|
using NLog;
|
|
|
@ -18,14 +18,17 @@ namespace NzbDrone.Core.MediaFiles
|
|
|
|
public class MediaFileTableCleanupService : IMediaFileTableCleanupService
|
|
|
|
public class MediaFileTableCleanupService : IMediaFileTableCleanupService
|
|
|
|
{
|
|
|
|
{
|
|
|
|
private readonly IMediaFileService _mediaFileService;
|
|
|
|
private readonly IMediaFileService _mediaFileService;
|
|
|
|
|
|
|
|
private readonly IMovieService _movieService;
|
|
|
|
private readonly IEpisodeService _episodeService;
|
|
|
|
private readonly IEpisodeService _episodeService;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
|
|
|
|
public MediaFileTableCleanupService(IMediaFileService mediaFileService,
|
|
|
|
public MediaFileTableCleanupService(IMediaFileService mediaFileService,
|
|
|
|
|
|
|
|
IMovieService movieService,
|
|
|
|
IEpisodeService episodeService,
|
|
|
|
IEpisodeService episodeService,
|
|
|
|
Logger logger)
|
|
|
|
Logger logger)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_mediaFileService = mediaFileService;
|
|
|
|
_mediaFileService = mediaFileService;
|
|
|
|
|
|
|
|
_movieService = movieService;
|
|
|
|
_episodeService = episodeService;
|
|
|
|
_episodeService = episodeService;
|
|
|
|
_logger = logger;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -89,61 +92,39 @@ namespace NzbDrone.Core.MediaFiles
|
|
|
|
|
|
|
|
|
|
|
|
public void Clean(Movie movie, List<string> filesOnDisk)
|
|
|
|
public void Clean(Movie movie, List<string> filesOnDisk)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
var movieFiles = _mediaFileService.GetFilesByMovie(movie.Id);
|
|
|
|
//TODO: Update implementation for movies.
|
|
|
|
|
|
|
|
var seriesFiles = _mediaFileService.GetFilesBySeries(movie.Id);
|
|
|
|
|
|
|
|
var episodes = _episodeService.GetEpisodeBySeries(movie.Id);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var filesOnDiskKeys = new HashSet<string>(filesOnDisk, PathEqualityComparer.Instance);
|
|
|
|
var filesOnDiskKeys = new HashSet<string>(filesOnDisk, PathEqualityComparer.Instance);
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var seriesFile in seriesFiles)
|
|
|
|
foreach(var movieFile in movieFiles)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var episodeFile = seriesFile;
|
|
|
|
var movieFilePath = Path.Combine(movie.Path, movieFile.RelativePath);
|
|
|
|
var episodeFilePath = Path.Combine(movie.Path, episodeFile.RelativePath);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
try
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (!filesOnDiskKeys.Contains(episodeFilePath))
|
|
|
|
if (!filesOnDiskKeys.Contains(movieFilePath))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_logger.Debug("File [{0}] no longer exists on disk, removing from db", episodeFilePath);
|
|
|
|
_logger.Debug("File [{0}] no longer exists on disk, removing from db", movieFilePath);
|
|
|
|
_mediaFileService.Delete(seriesFile, DeleteMediaFileReason.MissingFromDisk);
|
|
|
|
_mediaFileService.Delete(movieFile, DeleteMediaFileReason.MissingFromDisk);
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (episodes.None(e => e.EpisodeFileId == episodeFile.Id))
|
|
|
|
//var localMovie = _parsingService.GetLocalMovie(movieFile.Path, movie);
|
|
|
|
{
|
|
|
|
|
|
|
|
_logger.Debug("File [{0}] is not assigned to any episodes, removing from db", episodeFilePath);
|
|
|
|
|
|
|
|
_mediaFileService.Delete(episodeFile, DeleteMediaFileReason.NoLinkedEpisodes);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// var localEpsiode = _parsingService.GetLocalEpisode(episodeFile.Path, series);
|
|
|
|
//if (localMovie == null)
|
|
|
|
//
|
|
|
|
|
|
|
|
// if (localEpsiode == null || episodes.Count != localEpsiode.Episodes.Count)
|
|
|
|
|
|
|
|
//{
|
|
|
|
//{
|
|
|
|
// _logger.Debug("File [{0}] parsed episodes has changed, removing from db", episodeFile.Path);
|
|
|
|
// _logger.Debug("File [{0}] parsed episodes has changed, removing from db", localMovie.Path);
|
|
|
|
// _mediaFileService.Delete(episodeFile);
|
|
|
|
// _mediaFileService.Delete(localMovie);
|
|
|
|
// continue;
|
|
|
|
// continue;
|
|
|
|
//}
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var errorMessage = string.Format("Unable to cleanup EpisodeFile in DB: {0}", episodeFile.Id);
|
|
|
|
var errorMessage = string.Format("Unable to cleanup MovieFile in DB: {0}", movieFile.Id);
|
|
|
|
_logger.Error(ex, errorMessage);
|
|
|
|
_logger.Error(ex, errorMessage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var e in episodes)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var episode = e;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (episode.EpisodeFileId > 0 && seriesFiles.None(f => f.Id == episode.EpisodeFileId))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
episode.EpisodeFileId = 0;
|
|
|
|
|
|
|
|
_episodeService.UpdateEpisode(episode);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|