diff options
Diffstat (limited to 'Emby.Photos/PhotoProvider.cs')
| -rw-r--r-- | Emby.Photos/PhotoProvider.cs | 108 |
1 files changed, 44 insertions, 64 deletions
diff --git a/Emby.Photos/PhotoProvider.cs b/Emby.Photos/PhotoProvider.cs index a4179e660..4071e4e54 100644 --- a/Emby.Photos/PhotoProvider.cs +++ b/Emby.Photos/PhotoProvider.cs @@ -17,86 +17,80 @@ using TagLib.IFD.Tags; namespace Emby.Photos { + /// <summary> + /// Metadata provider for photos. + /// </summary> public class PhotoProvider : ICustomMetadataProvider<Photo>, IForcedProvider, IHasItemChangeMonitor { - private readonly ILogger _logger; - private IImageProcessor _imageProcessor; + private readonly ILogger<PhotoProvider> _logger; + private readonly IImageProcessor _imageProcessor; - public PhotoProvider(ILogger logger, IImageProcessor imageProcessor) + // These are causing taglib to hang + private readonly string[] _includeExtensions = new string[] { ".jpg", ".jpeg", ".png", ".tiff", ".cr2" }; + + /// <summary> + /// Initializes a new instance of the <see cref="PhotoProvider" /> class. + /// </summary> + /// <param name="logger">The logger.</param> + /// <param name="imageProcessor">The image processor.</param> + public PhotoProvider(ILogger<PhotoProvider> logger, IImageProcessor imageProcessor) { _logger = logger; _imageProcessor = imageProcessor; } + /// <inheritdoc /> + public string Name => "Embedded Information"; + + /// <inheritdoc /> public bool HasChanged(BaseItem item, IDirectoryService directoryService) { if (item.IsFileProtocol) { var file = directoryService.GetFile(item.Path); - if (file != null && file.LastWriteTimeUtc != item.DateModified) - { - return true; - } + return file != null && file.LastWriteTimeUtc != item.DateModified; } return false; } - // These are causing taglib to hang - private string[] _includextensions = new string[] { ".jpg", ".jpeg", ".png", ".tiff", ".cr2" }; - + /// <inheritdoc /> public Task<ItemUpdateType> 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 (_includextensions.Contains(Path.GetExtension(item.Path) ?? string.Empty, StringComparer.OrdinalIgnoreCase)) + if (_includeExtensions.Contains(Path.GetExtension(item.Path), StringComparer.OrdinalIgnoreCase)) { try { using (var file = TagLib.File.Create(item.Path)) { - var image = file as TagLib.Image.File; - - var tag = file.GetTag(TagTypes.TiffIFD) as IFDTag; - - if (tag != null) + if (file.GetTag(TagTypes.TiffIFD) is IFDTag tag) { var structure = tag.Structure; - - if (structure != null) + if (structure != null + && structure.GetEntry(0, (ushort)IFDEntryTag.ExifIFD) is SubIFDEntry exif) { - var exif = structure.GetEntry(0, (ushort)IFDEntryTag.ExifIFD) as SubIFDEntry; - - if (exif != null) + var exifStructure = exif.Structure; + if (exifStructure != null) { - var exifStructure = exif.Structure; + var entry = exifStructure.GetEntry(0, (ushort)ExifEntryTag.ApertureValue) as RationalIFDEntry; + if (entry != null) + { + item.Aperture = (double)entry.Value.Numerator / entry.Value.Denominator; + } - if (exifStructure != null) + entry = exifStructure.GetEntry(0, (ushort)ExifEntryTag.ShutterSpeedValue) as RationalIFDEntry; + if (entry != null) { - var entry = exifStructure.GetEntry(0, (ushort)ExifEntryTag.ApertureValue) as RationalIFDEntry; - - if (entry != null) - { - double val = entry.Value.Numerator; - val /= entry.Value.Denominator; - item.Aperture = val; - } - - entry = exifStructure.GetEntry(0, (ushort)ExifEntryTag.ShutterSpeedValue) as RationalIFDEntry; - - if (entry != null) - { - double val = entry.Value.Numerator; - val /= entry.Value.Denominator; - item.ShutterSpeed = val; - } + item.ShutterSpeed = (double)entry.Value.Numerator / entry.Value.Denominator; } } } } - if (image != null) + if (file is TagLib.Image.File image) { item.CameraMake = image.ImageTag.Make; item.CameraModel = image.ImageTag.Model; @@ -105,23 +99,14 @@ namespace Emby.Photos item.Height = image.Properties.PhotoHeight; var rating = image.ImageTag.Rating; - if (rating.HasValue) - { - item.CommunityRating = rating; - } - else - { - item.CommunityRating = null; - } + item.CommunityRating = rating.HasValue ? rating : null; item.Overview = image.ImageTag.Comment; - if (!string.IsNullOrWhiteSpace(image.ImageTag.Title)) + if (!string.IsNullOrWhiteSpace(image.ImageTag.Title) + && !item.LockedFields.Contains(MetadataField.Name)) { - if (!item.LockedFields.Contains(MetadataFields.Name)) - { - item.Name = image.ImageTag.Title; - } + item.Name = image.ImageTag.Title; } var dateTaken = image.ImageTag.DateTime; @@ -140,12 +125,9 @@ namespace Emby.Photos { item.Orientation = null; } - else + else if (Enum.TryParse(image.ImageTag.Orientation.ToString(), true, out ImageOrientation orientation)) { - if (Enum.TryParse(image.ImageTag.Orientation.ToString(), true, out ImageOrientation orientation)) - { - item.Orientation = orientation; - } + item.Orientation = orientation; } item.ExposureTime = image.ImageTag.ExposureTime; @@ -178,7 +160,7 @@ namespace Emby.Photos try { - var size = _imageProcessor.GetImageDimensions(item, img, false); + var size = _imageProcessor.GetImageDimensions(item, img); if (size.Width > 0 && size.Height > 0) { @@ -192,10 +174,8 @@ namespace Emby.Photos } } - const ItemUpdateType result = ItemUpdateType.ImageUpdate | ItemUpdateType.MetadataImport; - return Task.FromResult(result); + const ItemUpdateType Result = ItemUpdateType.ImageUpdate | ItemUpdateType.MetadataImport; + return Task.FromResult(Result); } - - public string Name => "Embedded Information"; } } |
