2013-03-07 04:34:56 +00:00
|
|
|
using System;
|
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Common;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.MediaFiles
|
|
|
|
{
|
|
|
|
public interface ICleanGhostFiles
|
|
|
|
{
|
|
|
|
void RemoveNonExistingFiles(int seriesId);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class GhostFileCleanupService : ICleanGhostFiles
|
|
|
|
{
|
|
|
|
private readonly IMediaFileService _mediaFileService;
|
2013-05-10 23:53:50 +00:00
|
|
|
private readonly IDiskProvider _diskProvider;
|
2013-03-07 04:34:56 +00:00
|
|
|
private readonly Logger _logger;
|
|
|
|
|
2013-05-10 23:53:50 +00:00
|
|
|
public GhostFileCleanupService(IMediaFileService mediaFileService, IDiskProvider diskProvider, Logger logger)
|
2013-03-07 04:34:56 +00:00
|
|
|
{
|
|
|
|
_mediaFileService = mediaFileService;
|
|
|
|
_diskProvider = diskProvider;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void RemoveNonExistingFiles(int seriesId)
|
|
|
|
{
|
|
|
|
var seriesFile = _mediaFileService.GetFilesBySeries(seriesId);
|
|
|
|
|
|
|
|
foreach (var episodeFile in seriesFile)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (!_diskProvider.FileExists(episodeFile.Path))
|
|
|
|
{
|
|
|
|
_logger.Trace("File [{0}] no longer exists on disk. removing from db", episodeFile.Path);
|
|
|
|
_mediaFileService.Delete(episodeFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
var message = String.Format("Unable to cleanup EpisodeFile in DB: {0}", episodeFile.Id);
|
|
|
|
_logger.ErrorException(message, ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|