2013-07-19 06:47:55 +03:00
|
|
|
using System.IO;
|
2013-07-16 02:53:06 +03:00
|
|
|
using System.Linq;
|
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Core.Parser.Model;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.MediaFiles.EpisodeImport.Specifications
|
|
|
|
{
|
|
|
|
public class NotExistingFileSpecification : IImportDecisionEngineSpecification
|
|
|
|
{
|
2013-07-22 09:31:51 +03:00
|
|
|
private readonly IMediaFileService _mediaFileService;
|
2013-07-16 02:53:06 +03:00
|
|
|
private readonly Logger _logger;
|
|
|
|
|
2013-07-22 09:31:51 +03:00
|
|
|
public NotExistingFileSpecification(IMediaFileService mediaFileService, Logger logger)
|
2013-07-16 02:53:06 +03:00
|
|
|
{
|
2013-07-22 09:31:51 +03:00
|
|
|
_mediaFileService = mediaFileService;
|
2013-07-16 02:53:06 +03:00
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public string RejectionReason { get { return "Existing File"; } }
|
|
|
|
|
|
|
|
public bool IsSatisfiedBy(LocalEpisode localEpisode)
|
|
|
|
{
|
2013-07-23 03:50:37 +03:00
|
|
|
if (_mediaFileService.Exists(localEpisode.Path))
|
|
|
|
{
|
|
|
|
_logger.Trace("File is a match for an existing episode file: {0}", localEpisode.Path);
|
|
|
|
return false;
|
|
|
|
}
|
2013-07-16 02:53:06 +03:00
|
|
|
|
2013-07-22 09:31:51 +03:00
|
|
|
var existingFiles = localEpisode.Episodes.Where(e => e.EpisodeFileId > 0).Select(e => e.EpisodeFile.Value);
|
|
|
|
|
|
|
|
foreach (var existingFile in existingFiles)
|
2013-07-16 02:53:06 +03:00
|
|
|
{
|
2013-07-22 09:31:51 +03:00
|
|
|
if (Path.GetFileName(existingFile.Path) == Path.GetFileName(localEpisode.Path) &&
|
|
|
|
existingFile.Size == localEpisode.Size)
|
2013-07-16 02:53:06 +03:00
|
|
|
{
|
|
|
|
_logger.Trace("File is a match for an existing episode file: {0}", localEpisode.Path);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|