Cleanup PhotoProvider.cs using new .NET 8 features
This commit is contained in:
parent
a597f1e410
commit
de750ac8a2
|
@ -16,167 +16,160 @@ using TagLib.IFD;
|
||||||
using TagLib.IFD.Entries;
|
using TagLib.IFD.Entries;
|
||||||
using TagLib.IFD.Tags;
|
using TagLib.IFD.Tags;
|
||||||
|
|
||||||
namespace Emby.Photos
|
namespace Emby.Photos;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Metadata provider for photos.
|
||||||
|
/// </summary>
|
||||||
|
public class PhotoProvider : ICustomMetadataProvider<Photo>, IForcedProvider, IHasItemChangeMonitor
|
||||||
{
|
{
|
||||||
|
private readonly ILogger<PhotoProvider> _logger;
|
||||||
|
private readonly IImageProcessor _imageProcessor;
|
||||||
|
|
||||||
|
// These are causing taglib to hang
|
||||||
|
private readonly string[] _includeExtensions = [".jpg", ".jpeg", ".png", ".tiff", ".cr2", ".webp", ".avif"];
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Metadata provider for photos.
|
/// Initializes a new instance of the <see cref="PhotoProvider" /> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class PhotoProvider : ICustomMetadataProvider<Photo>, IForcedProvider, IHasItemChangeMonitor
|
/// <param name="logger">The logger.</param>
|
||||||
|
/// <param name="imageProcessor">The image processor.</param>
|
||||||
|
public PhotoProvider(ILogger<PhotoProvider> logger, IImageProcessor imageProcessor)
|
||||||
{
|
{
|
||||||
private readonly ILogger<PhotoProvider> _logger;
|
_logger = logger;
|
||||||
private readonly IImageProcessor _imageProcessor;
|
_imageProcessor = imageProcessor;
|
||||||
|
}
|
||||||
|
|
||||||
// These are causing taglib to hang
|
/// <inheritdoc />
|
||||||
private readonly string[] _includeExtensions = new string[] { ".jpg", ".jpeg", ".png", ".tiff", ".cr2", ".webp", ".avif" };
|
public string Name => "Embedded Information";
|
||||||
|
|
||||||
/// <summary>
|
/// <inheritdoc />
|
||||||
/// Initializes a new instance of the <see cref="PhotoProvider" /> class.
|
public bool HasChanged(BaseItem item, IDirectoryService directoryService)
|
||||||
/// </summary>
|
{
|
||||||
/// <param name="logger">The logger.</param>
|
if (item.IsFileProtocol)
|
||||||
/// <param name="imageProcessor">The image processor.</param>
|
|
||||||
public PhotoProvider(ILogger<PhotoProvider> logger, IImageProcessor imageProcessor)
|
|
||||||
{
|
{
|
||||||
_logger = logger;
|
var file = directoryService.GetFile(item.Path);
|
||||||
_imageProcessor = imageProcessor;
|
return file is not null && file.LastWriteTimeUtc != item.DateModified;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
return false;
|
||||||
public string Name => "Embedded Information";
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public bool HasChanged(BaseItem item, IDirectoryService directoryService)
|
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 (_includeExtensions.Contains(Path.GetExtension(item.Path.AsSpan()), StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
if (item.IsFileProtocol)
|
try
|
||||||
{
|
{
|
||||||
var file = directoryService.GetFile(item.Path);
|
using var file = TagLib.File.Create(item.Path);
|
||||||
return file is not null && file.LastWriteTimeUtc != item.DateModified;
|
if (file.GetTag(TagTypes.TiffIFD) is IFDTag tag)
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <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 (_includeExtensions.Contains(Path.GetExtension(item.Path.AsSpan()), StringComparison.OrdinalIgnoreCase))
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
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 (exifStructure.GetEntry(0, (ushort)ExifEntryTag.ApertureValue) is RationalIFDEntry apertureEntry)
|
||||||
if (structure is not null
|
|
||||||
&& structure.GetEntry(0, (ushort)IFDEntryTag.ExifIFD) is SubIFDEntry exif)
|
|
||||||
{
|
{
|
||||||
var exifStructure = exif.Structure;
|
item.Aperture = (double)apertureEntry.Value.Numerator / apertureEntry.Value.Denominator;
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
var rating = image.ImageTag.Rating;
|
|
||||||
item.CommunityRating = rating.HasValue ? rating : null;
|
|
||||||
|
|
||||||
item.Overview = image.ImageTag.Comment;
|
|
||||||
|
|
||||||
if (!string.IsNullOrWhiteSpace(image.ImageTag.Title)
|
|
||||||
&& !item.LockedFields.Contains(MetadataField.Name))
|
|
||||||
{
|
|
||||||
item.Name = image.ImageTag.Title;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var dateTaken = image.ImageTag.DateTime;
|
if (exifStructure.GetEntry(0, (ushort)ExifEntryTag.ShutterSpeedValue) is RationalIFDEntry shutterSpeedEntry)
|
||||||
if (dateTaken.HasValue)
|
|
||||||
{
|
{
|
||||||
item.DateCreated = dateTaken.Value;
|
item.ShutterSpeed = (double)shutterSpeedEntry.Value.Numerator / shutterSpeedEntry.Value.Denominator;
|
||||||
item.PremiereDate = dateTaken.Value;
|
|
||||||
item.ProductionYear = dateTaken.Value.Year;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
if (image.ImageTag.ISOSpeedRatings.HasValue)
|
|
||||||
{
|
|
||||||
item.IsoSpeedRating = Convert.ToInt32(image.ImageTag.ISOSpeedRatings.Value);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
item.IsoSpeedRating = null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
|
if (file is TagLib.Image.File image)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "Image Provider - Error reading image tag for {0}", item.Path);
|
item.CameraMake = image.ImageTag.Make;
|
||||||
}
|
item.CameraModel = image.ImageTag.Model;
|
||||||
}
|
|
||||||
|
|
||||||
if (item.Width <= 0 || item.Height <= 0)
|
item.Width = image.Properties.PhotoWidth;
|
||||||
{
|
item.Height = image.Properties.PhotoHeight;
|
||||||
var img = item.GetImageInfo(ImageType.Primary, 0);
|
|
||||||
|
|
||||||
try
|
item.CommunityRating = image.ImageTag.Rating;
|
||||||
{
|
|
||||||
var size = _imageProcessor.GetImageDimensions(item, img);
|
|
||||||
|
|
||||||
if (size.Width > 0 && size.Height > 0)
|
item.Overview = image.ImageTag.Comment;
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(image.ImageTag.Title)
|
||||||
|
&& !item.LockedFields.Contains(MetadataField.Name))
|
||||||
{
|
{
|
||||||
item.Width = size.Width;
|
item.Name = image.ImageTag.Title;
|
||||||
item.Height = size.Height;
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
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.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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (ArgumentException)
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Image Provider - Error reading image tag for {0}", item.Path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (item.Width <= 0 || item.Height <= 0)
|
||||||
|
{
|
||||||
|
var img = item.GetImageInfo(ImageType.Primary, 0);
|
||||||
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (ArgumentException)
|
||||||
const ItemUpdateType Result = ItemUpdateType.ImageUpdate | ItemUpdateType.MetadataImport;
|
{
|
||||||
return Task.FromResult(Result);
|
// format not supported
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const ItemUpdateType Result = ItemUpdateType.ImageUpdate | ItemUpdateType.MetadataImport;
|
||||||
|
return Task.FromResult(Result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user