< Summary - Jellyfin

Information
Class: Emby.Photos.PhotoProvider
Assembly: Emby.Photos
File(s): /srv/git/jellyfin/Emby.Photos/PhotoProvider.cs
Line coverage
6%
Covered lines: 4
Uncovered lines: 61
Coverable lines: 65
Total lines: 175
Line coverage: 6.1%
Branch coverage
0%
Covered branches: 0
Total branches: 42
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_Name()100%210%
HasChanged(...)0%2040%
FetchAsync(...)0%1482380%

File(s)

/srv/git/jellyfin/Emby.Photos/PhotoProvider.cs

#LineLine coverage
 1using System;
 2using System.IO;
 3using System.Linq;
 4using System.Threading;
 5using System.Threading.Tasks;
 6using Jellyfin.Extensions;
 7using MediaBrowser.Controller.Drawing;
 8using MediaBrowser.Controller.Entities;
 9using MediaBrowser.Controller.Library;
 10using MediaBrowser.Controller.Providers;
 11using MediaBrowser.Model.Drawing;
 12using MediaBrowser.Model.Entities;
 13using Microsoft.Extensions.Logging;
 14using TagLib;
 15using TagLib.IFD;
 16using TagLib.IFD.Entries;
 17using TagLib.IFD.Tags;
 18
 19namespace Emby.Photos;
 20
 21/// <summary>
 22/// Metadata provider for photos.
 23/// </summary>
 24public class PhotoProvider : ICustomMetadataProvider<Photo>, IForcedProvider, IHasItemChangeMonitor
 25{
 26    private readonly ILogger<PhotoProvider> _logger;
 27    private readonly IImageProcessor _imageProcessor;
 28
 29    // Other extensions might cause taglib to hang
 2230    private readonly string[] _includeExtensions = [".jpg", ".jpeg", ".png", ".tiff", ".cr2", ".webp", ".avif"];
 31
 32    /// <summary>
 33    /// Initializes a new instance of the <see cref="PhotoProvider" /> class.
 34    /// </summary>
 35    /// <param name="logger">The logger.</param>
 36    /// <param name="imageProcessor">The image processor.</param>
 37    public PhotoProvider(ILogger<PhotoProvider> logger, IImageProcessor imageProcessor)
 38    {
 2239        _logger = logger;
 2240        _imageProcessor = imageProcessor;
 2241    }
 42
 43    /// <inheritdoc />
 044    public string Name => "Embedded Information";
 45
 46    /// <inheritdoc />
 47    public bool HasChanged(BaseItem item, IDirectoryService directoryService)
 48    {
 049        if (item.IsFileProtocol)
 50        {
 051            var file = directoryService.GetFile(item.Path);
 052            return file is not null && file.LastWriteTimeUtc != item.DateModified;
 53        }
 54
 055        return false;
 56    }
 57
 58    /// <inheritdoc />
 59    public Task<ItemUpdateType> FetchAsync(Photo item, MetadataRefreshOptions options, CancellationToken cancellationTok
 60    {
 061        item.SetImagePath(ImageType.Primary, item.Path);
 62
 63        // Examples: https://github.com/mono/taglib-sharp/blob/a5f6949a53d09ce63ee7495580d6802921a21f14/tests/fixtures/T
 064        if (_includeExtensions.Contains(Path.GetExtension(item.Path.AsSpan()), StringComparison.OrdinalIgnoreCase))
 65        {
 66            try
 67            {
 068                using var file = TagLib.File.Create(item.Path);
 069                if (file.GetTag(TagTypes.TiffIFD) is IFDTag tag)
 70                {
 071                    var structure = tag.Structure;
 072                    if (structure?.GetEntry(0, (ushort)IFDEntryTag.ExifIFD) is SubIFDEntry exif)
 73                    {
 074                        var exifStructure = exif.Structure;
 075                        if (exifStructure is not null)
 76                        {
 077                            if (exifStructure.GetEntry(0, (ushort)ExifEntryTag.ApertureValue) is RationalIFDEntry apertu
 78                            {
 079                                item.Aperture = (double)apertureEntry.Value.Numerator / apertureEntry.Value.Denominator;
 80                            }
 81
 082                            if (exifStructure.GetEntry(0, (ushort)ExifEntryTag.ShutterSpeedValue) is RationalIFDEntry sh
 83                            {
 084                                item.ShutterSpeed = (double)shutterSpeedEntry.Value.Numerator / shutterSpeedEntry.Value.
 85                            }
 86                        }
 87                    }
 88                }
 89
 090                if (file is TagLib.Image.File image)
 91                {
 092                    item.CameraMake = image.ImageTag.Make;
 093                    item.CameraModel = image.ImageTag.Model;
 94
 095                    item.Width = image.Properties.PhotoWidth;
 096                    item.Height = image.Properties.PhotoHeight;
 97
 098                    item.CommunityRating = image.ImageTag.Rating;
 99
 0100                    item.Overview = image.ImageTag.Comment;
 101
 0102                    if (!string.IsNullOrWhiteSpace(image.ImageTag.Title)
 0103                        && !item.LockedFields.Contains(MetadataField.Name))
 104                    {
 0105                        item.Name = image.ImageTag.Title;
 106                    }
 107
 0108                    var dateTaken = image.ImageTag.DateTime;
 0109                    if (dateTaken.HasValue)
 110                    {
 0111                        item.DateCreated = dateTaken.Value;
 0112                        item.PremiereDate = dateTaken.Value;
 0113                        item.ProductionYear = dateTaken.Value.Year;
 114                    }
 115
 0116                    item.Genres = image.ImageTag.Genres;
 0117                    item.Tags = image.ImageTag.Keywords;
 0118                    item.Software = image.ImageTag.Software;
 119
 0120                    if (image.ImageTag.Orientation == TagLib.Image.ImageOrientation.None)
 121                    {
 0122                        item.Orientation = null;
 123                    }
 0124                    else if (Enum.TryParse(image.ImageTag.Orientation.ToString(), true, out ImageOrientation orientation
 125                    {
 0126                        item.Orientation = orientation;
 127                    }
 128
 0129                    item.ExposureTime = image.ImageTag.ExposureTime;
 0130                    item.FocalLength = image.ImageTag.FocalLength;
 131
 0132                    item.Latitude = image.ImageTag.Latitude;
 0133                    item.Longitude = image.ImageTag.Longitude;
 0134                    item.Altitude = image.ImageTag.Altitude;
 135
 0136                    if (image.ImageTag.ISOSpeedRatings.HasValue)
 137                    {
 0138                        item.IsoSpeedRating = Convert.ToInt32(image.ImageTag.ISOSpeedRatings.Value);
 139                    }
 140                    else
 141                    {
 0142                        item.IsoSpeedRating = null;
 143                    }
 144                }
 0145            }
 0146            catch (Exception ex)
 147            {
 0148                _logger.LogError(ex, "Image Provider - Error reading image tag for {0}", item.Path);
 0149            }
 150        }
 151
 0152        if (item.Width <= 0 || item.Height <= 0)
 153        {
 0154            var img = item.GetImageInfo(ImageType.Primary, 0);
 155
 156            try
 157            {
 0158                var size = _imageProcessor.GetImageDimensions(item, img);
 159
 0160                if (size.Width > 0 && size.Height > 0)
 161                {
 0162                    item.Width = size.Width;
 0163                    item.Height = size.Height;
 164                }
 0165            }
 0166            catch (ArgumentException)
 167            {
 168                // format not supported
 0169            }
 170        }
 171
 172        const ItemUpdateType Result = ItemUpdateType.ImageUpdate | ItemUpdateType.MetadataImport;
 0173        return Task.FromResult(Result);
 174    }
 175}