Merge pull request #11119 from PeachesMLG/cleanup-emby-photos

Cleanup PhotoProvider.cs using new .NET 8 features
This commit is contained in:
Bond-009 2024-03-09 21:14:28 +01:00 committed by GitHub
commit ededaa95b4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -16,8 +16,8 @@ using TagLib.IFD;
using TagLib.IFD.Entries;
using TagLib.IFD.Tags;
namespace Emby.Photos
{
namespace Emby.Photos;
/// <summary>
/// Metadata provider for photos.
/// </summary>
@ -27,7 +27,7 @@ namespace Emby.Photos
private readonly IImageProcessor _imageProcessor;
// These are causing taglib to hang
private readonly string[] _includeExtensions = new string[] { ".jpg", ".jpeg", ".png", ".tiff", ".cr2", ".webp", ".avif" };
private readonly string[] _includeExtensions = [".jpg", ".jpeg", ".png", ".tiff", ".cr2", ".webp", ".avif"];
/// <summary>
/// Initializes a new instance of the <see cref="PhotoProvider" /> class.
@ -65,27 +65,23 @@ namespace Emby.Photos
{
try
{
using (var file = TagLib.File.Create(item.Path))
{
using var file = TagLib.File.Create(item.Path);
if (file.GetTag(TagTypes.TiffIFD) is IFDTag tag)
{
var structure = tag.Structure;
if (structure is not null
&& structure.GetEntry(0, (ushort)IFDEntryTag.ExifIFD) is SubIFDEntry exif)
if (structure?.GetEntry(0, (ushort)IFDEntryTag.ExifIFD) is SubIFDEntry exif)
{
var exifStructure = exif.Structure;
if (exifStructure is not null)
{
var entry = exifStructure.GetEntry(0, (ushort)ExifEntryTag.ApertureValue) as RationalIFDEntry;
if (entry is not null)
if (exifStructure.GetEntry(0, (ushort)ExifEntryTag.ApertureValue) is RationalIFDEntry apertureEntry)
{
item.Aperture = (double)entry.Value.Numerator / entry.Value.Denominator;
item.Aperture = (double)apertureEntry.Value.Numerator / apertureEntry.Value.Denominator;
}
entry = exifStructure.GetEntry(0, (ushort)ExifEntryTag.ShutterSpeedValue) as RationalIFDEntry;
if (entry is not null)
if (exifStructure.GetEntry(0, (ushort)ExifEntryTag.ShutterSpeedValue) is RationalIFDEntry shutterSpeedEntry)
{
item.ShutterSpeed = (double)entry.Value.Numerator / entry.Value.Denominator;
item.ShutterSpeed = (double)shutterSpeedEntry.Value.Numerator / shutterSpeedEntry.Value.Denominator;
}
}
}
@ -99,8 +95,7 @@ namespace Emby.Photos
item.Width = image.Properties.PhotoWidth;
item.Height = image.Properties.PhotoHeight;
var rating = image.ImageTag.Rating;
item.CommunityRating = rating.HasValue ? rating : null;
item.CommunityRating = image.ImageTag.Rating;
item.Overview = image.ImageTag.Comment;
@ -148,7 +143,6 @@ namespace Emby.Photos
}
}
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Image Provider - Error reading image tag for {0}", item.Path);
@ -179,4 +173,3 @@ namespace Emby.Photos
return Task.FromResult(Result);
}
}
}