2013-07-19 03:47:55 +00:00
|
|
|
|
using System.Linq;
|
2013-07-16 02:56:46 +00:00
|
|
|
|
using NLog;
|
2013-07-31 05:11:54 +00:00
|
|
|
|
using NzbDrone.Common;
|
2013-07-16 02:56:46 +00:00
|
|
|
|
using NzbDrone.Core.Parser.Model;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.MediaFiles
|
|
|
|
|
{
|
|
|
|
|
public interface IUpgradeMediaFiles
|
|
|
|
|
{
|
|
|
|
|
EpisodeFile UpgradeEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class UpgradeMediaFileService : IUpgradeMediaFiles
|
|
|
|
|
{
|
|
|
|
|
private readonly IRecycleBinProvider _recycleBinProvider;
|
|
|
|
|
private readonly IMediaFileService _mediaFileService;
|
|
|
|
|
private readonly IMoveEpisodeFiles _episodeFileMover;
|
2013-07-31 05:11:54 +00:00
|
|
|
|
private readonly IDiskProvider _diskProvider;
|
2013-07-16 02:56:46 +00:00
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
|
|
public UpgradeMediaFileService(IRecycleBinProvider recycleBinProvider,
|
|
|
|
|
IMediaFileService mediaFileService,
|
|
|
|
|
IMoveEpisodeFiles episodeFileMover,
|
2013-07-31 05:11:54 +00:00
|
|
|
|
IDiskProvider diskProvider,
|
2013-07-16 02:56:46 +00:00
|
|
|
|
Logger logger)
|
|
|
|
|
{
|
|
|
|
|
_recycleBinProvider = recycleBinProvider;
|
|
|
|
|
_mediaFileService = mediaFileService;
|
|
|
|
|
_episodeFileMover = episodeFileMover;
|
2013-07-31 05:11:54 +00:00
|
|
|
|
_diskProvider = diskProvider;
|
2013-07-16 02:56:46 +00:00
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EpisodeFile UpgradeEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode)
|
|
|
|
|
{
|
|
|
|
|
var existingFiles = localEpisode.Episodes
|
|
|
|
|
.Where(e => e.EpisodeFileId > 0)
|
|
|
|
|
.Select(e => e.EpisodeFile.Value)
|
|
|
|
|
.GroupBy(e => e.Id);
|
|
|
|
|
|
2013-08-08 05:40:31 +00:00
|
|
|
|
episodeFile = _episodeFileMover.PreMoveEpisodeFile(episodeFile, localEpisode);
|
2013-07-31 05:11:54 +00:00
|
|
|
|
|
2013-07-16 02:56:46 +00:00
|
|
|
|
foreach (var existingFile in existingFiles)
|
|
|
|
|
{
|
|
|
|
|
var file = existingFile.First();
|
|
|
|
|
|
2013-07-31 05:11:54 +00:00
|
|
|
|
if (_diskProvider.FileExists(file.Path))
|
|
|
|
|
{
|
|
|
|
|
_logger.Trace("Removing existing episode file: {0}", file);
|
|
|
|
|
_recycleBinProvider.DeleteFile(file.Path);
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-23 00:50:37 +00:00
|
|
|
|
_mediaFileService.Delete(file, true);
|
2013-07-16 02:56:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.Trace("Moving episode file: {0}", episodeFile);
|
|
|
|
|
return _episodeFileMover.MoveEpisodeFile(episodeFile, localEpisode);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|