mirror of
https://github.com/lidarr/lidarr.git
synced 2025-08-20 05:23:31 -07:00
Episode import uses specs and moves before import now
This commit is contained in:
parent
9ed5a06504
commit
aeb8ee06f6
22 changed files with 942 additions and 499 deletions
|
@ -6,13 +6,15 @@ using NzbDrone.Common;
|
|||
using NzbDrone.Common.Messaging;
|
||||
using NzbDrone.Core.MediaFiles.Events;
|
||||
using NzbDrone.Core.Organizer;
|
||||
using NzbDrone.Core.Parser.Model;
|
||||
using NzbDrone.Core.Tv;
|
||||
|
||||
namespace NzbDrone.Core.MediaFiles
|
||||
{
|
||||
public interface IMoveEpisodeFiles
|
||||
{
|
||||
EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile, bool newDownload = false);
|
||||
EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile);
|
||||
EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode);
|
||||
}
|
||||
|
||||
public class MoveEpisodeFiles : IMoveEpisodeFiles
|
||||
|
@ -36,56 +38,69 @@ namespace NzbDrone.Core.MediaFiles
|
|||
_logger = logger;
|
||||
}
|
||||
|
||||
public EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile, bool newDownload = false)
|
||||
public EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile)
|
||||
{
|
||||
if (episodeFile == null)
|
||||
throw new ArgumentNullException("episodeFile");
|
||||
|
||||
var series = _seriesRepository.Get(episodeFile.SeriesId);
|
||||
var episodes = _episodeService.GetEpisodesByFileId(episodeFile.Id);
|
||||
string newFileName = _buildFileNames.BuildFilename(episodes, series, episodeFile);
|
||||
var newFile = _buildFileNames.BuildFilePath(series, episodes.First().SeasonNumber, newFileName, Path.GetExtension(episodeFile.Path));
|
||||
var newFileName = _buildFileNames.BuildFilename(episodes, series, episodeFile);
|
||||
var destinationFilename = _buildFileNames.BuildFilePath(series, episodes.First().SeasonNumber, newFileName, Path.GetExtension(episodeFile.Path));
|
||||
|
||||
//Only rename if existing and new filenames don't match
|
||||
if (DiskProvider.PathEquals(episodeFile.Path, newFile))
|
||||
{
|
||||
_logger.Debug("Skipping file rename, source and destination are the same: {0}", episodeFile.Path);
|
||||
return null;
|
||||
}
|
||||
episodeFile = MoveFile(episodeFile, destinationFilename);
|
||||
|
||||
_mediaFileService.Update(episodeFile);
|
||||
|
||||
return episodeFile;
|
||||
}
|
||||
|
||||
public EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode)
|
||||
{
|
||||
var newFileName = _buildFileNames.BuildFilename(localEpisode.Episodes, localEpisode.Series, episodeFile);
|
||||
var destinationFilename = _buildFileNames.BuildFilePath(localEpisode.Series, localEpisode.SeasonNumber, newFileName, Path.GetExtension(episodeFile.Path));
|
||||
episodeFile = MoveFile(episodeFile, destinationFilename);
|
||||
|
||||
//TODO: This just re-parses the source path (which is how we got localEpisode to begin with)
|
||||
var parsedEpisodeInfo = Parser.Parser.ParsePath(localEpisode.Path);
|
||||
_messageAggregator.PublishEvent(new EpisodeDownloadedEvent(parsedEpisodeInfo, localEpisode.Series));
|
||||
|
||||
return episodeFile;
|
||||
}
|
||||
|
||||
private EpisodeFile MoveFile(EpisodeFile episodeFile, string destinationFilename)
|
||||
{
|
||||
if (!_diskProvider.FileExists(episodeFile.Path))
|
||||
{
|
||||
_logger.Error("Episode file path does not exist, {0}", episodeFile.Path);
|
||||
return null;
|
||||
}
|
||||
|
||||
_diskProvider.CreateFolder(new FileInfo(newFile).DirectoryName);
|
||||
//Only rename if existing and new filenames don't match
|
||||
if (DiskProvider.PathEquals(episodeFile.Path, destinationFilename))
|
||||
{
|
||||
_logger.Debug("Skipping file rename, source and destination are the same: {0}", episodeFile.Path);
|
||||
return null;
|
||||
}
|
||||
|
||||
_logger.Debug("Moving [{0}] > [{1}]", episodeFile.Path, newFile);
|
||||
_diskProvider.MoveFile(episodeFile.Path, newFile);
|
||||
_diskProvider.CreateFolder(new FileInfo(destinationFilename).DirectoryName);
|
||||
|
||||
_logger.Debug("Moving [{0}] > [{1}]", episodeFile.Path, destinationFilename);
|
||||
_diskProvider.MoveFile(episodeFile.Path, destinationFilename);
|
||||
|
||||
//Wrapped in Try/Catch to prevent this from causing issues with remote NAS boxes, the move worked, which is more important.
|
||||
try
|
||||
{
|
||||
_diskProvider.InheritFolderPermissions(newFile);
|
||||
_diskProvider.InheritFolderPermissions(destinationFilename);
|
||||
}
|
||||
catch (UnauthorizedAccessException ex)
|
||||
{
|
||||
_logger.Debug("Unable to apply folder permissions to: ", newFile);
|
||||
_logger.Debug("Unable to apply folder permissions to: ", destinationFilename);
|
||||
_logger.TraceException(ex.Message, ex);
|
||||
}
|
||||
|
||||
episodeFile.Path = newFile;
|
||||
_mediaFileService.Update(episodeFile);
|
||||
|
||||
var parsedEpisodeInfo = Parser.Parser.ParsePath(episodeFile.Path);
|
||||
parsedEpisodeInfo.Quality = episodeFile.Quality;
|
||||
|
||||
if (newDownload)
|
||||
{
|
||||
_messageAggregator.PublishEvent(new EpisodeDownloadedEvent(parsedEpisodeInfo, series));
|
||||
}
|
||||
|
||||
episodeFile.Path = destinationFilename;
|
||||
|
||||
return episodeFile;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue