mirror of https://github.com/Radarr/Radarr
108 lines
4.1 KiB
C#
108 lines
4.1 KiB
C#
using System.IO;
|
|
using System.Linq;
|
|
using NLog;
|
|
using NzbDrone.Common.Disk;
|
|
using NzbDrone.Core.Parser.Model;
|
|
|
|
namespace NzbDrone.Core.MediaFiles
|
|
{
|
|
public interface IUpgradeMediaFiles
|
|
{
|
|
EpisodeFileMoveResult UpgradeEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode, bool copyOnly = false);
|
|
MovieFileMoveResult UpgradeMovieFile(MovieFile movieFile, LocalMovie localMovie, bool copyOnly = false);
|
|
}
|
|
|
|
public class UpgradeMediaFileService : IUpgradeMediaFiles
|
|
{
|
|
private readonly IRecycleBinProvider _recycleBinProvider;
|
|
private readonly IMediaFileService _mediaFileService;
|
|
private readonly IMoveEpisodeFiles _episodeFileMover;
|
|
private readonly IMoveMovieFiles _movieFileMover;
|
|
private readonly IDiskProvider _diskProvider;
|
|
private readonly Logger _logger;
|
|
|
|
public UpgradeMediaFileService(IRecycleBinProvider recycleBinProvider,
|
|
IMediaFileService mediaFileService,
|
|
IMoveEpisodeFiles episodeFileMover,
|
|
IMoveMovieFiles movieFileMover,
|
|
IDiskProvider diskProvider,
|
|
Logger logger)
|
|
{
|
|
_recycleBinProvider = recycleBinProvider;
|
|
_mediaFileService = mediaFileService;
|
|
_episodeFileMover = episodeFileMover;
|
|
_movieFileMover = movieFileMover;
|
|
_diskProvider = diskProvider;
|
|
_logger = logger;
|
|
}
|
|
|
|
public MovieFileMoveResult UpgradeMovieFile(MovieFile movieFile, LocalMovie localMovie, bool copyOnly = false)
|
|
{
|
|
_logger.Trace("Upgrading existing movie file.");
|
|
var moveFileResult = new MovieFileMoveResult();
|
|
|
|
var existingFile = localMovie.Movie.MovieFile.Value;
|
|
|
|
if (existingFile != null)
|
|
{
|
|
var movieFilePath = Path.Combine(localMovie.Movie.Path, existingFile.RelativePath);
|
|
|
|
if (_diskProvider.FileExists(movieFilePath))
|
|
{
|
|
_logger.Debug("Removing existing movie file: {0}", existingFile);
|
|
_recycleBinProvider.DeleteFile(movieFilePath);
|
|
}
|
|
|
|
moveFileResult.OldFiles.Add(existingFile);
|
|
_mediaFileService.Delete(existingFile, DeleteMediaFileReason.Upgrade);
|
|
}
|
|
|
|
if (copyOnly)
|
|
{
|
|
moveFileResult.MovieFile = _movieFileMover.CopyMovieFile(movieFile, localMovie);
|
|
}
|
|
else
|
|
{
|
|
moveFileResult.MovieFile = _movieFileMover.MoveMovieFile(movieFile, localMovie);
|
|
}
|
|
|
|
return moveFileResult;
|
|
}
|
|
|
|
public EpisodeFileMoveResult UpgradeEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode, bool copyOnly = false)
|
|
{
|
|
var moveFileResult = new EpisodeFileMoveResult();
|
|
var existingFiles = localEpisode.Episodes
|
|
.Where(e => e.EpisodeFileId > 0)
|
|
.Select(e => e.EpisodeFile.Value)
|
|
.GroupBy(e => e.Id);
|
|
|
|
foreach (var existingFile in existingFiles)
|
|
{
|
|
var file = existingFile.First();
|
|
var episodeFilePath = Path.Combine(localEpisode.Series.Path, file.RelativePath);
|
|
|
|
if (_diskProvider.FileExists(episodeFilePath))
|
|
{
|
|
_logger.Debug("Removing existing episode file: {0}", file);
|
|
_recycleBinProvider.DeleteFile(episodeFilePath);
|
|
}
|
|
|
|
moveFileResult.OldFiles.Add(file);
|
|
_mediaFileService.Delete(file, DeleteMediaFileReason.Upgrade);
|
|
}
|
|
|
|
if (copyOnly)
|
|
{
|
|
moveFileResult.EpisodeFile = _episodeFileMover.CopyEpisodeFile(episodeFile, localEpisode);
|
|
}
|
|
else
|
|
{
|
|
moveFileResult.EpisodeFile = _episodeFileMover.MoveEpisodeFile(episodeFile, localEpisode);
|
|
}
|
|
|
|
return moveFileResult;
|
|
}
|
|
}
|
|
}
|