@ -13,23 +13,29 @@ using NzbDrone.Common.Extensions;
using NzbDrone.Common.Instrumentation.Extensions ;
using NzbDrone.Core.Organizer ;
using NzbDrone.Core.Tv ;
using NzbDrone.Core.Configuration ;
namespace NzbDrone.Core.MediaFiles
{
public interface IRenameMovieFileService
{
List < RenameMovieFilePreview > GetRenamePreviews ( int movieId ) ;
void RenameMoviePath ( Movie movie , bool shouldRenameFiles ) ;
}
public class RenameMovieFileService : IRenameMovieFileService ,
IExecute < RenameMovieFilesCommand > ,
IExecute < RenameMovieCommand >
IExecute < RenameMovieCommand > ,
IExecute < RenameMovieFolderCommand >
{
private readonly IMovieService _movieService ;
private readonly IMediaFileService _mediaFileService ;
private readonly IMoveMovieFiles _movieFileMover ;
private readonly IEventAggregator _eventAggregator ;
private readonly IBuildFileNames _filenameBuilder ;
private readonly IConfigService _configService ;
private readonly IDiskProvider _diskProvider ;
private readonly IRecycleBinProvider _recycleBinProvider ;
private readonly Logger _logger ;
public RenameMovieFileService ( IMovieService movieService ,
@ -37,6 +43,9 @@ namespace NzbDrone.Core.MediaFiles
IMoveMovieFiles movieFileMover ,
IEventAggregator eventAggregator ,
IBuildFileNames filenameBuilder ,
IConfigService configService ,
IRecycleBinProvider recycleBinProvider ,
IDiskProvider diskProvider ,
Logger logger )
{
_movieService = movieService ;
@ -44,6 +53,9 @@ namespace NzbDrone.Core.MediaFiles
_movieFileMover = movieFileMover ;
_eventAggregator = eventAggregator ;
_filenameBuilder = filenameBuilder ;
_configService = configService ;
_recycleBinProvider = recycleBinProvider ;
_diskProvider = diskProvider ;
_logger = logger ;
}
@ -71,8 +83,9 @@ namespace NzbDrone.Core.MediaFiles
{
MovieId = movie . Id ,
MovieFileId = file . Id ,
ExistingPath = file . RelativePath ,
NewPath = movie . Path . GetRelativePath ( newPath )
ExistingPath = movieFilePath ,
//NewPath = movie.Path.GetRelativePath(newPath)
NewPath = newPath
} ;
}
@ -80,13 +93,19 @@ namespace NzbDrone.Core.MediaFiles
}
private void RenameFiles ( List < MovieFile > movieFiles , Movie movie )
private void RenameFiles ( List < MovieFile > movieFiles , Movie movie , string oldMoviePath = null )
{
var renamed = new List < MovieFile > ( ) ;
foreach( var movieFile in movieFiles )
if ( oldMoviePath = = null )
{
var movieFilePath = Path . Combine ( movie . Path , movieFile . RelativePath ) ;
oldMoviePath = movie . Path ;
}
foreach ( var movieFile in movieFiles )
{
var oldMovieFilePath = Path . Combine ( oldMoviePath , movieFile . RelativePath ) ;
movieFile . Path = oldMovieFilePath ;
try
{
@ -94,22 +113,64 @@ namespace NzbDrone.Core.MediaFiles
_movieFileMover . MoveMovieFile ( movieFile , movie ) ;
_mediaFileService . Update ( movieFile ) ;
_movieService . UpdateMovie ( movie ) ;
renamed . Add ( movieFile ) ;
_logger . Debug ( "Renamed movie file: {0}" , movieFile ) ;
}
catch ( SameFilenameException ex )
catch ( SameFilenameException ex )
{
_logger . Debug ( "File not renamed, source and destination are the same: {0}" , ex . Filename ) ;
}
catch ( Exception ex )
catch ( Exception ex )
{
_logger . Error ( ex , "Failed to rename file: " + m ovieFilePath) ;
_logger . Error ( ex , "Failed to rename file: " + oldM ovieFilePath) ;
}
}
}
public void RenameMoviePath ( Movie movie , bool shouldRenameFiles = true )
{
var newFolder = _filenameBuilder . BuildMoviePath ( movie ) ;
if ( newFolder ! = movie . Path & & movie . PathState = = MoviePathState . Dynamic )
{
if ( ! _configService . AutoRenameFolders )
{
_logger . Info ( "{0}'s movie should be {1} according to your naming config." , movie , newFolder ) ;
return ;
}
_logger . Info ( "{0}'s movie folder changed to: {1}" , movie , newFolder ) ;
var oldFolder = movie . Path ;
movie . Path = newFolder ;
if ( shouldRenameFiles )
{
var movieFiles = _mediaFileService . GetFilesByMovie ( movie . Id ) ;
_logger . ProgressInfo ( "Renaming movie files for {0}" , movie . Title ) ;
RenameFiles ( movieFiles , movie , oldFolder ) ;
_logger . ProgressInfo ( "All movie files renamed for {0}" , movie . Title ) ;
}
_movieService . UpdateMovie ( movie ) ;
if ( _diskProvider . GetFiles ( oldFolder , SearchOption . AllDirectories ) . Count ( ) = = 0 )
{
_recycleBinProvider . DeleteFolder ( oldFolder ) ;
}
}
if ( movie . PathState = = MoviePathState . StaticOnce )
{
movie . PathState = MoviePathState . Dynamic ;
_movieService . UpdateMovie ( movie ) ;
}
}
public void Execute ( RenameMovieFilesCommand message )
{
var movie = _movieService . GetMovie ( message . MovieId ) ;
@ -134,5 +195,17 @@ namespace NzbDrone.Core.MediaFiles
}
}
public void Execute ( RenameMovieFolderCommand message )
{
_logger . Debug ( "Renaming movie folder for selected movie if necessary" ) ;
var moviesToRename = _movieService . GetMovies ( message . MovieIds ) ;
foreach ( var movie in moviesToRename )
{
var movieFiles = _mediaFileService . GetFilesByMovie ( movie . Id ) ;
_logger . ProgressInfo ( "Renaming movie folder for {0}" , movie . Title ) ;
RenameMoviePath ( movie ) ;
}
}
}
}