@ -1,4 +1,4 @@
using System ;
using System ;
using System.Collections.Generic ;
using System.IO ;
using System.Linq ;
@ -19,23 +19,23 @@ namespace NzbDrone.Core.Extras.Metadata
{
private readonly IMetadataFactory _metadataFactory ;
private readonly ICleanMetadataService _cleanMetadataService ;
private readonly IDiskTransferService _diskTransferService ;
private readonly IDiskProvider _diskProvider ;
private readonly IDiskTransferService _diskTransferService ;
private readonly IHttpClient _httpClient ;
private readonly IMediaFileAttributeService _mediaFileAttributeService ;
private readonly IMetadataFileService _metadataFileService ;
private readonly Logger _logger ;
public MetadataService ( IConfigService configService ,
IDiskProvider diskProvider ,
IDiskTransferService diskTransferService ,
IMetadataFactory metadataFactory ,
ICleanMetadataService cleanMetadataService ,
IDiskProvider diskProvider ,
IHttpClient httpClient ,
IMediaFileAttributeService mediaFileAttributeService ,
IMetadataFileService metadataFileService ,
Logger logger )
: base ( configService , disk TransferService, metadataFileService )
: base ( configService , disk Provider, diskTransferService , logger )
{
_metadataFactory = metadataFactory ;
_cleanMetadataService = cleanMetadataService ;
@ -49,14 +49,14 @@ namespace NzbDrone.Core.Extras.Metadata
public override int Order = > 0 ;
public override IEnumerable < ExtraFile > CreateAfter SeriesScan( Series series , List < EpisodeFile > episod eFiles)
public override IEnumerable < ExtraFile > CreateAfter MovieScan( Movie movie , List < MovieFile > movi eFiles)
{
var metadataFiles = _metadataFileService . GetFilesBy Series( series . Id ) ;
_cleanMetadataService . Clean ( series ) ;
var metadataFiles = _metadataFileService . GetFilesBy Movie( movie . Id ) ;
_cleanMetadataService . Clean ( movie ) ;
if ( ! _diskProvider . FolderExists ( series . Path ) )
if ( ! _diskProvider . FolderExists ( movie . Path ) )
{
_logger . Info ( " Series folder does not exist, skipping metadata creation") ;
_logger . Info ( " Movie folder does not exist, skipping metadata creation") ;
return Enumerable . Empty < MetadataFile > ( ) ;
}
@ -66,14 +66,10 @@ namespace NzbDrone.Core.Extras.Metadata
{
var consumerFiles = GetMetadataFilesForConsumer ( consumer , metadataFiles ) ;
files . AddIfNotNull ( ProcessSeriesMetadata ( consumer , series , consumerFiles ) ) ;
files . AddRange ( ProcessSeriesImages ( consumer , series , consumerFiles ) ) ;
files . AddRange ( ProcessSeasonImages ( consumer , series , consumerFiles ) ) ;
foreach ( var episodeFile in episodeFiles )
foreach ( var episodeFile in movieFiles )
{
files . AddIfNotNull ( Process EpisodeMetadata( consumer , series , episodeFile , consumerFiles ) ) ;
files . AddRange ( Process EpisodeImages( consumer , series , episodeFile , consumerFiles ) ) ;
files . AddIfNotNull ( ProcessMovieMetadata ( consumer , movie , episodeFile , consumerFiles ) ) ;
files . AddRange ( ProcessMovieImages ( consumer , movie , episodeFile , consumerFiles ) ) ;
}
}
@ -82,47 +78,15 @@ namespace NzbDrone.Core.Extras.Metadata
return files ;
}
public override IEnumerable < ExtraFile > CreateAfterEpisodeImport ( Series series , EpisodeFile episodeFile )
{
var files = new List < MetadataFile > ( ) ;
foreach ( var consumer in _metadataFactory . Enabled ( ) )
{
files . AddIfNotNull ( ProcessEpisodeMetadata ( consumer , series , episodeFile , new List < MetadataFile > ( ) ) ) ;
files . AddRange ( ProcessEpisodeImages ( consumer , series , episodeFile , new List < MetadataFile > ( ) ) ) ;
}
_metadataFileService . Upsert ( files ) ;
return files ;
}
public override IEnumerable < ExtraFile > CreateAfterEpisodeImport ( Series series , string seriesFolder , string seasonFolder )
public override IEnumerable < ExtraFile > CreateAfterMovieImport ( Movie movie , MovieFile movieFile )
{
var metadataFiles = _metadataFileService . GetFilesBySeries ( series . Id ) ;
if ( seriesFolder . IsNullOrWhiteSpace ( ) & & seasonFolder . IsNullOrWhiteSpace ( ) )
{
return new List < MetadataFile > ( ) ;
}
var files = new List < MetadataFile > ( ) ;
foreach ( var consumer in _metadataFactory . Enabled ( ) )
{
var consumerFiles = GetMetadataFilesForConsumer ( consumer , metadataFiles ) ;
if ( seriesFolder . IsNotNullOrWhiteSpace ( ) )
{
files . AddIfNotNull ( ProcessSeriesMetadata ( consumer , series , consumerFiles ) ) ;
files . AddRange ( ProcessSeriesImages ( consumer , series , consumerFiles ) ) ;
}
if ( seasonFolder . IsNotNullOrWhiteSpace ( ) )
{
files . AddRange ( ProcessSeasonImages ( consumer , series , consumerFiles ) ) ;
}
files . AddIfNotNull ( ProcessMovieMetadata ( consumer , movie , movieFile , new List < MetadataFile > ( ) ) ) ;
files . AddRange ( ProcessMovieImages ( consumer , movie , movieFile , new List < MetadataFile > ( ) ) ) ;
}
_metadataFileService . Upsert ( files ) ;
@ -130,9 +94,9 @@ namespace NzbDrone.Core.Extras.Metadata
return files ;
}
public override IEnumerable < ExtraFile > MoveFilesAfterRename ( Series series , List < EpisodeFile > episod eFiles)
public override IEnumerable < ExtraFile > MoveFilesAfterRename ( Movie movie , List < MovieFile > movi eFiles)
{
var metadataFiles = _metadataFileService . GetFilesBy Series( series . Id ) ;
var metadataFiles = _metadataFileService . GetFilesBy Movie( movie . Id ) ;
var movedFiles = new List < MetadataFile > ( ) ;
// TODO: Move EpisodeImage and EpisodeMetadata metadata files, instead of relying on consumers to do it
@ -140,26 +104,26 @@ namespace NzbDrone.Core.Extras.Metadata
foreach ( var consumer in _metadataFactory . GetAvailableProviders ( ) )
{
foreach ( var episodeFile in episod eFiles)
foreach ( var movieFile in movi eFiles)
{
var metadataFilesForConsumer = GetMetadataFilesForConsumer ( consumer , metadataFiles ) . Where ( m = > m . EpisodeFileId = = episod eFile. Id ) . ToList ( ) ;
var metadataFilesForConsumer = GetMetadataFilesForConsumer ( consumer , metadataFiles ) . Where ( m = > m . MovieFileId = = movi eFile. Id ) . ToList ( ) ;
foreach ( var metadataFile in metadataFilesForConsumer )
{
var newFileName = consumer . GetFilenameAfterMove ( series, episod eFile, metadataFile ) ;
var existingFileName = Path . Combine ( series . Path , metadataFile . RelativePath ) ;
var newFileName = consumer . GetFilenameAfterMove ( movie, movi eFile, metadataFile ) ;
var existingFileName = Path . Combine ( movie . Path , metadataFile . RelativePath ) ;
if ( newFileName . PathNotEquals ( existingFileName ) )
{
try
{
_diskProvider . MoveFile ( existingFileName , newFileName ) ;
metadataFile . RelativePath = series . Path . GetRelativePath ( newFileName ) ;
metadataFile . RelativePath = movie . Path . GetRelativePath ( newFileName ) ;
movedFiles . Add ( metadataFile ) ;
}
catch ( Exception ex )
{
_logger . Warn ( ex , "Unable to move metadata file : {0}", existingFileName ) ;
_logger . Warn ( ex , "Unable to move metadata file after rename : {0}", existingFileName ) ;
}
}
}
@ -171,94 +135,50 @@ namespace NzbDrone.Core.Extras.Metadata
return movedFiles ;
}
public override ExtraFile Import ( Series series , EpisodeFile episod eFile, string path , string extension , bool readOnly )
public override ExtraFile Import ( Movie movie , MovieFile movi eFile, string path , string extension , bool readOnly )
{
return null ;
}
private List < MetadataFile > GetMetadataFilesForConsumer ( IMetadata consumer , List < MetadataFile > seriesMetadata )
{
return seriesMetadata . Where ( c = > c . Consumer = = consumer . GetType ( ) . Name ) . ToList ( ) ;
}
private MetadataFile ProcessSeriesMetadata ( IMetadata consumer , Series series , List < MetadataFile > existingMetadataFiles )
private List < MetadataFile > GetMetadataFilesForConsumer ( IMetadata consumer , List < MetadataFile > movieMetadata )
{
var seriesMetadata = consumer . SeriesMetadata ( series ) ;
if ( seriesMetadata = = null )
{
return null ;
}
var hash = seriesMetadata . Contents . SHA256Hash ( ) ;
var metadata = GetMetadataFile ( series , existingMetadataFiles , e = > e . Type = = MetadataType . SeriesMetadata ) ? ?
new MetadataFile
{
SeriesId = series . Id ,
Consumer = consumer . GetType ( ) . Name ,
Type = MetadataType . SeriesMetadata
} ;
if ( hash = = metadata . Hash )
{
if ( seriesMetadata . RelativePath ! = metadata . RelativePath )
{
metadata . RelativePath = seriesMetadata . RelativePath ;
return metadata ;
}
return null ;
}
var fullPath = Path . Combine ( series . Path , seriesMetadata . RelativePath ) ;
_logger . Debug ( "Writing Series Metadata to: {0}" , fullPath ) ;
SaveMetadataFile ( fullPath , seriesMetadata . Contents ) ;
metadata . Hash = hash ;
metadata . RelativePath = seriesMetadata . RelativePath ;
metadata . Extension = Path . GetExtension ( fullPath ) ;
return metadata ;
return movieMetadata . Where ( c = > c . Consumer = = consumer . GetType ( ) . Name ) . ToList ( ) ;
}
private MetadataFile Process EpisodeMetadata( IMetadata consumer , Series series , EpisodeFile episod eFile, List < MetadataFile > existingMetadataFiles )
private MetadataFile ProcessMovieMetadata ( IMetadata consumer , Movie movie , MovieFile movieFile , List < MetadataFile > existingMetadataFiles )
{
var episodeMetadata = consumer . EpisodeMetadata ( series , episod eFile) ;
var movieFileMetadata = consumer . MovieMetadata ( movie , movieFile ) ;
if ( episod eMetadata = = null )
if ( movieFileMetadata = = null )
{
return null ;
}
var fullPath = Path . Combine ( series. Path , episod eMetadata. RelativePath ) ;
var fullPath = Path . Combine ( movie . Path , movieFileMetadata . RelativePath ) ;
var existingMetadata = GetMetadataFile ( series , existingMetadataFiles , c = > c . Type = = MetadataType . Episod eMetadata & &
c . EpisodeFileId = = episod eFile. Id ) ;
var existingMetadata = GetMetadataFile ( movie , existingMetadataFiles , c = > c . Type = = MetadataType . MovieMetadata & &
c . MovieFileId = = movieFile . Id ) ;
if ( existingMetadata ! = null )
{
var existingFullPath = Path . Combine ( series . Path , existingMetadata . RelativePath ) ;
var existingFullPath = Path . Combine ( movie . Path , existingMetadata . RelativePath ) ;
if ( fullPath . PathNotEquals ( existingFullPath ) )
{
_diskTransferService . TransferFile ( existingFullPath , fullPath , TransferMode . Move ) ;
existingMetadata . RelativePath = episod eMetadata. RelativePath ;
existingMetadata . RelativePath = movieFileMetadata . RelativePath ;
}
}
var hash = episod eMetadata. Contents . SHA256Hash ( ) ;
var hash = movieFileMetadata . Contents . SHA256Hash ( ) ;
var metadata = existingMetadata ? ?
new MetadataFile
{
SeriesId = series . Id ,
SeasonNumber = episodeFile . SeasonNumber ,
EpisodeFileId = episodeFile . Id ,
MovieId = movie . Id ,
MovieFileId = movieFile . Id ,
Consumer = consumer . GetType ( ) . Name ,
Type = MetadataType . Episod eMetadata,
RelativePath = episod eMetadata. RelativePath ,
Type = MetadataType . MovieMetadata ,
RelativePath = movieFileMetadata . RelativePath ,
Extension = Path . GetExtension ( fullPath )
} ;
@ -267,105 +187,34 @@ namespace NzbDrone.Core.Extras.Metadata
return null ;
}
_logger . Debug ( "Writing Episod e Metadata to: {0}", fullPath ) ;
SaveMetadataFile ( fullPath , episod eMetadata. Contents ) ;
_logger . Debug ( "Writing Movie Fil e Metadata to: {0}", fullPath ) ;
SaveMetadataFile ( fullPath , movieFil eMetadata. Contents ) ;
metadata . Hash = hash ;
return metadata ;
}
private List < MetadataFile > ProcessSeriesImages ( IMetadata consumer , Series series , List < MetadataFile > existingMetadataFiles )
{
var result = new List < MetadataFile > ( ) ;
foreach ( var image in consumer . SeriesImages ( series ) )
{
var fullPath = Path . Combine ( series . Path , image . RelativePath ) ;
if ( _diskProvider . FileExists ( fullPath ) )
{
_logger . Debug ( "Series image already exists: {0}" , fullPath ) ;
continue ;
}
var metadata = GetMetadataFile ( series , existingMetadataFiles , c = > c . Type = = MetadataType . SeriesImage & &
c . RelativePath = = image . RelativePath ) ? ?
new MetadataFile
{
SeriesId = series . Id ,
Consumer = consumer . GetType ( ) . Name ,
Type = MetadataType . SeriesImage ,
RelativePath = image . RelativePath ,
Extension = Path . GetExtension ( fullPath )
} ;
DownloadImage ( series , image ) ;
result . Add ( metadata ) ;
}
return result ;
}
private List < MetadataFile > ProcessSeasonImages ( IMetadata consumer , Series series , List < MetadataFile > existingMetadataFiles )
{
var result = new List < MetadataFile > ( ) ;
foreach ( var season in series . Seasons )
{
foreach ( var image in consumer . SeasonImages ( series , season ) )
{
var fullPath = Path . Combine ( series . Path , image . RelativePath ) ;
if ( _diskProvider . FileExists ( fullPath ) )
{
_logger . Debug ( "Season image already exists: {0}" , fullPath ) ;
continue ;
}
var metadata = GetMetadataFile ( series , existingMetadataFiles , c = > c . Type = = MetadataType . SeasonImage & &
c . SeasonNumber = = season . SeasonNumber & &
c . RelativePath = = image . RelativePath ) ? ?
new MetadataFile
{
SeriesId = series . Id ,
SeasonNumber = season . SeasonNumber ,
Consumer = consumer . GetType ( ) . Name ,
Type = MetadataType . SeasonImage ,
RelativePath = image . RelativePath ,
Extension = Path . GetExtension ( fullPath )
} ;
DownloadImage ( series , image ) ;
result . Add ( metadata ) ;
}
}
return result ;
}
private List < MetadataFile > ProcessEpisodeImages ( IMetadata consumer , Series series , EpisodeFile episodeFile , List < MetadataFile > existingMetadataFiles )
private List < MetadataFile > ProcessMovieImages ( IMetadata consumer , Movie movie , MovieFile movieFile , List < MetadataFile > existingMetadataFiles )
{
var result = new List < MetadataFile > ( ) ;
foreach ( var image in consumer . EpisodeImages( series , episod eFile) )
foreach ( var image in consumer . MovieImages ( movie , movieFile ) )
{
var fullPath = Path . Combine ( series . Path , image . RelativePath ) ;
var fullPath = Path . Combine ( movie . Path , image . RelativePath ) ;
if ( _diskProvider . FileExists ( fullPath ) )
{
_logger . Debug ( " Episod e image already exists: {0}", fullPath ) ;
_logger . Debug ( "Movie image already exists: {0}" , fullPath ) ;
continue ;
}
var existingMetadata = GetMetadataFile ( series , existingMetadataFiles , c = > c . Type = = MetadataType . Episod eImage & &
c . EpisodeFileId = = episodeFile . Id ) ;
var existingMetadata = GetMetadataFile ( movie , existingMetadataFiles , c = > c . Type = = MetadataType . MovieImage & &
c . RelativePath = = image . RelativePath ) ;
if ( existingMetadata ! = null )
{
var existingFullPath = Path . Combine ( series . Path , existingMetadata . RelativePath ) ;
var existingFullPath = Path . Combine ( movie . Path , existingMetadata . RelativePath ) ;
if ( fullPath . PathNotEquals ( existingFullPath ) )
{
_diskTransferService . TransferFile ( existingFullPath , fullPath , TransferMode . Move ) ;
@ -378,16 +227,15 @@ namespace NzbDrone.Core.Extras.Metadata
var metadata = existingMetadata ? ?
new MetadataFile
{
SeriesId = series . Id ,
SeasonNumber = episodeFile . SeasonNumber ,
EpisodeFileId = episodeFile . Id ,
MovieId = movie . Id ,
MovieFileId = movieFile . Id ,
Consumer = consumer . GetType ( ) . Name ,
Type = MetadataType . Episod eImage,
Type = MetadataType . Movi eImage,
RelativePath = image . RelativePath ,
Extension = Path . GetExtension ( fullPath )
} ;
DownloadImage ( series , image ) ;
DownloadImage ( movie , image ) ;
result . Add ( metadata ) ;
}
@ -395,9 +243,9 @@ namespace NzbDrone.Core.Extras.Metadata
return result ;
}
private void DownloadImage ( Series series , ImageFileResult image )
private void DownloadImage ( Movie movie , ImageFileResult image )
{
var fullPath = Path . Combine ( series . Path , image . RelativePath ) ;
var fullPath = Path . Combine ( movie . Path , image . RelativePath ) ;
try
{
@ -413,11 +261,11 @@ namespace NzbDrone.Core.Extras.Metadata
}
catch ( WebException ex )
{
_logger . Warn ( ex , "Couldn't download image {0} for {1}. {2}" , image . Url , series , ex . Message ) ;
_logger . Warn ( ex , "Couldn't download image {0} for {1}. {2}" , image . Url , movie , ex . Message ) ;
}
catch ( Exception ex )
{
_logger . Error ( ex , "Couldn't download image {0} for {1}. {2}" , image . Url , series , ex . Message ) ;
_logger . Error ( ex , "Couldn't download image {0} for {1}. {2}" , image . Url , movie , ex . Message ) ;
}
}
@ -427,7 +275,7 @@ namespace NzbDrone.Core.Extras.Metadata
_mediaFileAttributeService . SetFilePermissions ( path ) ;
}
private MetadataFile GetMetadataFile ( Series series , List < MetadataFile > existingMetadataFiles , Func < MetadataFile , bool > predicate )
private MetadataFile GetMetadataFile ( Movie movie , List < MetadataFile > existingMetadataFiles , Func < MetadataFile , bool > predicate )
{
var matchingMetadataFiles = existingMetadataFiles . Where ( predicate ) . ToList ( ) ;
@ -439,7 +287,7 @@ namespace NzbDrone.Core.Extras.Metadata
//Remove duplicate metadata files from DB and disk
foreach ( var file in matchingMetadataFiles . Skip ( 1 ) )
{
var path = Path . Combine ( series . Path , file . RelativePath ) ;
var path = Path . Combine ( movie . Path , file . RelativePath ) ;
_logger . Debug ( "Removing duplicate Metadata file: {0}" , path ) ;