jellyfin/Emby.Photos/PhotoProvider.cs

182 lines
7.4 KiB
C#
Raw Normal View History

using System;
2016-10-31 00:42:14 -07:00
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2017-09-22 13:33:01 -07:00
using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.Entities;
2014-02-12 22:11:54 -07:00
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Drawing;
2014-02-12 22:11:54 -07:00
using MediaBrowser.Model.Entities;
using Microsoft.Extensions.Logging;
using TagLib;
using TagLib.IFD;
using TagLib.IFD.Entries;
using TagLib.IFD.Tags;
2014-02-12 22:11:54 -07:00
namespace Emby.Photos
2014-02-12 22:11:54 -07:00
{
/// <summary>
/// Metadata provider for photos.
/// </summary>
2018-09-12 10:26:21 -07:00
public class PhotoProvider : ICustomMetadataProvider<Photo>, IForcedProvider, IHasItemChangeMonitor
2014-02-12 22:11:54 -07:00
{
private readonly ILogger _logger;
2019-05-11 02:55:41 -07:00
private readonly IImageProcessor _imageProcessor;
// These are causing taglib to hang
private readonly string[] _includeExtensions = new string[] { ".jpg", ".jpeg", ".png", ".tiff", ".cr2" };
2014-02-12 22:11:54 -07:00
/// <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)
2014-02-12 22:11:54 -07:00
{
_logger = logger;
2017-09-22 13:33:01 -07:00
_imageProcessor = imageProcessor;
2014-02-12 22:11:54 -07:00
}
/// <inheritdoc />
2019-05-11 02:55:41 -07:00
public string Name => "Embedded Information";
/// <inheritdoc />
2018-09-12 10:26:21 -07:00
public bool HasChanged(BaseItem item, IDirectoryService directoryService)
{
if (item.IsFileProtocol)
{
var file = directoryService.GetFile(item.Path);
return file != null && file.LastWriteTimeUtc != item.DateModified;
2018-09-12 10:26:21 -07:00
}
return false;
}
/// <inheritdoc />
2014-06-09 12:16:14 -07:00
public Task<ItemUpdateType> FetchAsync(Photo item, MetadataRefreshOptions options, CancellationToken cancellationToken)
2014-02-12 22:11:54 -07:00
{
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), StringComparer.OrdinalIgnoreCase))
2014-02-12 22:11:54 -07:00
{
2017-09-22 13:33:01 -07:00
try
{
using (var file = TagLib.File.Create(item.Path))
{
2019-05-11 02:55:41 -07:00
if (file.GetTag(TagTypes.TiffIFD) is IFDTag tag)
{
var structure = tag.Structure;
2019-05-11 02:55:41 -07:00
if (structure != null
&& structure.GetEntry(0, (ushort)IFDEntryTag.ExifIFD) is SubIFDEntry exif)
{
2019-05-11 02:55:41 -07:00
var exifStructure = exif.Structure;
if (exifStructure != null)
{
2019-05-11 02:55:41 -07:00
var entry = exifStructure.GetEntry(0, (ushort)ExifEntryTag.ApertureValue) as RationalIFDEntry;
if (entry != null)
{
item.Aperture = (double)entry.Value.Numerator / entry.Value.Denominator;
}
2015-09-14 10:39:35 -07:00
2019-05-11 02:55:41 -07:00
entry = exifStructure.GetEntry(0, (ushort)ExifEntryTag.ShutterSpeedValue) as RationalIFDEntry;
if (entry != null)
2015-09-14 10:39:35 -07:00
{
2019-05-11 02:55:41 -07:00
item.ShutterSpeed = (double)entry.Value.Numerator / entry.Value.Denominator;
2015-09-14 10:39:35 -07:00
}
}
}
}
2014-02-12 22:11:54 -07:00
2019-05-11 02:55:41 -07:00
if (file is TagLib.Image.File image)
{
item.CameraMake = image.ImageTag.Make;
item.CameraModel = image.ImageTag.Model;
2015-03-12 07:51:41 -07:00
item.Width = image.Properties.PhotoWidth;
item.Height = image.Properties.PhotoHeight;
2014-02-19 09:24:06 -07:00
var rating = image.ImageTag.Rating;
item.CommunityRating = rating.HasValue ? rating : null;
2014-02-12 22:11:54 -07:00
item.Overview = image.ImageTag.Comment;
2019-05-11 02:55:41 -07:00
if (!string.IsNullOrWhiteSpace(image.ImageTag.Title)
&& !item.LockedFields.Contains(MetadataFields.Name))
{
2019-05-11 02:55:41 -07:00
item.Name = image.ImageTag.Title;
}
2014-02-12 22:11:54 -07:00
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;
2015-09-14 10:39:35 -07:00
if (image.ImageTag.Orientation == TagLib.Image.ImageOrientation.None)
{
item.Orientation = null;
}
2019-05-11 02:55:41 -07:00
else if (Enum.TryParse(image.ImageTag.Orientation.ToString(), true, out ImageOrientation orientation))
{
2019-05-11 02:55:41 -07:00
item.Orientation = orientation;
}
item.ExposureTime = image.ImageTag.ExposureTime;
item.FocalLength = image.ImageTag.FocalLength;
2014-08-30 07:26:29 -07:00
item.Latitude = image.ImageTag.Latitude;
item.Longitude = image.ImageTag.Longitude;
item.Altitude = image.ImageTag.Altitude;
2014-08-30 07:26:29 -07:00
if (image.ImageTag.ISOSpeedRatings.HasValue)
{
item.IsoSpeedRating = Convert.ToInt32(image.ImageTag.ISOSpeedRatings.Value);
}
else
{
item.IsoSpeedRating = null;
2017-09-22 13:33:01 -07:00
}
2017-05-14 11:55:05 -07:00
}
2015-09-14 10:39:35 -07:00
}
2014-08-30 07:26:29 -07:00
}
2018-12-20 05:11:26 -07:00
catch (Exception ex)
2017-09-22 13:33:01 -07:00
{
2018-12-20 05:11:26 -07:00
_logger.LogError(ex, "Image Provider - Error reading image tag for {0}", item.Path);
2017-09-22 13:33:01 -07:00
}
}
2017-09-22 13:33:01 -07:00
2018-09-12 10:26:21 -07:00
if (item.Width <= 0 || item.Height <= 0)
{
2017-10-21 23:22:43 -07:00
var img = item.GetImageInfo(ImageType.Primary, 0);
2017-09-22 13:33:01 -07:00
2018-09-12 10:26:21 -07:00
try
{
var size = _imageProcessor.GetImageDimensions(item, img);
2018-09-12 10:26:21 -07:00
if (size.Width > 0 && size.Height > 0)
{
item.Width = size.Width;
item.Height = size.Height;
2018-09-12 10:26:21 -07:00
}
}
catch (ArgumentException)
2017-10-21 23:22:43 -07:00
{
2018-09-12 10:26:21 -07:00
// format not supported
2017-10-21 23:22:43 -07:00
}
}
2014-02-12 22:11:54 -07:00
2019-09-01 09:39:23 -07:00
const ItemUpdateType Result = ItemUpdateType.ImageUpdate | ItemUpdateType.MetadataImport;
return Task.FromResult(Result);
2014-02-12 22:11:54 -07:00
}
}
}