1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00

Metadata file improvements

Fixed: Duplicate metadata files won't cause metadata file processing to fail
Fixed: Duplicate Metadata files will be removed from disk during rescan
This commit is contained in:
Mark McDowall 2015-02-09 17:07:48 -08:00
parent 573c2b8f60
commit db4eadac40

View File

@ -145,7 +145,7 @@ private MetadataFile ProcessSeriesMetadata(IMetadata consumer, Series series, Li
var hash = seriesMetadata.Contents.SHA256Hash();
var metadata = existingMetadataFiles.SingleOrDefault(e => e.Type == MetadataType.SeriesMetadata) ??
var metadata = GetMetadataFile(series, existingMetadataFiles, e => e.Type == MetadataType.SeriesMetadata) ??
new MetadataFile
{
SeriesId = series.Id,
@ -180,8 +180,8 @@ private MetadataFile ProcessEpisodeMetadata(IMetadata consumer, Series series, E
var fullPath = Path.Combine(series.Path, episodeMetadata.RelativePath);
var existingMetadata = existingMetadataFiles.SingleOrDefault(c => c.Type == MetadataType.EpisodeMetadata &&
c.EpisodeFileId == episodeFile.Id);
var existingMetadata = GetMetadataFile(series, existingMetadataFiles, c => c.Type == MetadataType.EpisodeMetadata &&
c.EpisodeFileId == episodeFile.Id);
if (existingMetadata != null)
{
@ -230,8 +230,8 @@ private List<MetadataFile> ProcessSeriesImages(IMetadata consumer, Series series
continue;
}
var metadata = existingMetadataFiles.SingleOrDefault(c => c.Type == MetadataType.SeriesImage &&
c.RelativePath == image.RelativePath) ??
var metadata = GetMetadataFile(series, existingMetadataFiles, c => c.Type == MetadataType.SeriesImage &&
c.RelativePath == image.RelativePath) ??
new MetadataFile
{
SeriesId = series.Id,
@ -252,19 +252,23 @@ private List<MetadataFile> ProcessSeasonImages(IMetadata consumer, Series series
{
var result = new List<MetadataFile>();
foreach (var season in series.Seasons)
foreach (var seasonItem in series.Seasons)
{
foreach (var image in consumer.SeasonImages(series, season))
var season = seasonItem;
foreach (var imageItem in consumer.SeasonImages(series, season))
{
var image = imageItem;
if (_diskProvider.FileExists(image.RelativePath))
{
_logger.Debug("Season image already exists: {0}", image.RelativePath);
continue;
}
var metadata = existingMetadataFiles.SingleOrDefault(c => c.Type == MetadataType.SeasonImage &&
c.SeasonNumber == season.SeasonNumber &&
c.RelativePath == image.RelativePath) ??
var metadata = GetMetadataFile(series, existingMetadataFiles, c => c.Type == MetadataType.SeasonImage &&
c.SeasonNumber == season.SeasonNumber &&
c.RelativePath == image.RelativePath) ??
new MetadataFile
{
SeriesId = series.Id,
@ -297,8 +301,8 @@ private List<MetadataFile> ProcessEpisodeImages(IMetadata consumer, Series serie
continue;
}
var existingMetadata = existingMetadataFiles.FirstOrDefault(c => c.Type == MetadataType.EpisodeImage &&
c.EpisodeFileId == episodeFile.Id);
var existingMetadata = GetMetadataFile(series, existingMetadataFiles, c => c.Type == MetadataType.EpisodeImage &&
c.EpisodeFileId == episodeFile.Id);
if (existingMetadata != null)
{
@ -352,5 +356,29 @@ private void SaveMetadataFile(String path, String contents)
_diskProvider.WriteAllText(path, contents);
_mediaFileAttributeService.SetFilePermissions(path);
}
private MetadataFile GetMetadataFile(Series series, List<MetadataFile> existingMetadataFiles, Func<MetadataFile, bool> predicate)
{
var matchingMetadataFiles = existingMetadataFiles.Where(predicate).ToList();
if (matchingMetadataFiles.Empty())
{
return null;
}
//Remove duplicate metadata files from DB and disk
foreach (var file in matchingMetadataFiles.Skip(1))
{
var path = Path.Combine(series.Path, file.RelativePath);
_logger.Debug("Removing duplicate Metadata file: {0}", path);
_diskProvider.DeleteFile(path);
_metadataFileService.Delete(file.Id);
}
return matchingMetadataFiles.First();
}
}
}