From de750ac8a20d0517bff4ce4c5a97d052ecdffa4c Mon Sep 17 00:00:00 2001 From: Peaches_MLG Date: Thu, 7 Mar 2024 19:19:00 +0000 Subject: [PATCH] Cleanup PhotoProvider.cs using new .NET 8 features --- Emby.Photos/PhotoProvider.cs | 241 +++++++++++++++++------------------ 1 file changed, 117 insertions(+), 124 deletions(-) diff --git a/Emby.Photos/PhotoProvider.cs b/Emby.Photos/PhotoProvider.cs index 27329a7f2f..e2f1ca813c 100644 --- a/Emby.Photos/PhotoProvider.cs +++ b/Emby.Photos/PhotoProvider.cs @@ -16,167 +16,160 @@ using TagLib.IFD; using TagLib.IFD.Entries; using TagLib.IFD.Tags; -namespace Emby.Photos +namespace Emby.Photos; + +/// +/// Metadata provider for photos. +/// +public class PhotoProvider : ICustomMetadataProvider, IForcedProvider, IHasItemChangeMonitor { + private readonly ILogger _logger; + private readonly IImageProcessor _imageProcessor; + + // These are causing taglib to hang + private readonly string[] _includeExtensions = [".jpg", ".jpeg", ".png", ".tiff", ".cr2", ".webp", ".avif"]; + /// - /// Metadata provider for photos. + /// Initializes a new instance of the class. /// - public class PhotoProvider : ICustomMetadataProvider, IForcedProvider, IHasItemChangeMonitor + /// The logger. + /// The image processor. + public PhotoProvider(ILogger logger, IImageProcessor imageProcessor) { - private readonly ILogger _logger; - private readonly IImageProcessor _imageProcessor; - - // These are causing taglib to hang - private readonly string[] _includeExtensions = new string[] { ".jpg", ".jpeg", ".png", ".tiff", ".cr2", ".webp", ".avif" }; - - /// - /// Initializes a new instance of the class. - /// - /// The logger. - /// The image processor. - public PhotoProvider(ILogger logger, IImageProcessor imageProcessor) - { - _logger = logger; - _imageProcessor = imageProcessor; - } + _logger = logger; + _imageProcessor = imageProcessor; + } - /// - public string Name => "Embedded Information"; + /// + public string Name => "Embedded Information"; - /// - public bool HasChanged(BaseItem item, IDirectoryService directoryService) + /// + public bool HasChanged(BaseItem item, IDirectoryService directoryService) + { + if (item.IsFileProtocol) { - if (item.IsFileProtocol) - { - var file = directoryService.GetFile(item.Path); - return file is not null && file.LastWriteTimeUtc != item.DateModified; - } - - return false; + var file = directoryService.GetFile(item.Path); + return file is not null && file.LastWriteTimeUtc != item.DateModified; } - /// - public Task FetchAsync(Photo item, MetadataRefreshOptions options, CancellationToken cancellationToken) - { - item.SetImagePath(ImageType.Primary, item.Path); + return false; + } - // Examples: https://github.com/mono/taglib-sharp/blob/a5f6949a53d09ce63ee7495580d6802921a21f14/tests/fixtures/TagLib.Tests.Images/NullOrientationTest.cs - if (_includeExtensions.Contains(Path.GetExtension(item.Path.AsSpan()), StringComparison.OrdinalIgnoreCase)) + /// + public Task FetchAsync(Photo item, MetadataRefreshOptions options, CancellationToken cancellationToken) + { + item.SetImagePath(ImageType.Primary, item.Path); + + // Examples: https://github.com/mono/taglib-sharp/blob/a5f6949a53d09ce63ee7495580d6802921a21f14/tests/fixtures/TagLib.Tests.Images/NullOrientationTest.cs + if (_includeExtensions.Contains(Path.GetExtension(item.Path.AsSpan()), StringComparison.OrdinalIgnoreCase)) + { + try { - try + using var file = TagLib.File.Create(item.Path); + if (file.GetTag(TagTypes.TiffIFD) is IFDTag tag) { - using (var file = TagLib.File.Create(item.Path)) + var structure = tag.Structure; + if (structure?.GetEntry(0, (ushort)IFDEntryTag.ExifIFD) is SubIFDEntry exif) { - if (file.GetTag(TagTypes.TiffIFD) is IFDTag tag) + var exifStructure = exif.Structure; + if (exifStructure is not null) { - var structure = tag.Structure; - if (structure is not null - && structure.GetEntry(0, (ushort)IFDEntryTag.ExifIFD) is SubIFDEntry exif) + if (exifStructure.GetEntry(0, (ushort)ExifEntryTag.ApertureValue) is RationalIFDEntry apertureEntry) { - var exifStructure = exif.Structure; - if (exifStructure is not null) - { - var entry = exifStructure.GetEntry(0, (ushort)ExifEntryTag.ApertureValue) as RationalIFDEntry; - if (entry is not null) - { - item.Aperture = (double)entry.Value.Numerator / entry.Value.Denominator; - } - - entry = exifStructure.GetEntry(0, (ushort)ExifEntryTag.ShutterSpeedValue) as RationalIFDEntry; - if (entry is not null) - { - item.ShutterSpeed = (double)entry.Value.Numerator / entry.Value.Denominator; - } - } + item.Aperture = (double)apertureEntry.Value.Numerator / apertureEntry.Value.Denominator; + } + + if (exifStructure.GetEntry(0, (ushort)ExifEntryTag.ShutterSpeedValue) is RationalIFDEntry shutterSpeedEntry) + { + item.ShutterSpeed = (double)shutterSpeedEntry.Value.Numerator / shutterSpeedEntry.Value.Denominator; } } + } + } - if (file is TagLib.Image.File image) - { - item.CameraMake = image.ImageTag.Make; - item.CameraModel = image.ImageTag.Model; + if (file is TagLib.Image.File image) + { + item.CameraMake = image.ImageTag.Make; + item.CameraModel = image.ImageTag.Model; - item.Width = image.Properties.PhotoWidth; - item.Height = image.Properties.PhotoHeight; + 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; + item.Overview = image.ImageTag.Comment; - if (!string.IsNullOrWhiteSpace(image.ImageTag.Title) - && !item.LockedFields.Contains(MetadataField.Name)) - { - item.Name = image.ImageTag.Title; - } + if (!string.IsNullOrWhiteSpace(image.ImageTag.Title) + && !item.LockedFields.Contains(MetadataField.Name)) + { + item.Name = image.ImageTag.Title; + } - var dateTaken = image.ImageTag.DateTime; - if (dateTaken.HasValue) - { - item.DateCreated = dateTaken.Value; - item.PremiereDate = dateTaken.Value; - item.ProductionYear = dateTaken.Value.Year; - } + var dateTaken = image.ImageTag.DateTime; + if (dateTaken.HasValue) + { + item.DateCreated = dateTaken.Value; + item.PremiereDate = dateTaken.Value; + item.ProductionYear = dateTaken.Value.Year; + } - item.Genres = image.ImageTag.Genres; - item.Tags = image.ImageTag.Keywords; - item.Software = image.ImageTag.Software; + item.Genres = image.ImageTag.Genres; + item.Tags = image.ImageTag.Keywords; + item.Software = image.ImageTag.Software; - if (image.ImageTag.Orientation == TagLib.Image.ImageOrientation.None) - { - item.Orientation = null; - } - else if (Enum.TryParse(image.ImageTag.Orientation.ToString(), true, out ImageOrientation orientation)) - { - item.Orientation = orientation; - } + if (image.ImageTag.Orientation == TagLib.Image.ImageOrientation.None) + { + item.Orientation = null; + } + else if (Enum.TryParse(image.ImageTag.Orientation.ToString(), true, out ImageOrientation orientation)) + { + item.Orientation = orientation; + } - item.ExposureTime = image.ImageTag.ExposureTime; - item.FocalLength = image.ImageTag.FocalLength; + item.ExposureTime = image.ImageTag.ExposureTime; + item.FocalLength = image.ImageTag.FocalLength; - item.Latitude = image.ImageTag.Latitude; - item.Longitude = image.ImageTag.Longitude; - item.Altitude = image.ImageTag.Altitude; + item.Latitude = image.ImageTag.Latitude; + item.Longitude = image.ImageTag.Longitude; + item.Altitude = image.ImageTag.Altitude; - if (image.ImageTag.ISOSpeedRatings.HasValue) - { - item.IsoSpeedRating = Convert.ToInt32(image.ImageTag.ISOSpeedRatings.Value); - } - else - { - item.IsoSpeedRating = null; - } - } + if (image.ImageTag.ISOSpeedRatings.HasValue) + { + item.IsoSpeedRating = Convert.ToInt32(image.ImageTag.ISOSpeedRatings.Value); + } + else + { + item.IsoSpeedRating = null; } - } - catch (Exception ex) - { - _logger.LogError(ex, "Image Provider - Error reading image tag for {0}", item.Path); } } - - if (item.Width <= 0 || item.Height <= 0) + catch (Exception ex) { - var img = item.GetImageInfo(ImageType.Primary, 0); + _logger.LogError(ex, "Image Provider - Error reading image tag for {0}", item.Path); + } + } - try - { - var size = _imageProcessor.GetImageDimensions(item, img); + if (item.Width <= 0 || item.Height <= 0) + { + var img = item.GetImageInfo(ImageType.Primary, 0); - if (size.Width > 0 && size.Height > 0) - { - item.Width = size.Width; - item.Height = size.Height; - } - } - catch (ArgumentException) + try + { + var size = _imageProcessor.GetImageDimensions(item, img); + + if (size.Width > 0 && size.Height > 0) { - // format not supported + item.Width = size.Width; + item.Height = size.Height; } } - - const ItemUpdateType Result = ItemUpdateType.ImageUpdate | ItemUpdateType.MetadataImport; - return Task.FromResult(Result); + catch (ArgumentException) + { + // format not supported + } } + + const ItemUpdateType Result = ItemUpdateType.ImageUpdate | ItemUpdateType.MetadataImport; + return Task.FromResult(Result); } }