2019-01-13 20:54:44 +01:00
|
|
|
using System;
|
2016-10-31 08:42:14 +01:00
|
|
|
using System.IO;
|
2016-10-26 20:25:03 +02:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2017-09-22 22:33:01 +02:00
|
|
|
using MediaBrowser.Controller.Drawing;
|
2016-10-26 20:25:03 +02:00
|
|
|
using MediaBrowser.Controller.Entities;
|
2014-02-13 06:11:54 +01:00
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2019-01-13 21:46:33 +01:00
|
|
|
using MediaBrowser.Model.Drawing;
|
2014-02-13 06:11:54 +01:00
|
|
|
using MediaBrowser.Model.Entities;
|
2016-10-31 08:42:14 +01:00
|
|
|
using MediaBrowser.Model.IO;
|
2018-12-13 14:18:25 +01:00
|
|
|
using Microsoft.Extensions.Logging;
|
2014-08-28 03:27:46 +02:00
|
|
|
using TagLib;
|
|
|
|
using TagLib.IFD;
|
|
|
|
using TagLib.IFD.Entries;
|
|
|
|
using TagLib.IFD.Tags;
|
2014-02-13 06:11:54 +01:00
|
|
|
|
2016-10-26 20:25:03 +02:00
|
|
|
namespace Emby.Photos
|
2014-02-13 06:11:54 +01:00
|
|
|
{
|
2018-09-12 19:26:21 +02:00
|
|
|
public class PhotoProvider : ICustomMetadataProvider<Photo>, IForcedProvider, IHasItemChangeMonitor
|
2014-02-13 06:11:54 +01:00
|
|
|
{
|
|
|
|
private readonly ILogger _logger;
|
2016-10-31 08:42:14 +01:00
|
|
|
private readonly IFileSystem _fileSystem;
|
2017-09-22 22:33:01 +02:00
|
|
|
private IImageProcessor _imageProcessor;
|
2014-02-13 06:11:54 +01:00
|
|
|
|
2017-09-22 22:33:01 +02:00
|
|
|
public PhotoProvider(ILogger logger, IFileSystem fileSystem, IImageProcessor imageProcessor)
|
2014-02-13 06:11:54 +01:00
|
|
|
{
|
|
|
|
_logger = logger;
|
2016-10-31 08:42:14 +01:00
|
|
|
_fileSystem = fileSystem;
|
2017-09-22 22:33:01 +02:00
|
|
|
_imageProcessor = imageProcessor;
|
2014-02-13 06:11:54 +01:00
|
|
|
}
|
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
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 false;
|
|
|
|
}
|
|
|
|
|
2017-09-22 22:33:01 +02:00
|
|
|
// These are causing taglib to hang
|
2018-09-12 19:26:21 +02:00
|
|
|
private string[] _includextensions = new string[] { ".jpg", ".jpeg", ".png", ".tiff", ".cr2" };
|
2017-09-22 22:33:01 +02:00
|
|
|
|
2014-06-09 21:16:14 +02:00
|
|
|
public Task<ItemUpdateType> FetchAsync(Photo item, MetadataRefreshOptions options, CancellationToken cancellationToken)
|
2014-02-13 06:11:54 +01:00
|
|
|
{
|
|
|
|
item.SetImagePath(ImageType.Primary, item.Path);
|
|
|
|
|
2014-08-28 03:27:46 +02:00
|
|
|
// Examples: https://github.com/mono/taglib-sharp/blob/a5f6949a53d09ce63ee7495580d6802921a21f14/tests/fixtures/TagLib.Tests.Images/NullOrientationTest.cs
|
2017-10-09 07:24:55 +02:00
|
|
|
if (_includextensions.Contains(Path.GetExtension(item.Path) ?? string.Empty, StringComparer.OrdinalIgnoreCase))
|
2014-02-13 06:11:54 +01:00
|
|
|
{
|
2017-09-22 22:33:01 +02:00
|
|
|
try
|
2014-08-28 03:27:46 +02:00
|
|
|
{
|
2018-12-11 14:55:33 +01:00
|
|
|
using (var file = TagLib.File.Create(item.Path))
|
2014-08-28 03:27:46 +02:00
|
|
|
{
|
2018-12-11 14:55:33 +01:00
|
|
|
var image = file as TagLib.Image.File;
|
|
|
|
|
|
|
|
var tag = file.GetTag(TagTypes.TiffIFD) as IFDTag;
|
2014-08-28 03:27:46 +02:00
|
|
|
|
2018-12-11 14:55:33 +01:00
|
|
|
if (tag != null)
|
|
|
|
{
|
|
|
|
var structure = tag.Structure;
|
2017-09-22 22:33:01 +02:00
|
|
|
|
2018-12-11 14:55:33 +01:00
|
|
|
if (structure != null)
|
2014-08-28 03:27:46 +02:00
|
|
|
{
|
2018-12-11 14:55:33 +01:00
|
|
|
var exif = structure.GetEntry(0, (ushort)IFDEntryTag.ExifIFD) as SubIFDEntry;
|
2014-08-28 03:27:46 +02:00
|
|
|
|
2018-12-11 14:55:33 +01:00
|
|
|
if (exif != null)
|
2014-08-28 03:27:46 +02:00
|
|
|
{
|
2018-12-11 14:55:33 +01:00
|
|
|
var exifStructure = exif.Structure;
|
2015-09-14 19:39:35 +02:00
|
|
|
|
2018-12-11 14:55:33 +01:00
|
|
|
if (exifStructure != null)
|
2015-09-14 19:39:35 +02:00
|
|
|
{
|
2018-12-11 14:55:33 +01:00
|
|
|
var entry = exifStructure.GetEntry(0, (ushort)ExifEntryTag.ApertureValue) as RationalIFDEntry;
|
2017-05-14 20:55:05 +02:00
|
|
|
|
2018-12-11 14:55:33 +01:00
|
|
|
if (entry != null)
|
2017-05-14 20:55:05 +02:00
|
|
|
{
|
2018-12-11 14:55:33 +01:00
|
|
|
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;
|
2017-05-14 20:55:05 +02:00
|
|
|
}
|
2015-09-14 19:39:35 +02:00
|
|
|
}
|
2014-08-28 03:27:46 +02:00
|
|
|
}
|
|
|
|
}
|
2018-12-11 14:55:33 +01:00
|
|
|
}
|
2014-02-13 06:11:54 +01:00
|
|
|
|
2018-12-11 14:55:33 +01:00
|
|
|
if (image != null)
|
|
|
|
{
|
|
|
|
item.CameraMake = image.ImageTag.Make;
|
|
|
|
item.CameraModel = image.ImageTag.Model;
|
2015-03-12 15:51:41 +01:00
|
|
|
|
2018-12-11 14:55:33 +01:00
|
|
|
item.Width = image.Properties.PhotoWidth;
|
|
|
|
item.Height = image.Properties.PhotoHeight;
|
2014-02-19 17:24:06 +01:00
|
|
|
|
2018-12-11 14:55:33 +01:00
|
|
|
var rating = image.ImageTag.Rating;
|
|
|
|
if (rating.HasValue)
|
|
|
|
{
|
|
|
|
item.CommunityRating = rating;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
item.CommunityRating = null;
|
|
|
|
}
|
2014-02-13 06:11:54 +01:00
|
|
|
|
2018-12-11 14:55:33 +01:00
|
|
|
item.Overview = image.ImageTag.Comment;
|
2014-08-28 03:27:46 +02:00
|
|
|
|
2018-12-11 14:55:33 +01:00
|
|
|
if (!string.IsNullOrWhiteSpace(image.ImageTag.Title))
|
|
|
|
{
|
|
|
|
if (!item.LockedFields.Contains(MetadataFields.Name))
|
2017-09-22 22:33:01 +02:00
|
|
|
{
|
2018-12-11 14:55:33 +01:00
|
|
|
item.Name = image.ImageTag.Title;
|
2017-09-22 22:33:01 +02:00
|
|
|
}
|
2018-12-11 14:55:33 +01:00
|
|
|
}
|
2014-02-13 06:11:54 +01:00
|
|
|
|
2018-12-11 14:55:33 +01:00
|
|
|
var dateTaken = image.ImageTag.DateTime;
|
|
|
|
if (dateTaken.HasValue)
|
|
|
|
{
|
|
|
|
item.DateCreated = dateTaken.Value;
|
|
|
|
item.PremiereDate = dateTaken.Value;
|
|
|
|
item.ProductionYear = dateTaken.Value.Year;
|
|
|
|
}
|
2014-08-28 03:27:46 +02:00
|
|
|
|
2018-12-11 14:55:33 +01:00
|
|
|
item.Genres = image.ImageTag.Genres;
|
|
|
|
item.Tags = image.ImageTag.Keywords;
|
|
|
|
item.Software = image.ImageTag.Software;
|
2015-09-14 19:39:35 +02:00
|
|
|
|
2018-12-11 14:55:33 +01:00
|
|
|
if (image.ImageTag.Orientation == TagLib.Image.ImageOrientation.None)
|
|
|
|
{
|
|
|
|
item.Orientation = null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-13 21:46:33 +01:00
|
|
|
if (Enum.TryParse(image.ImageTag.Orientation.ToString(), true, out ImageOrientation orientation))
|
2017-09-22 22:33:01 +02:00
|
|
|
{
|
2018-12-11 14:55:33 +01:00
|
|
|
item.Orientation = orientation;
|
2017-09-22 22:33:01 +02:00
|
|
|
}
|
2018-12-11 14:55:33 +01:00
|
|
|
}
|
2014-08-28 03:27:46 +02:00
|
|
|
|
2018-12-11 14:55:33 +01:00
|
|
|
item.ExposureTime = image.ImageTag.ExposureTime;
|
|
|
|
item.FocalLength = image.ImageTag.FocalLength;
|
2014-08-30 16:26:29 +02:00
|
|
|
|
2018-12-11 14:55:33 +01:00
|
|
|
item.Latitude = image.ImageTag.Latitude;
|
|
|
|
item.Longitude = image.ImageTag.Longitude;
|
|
|
|
item.Altitude = image.ImageTag.Altitude;
|
2014-08-30 16:26:29 +02:00
|
|
|
|
2018-12-11 14:55:33 +01:00
|
|
|
if (image.ImageTag.ISOSpeedRatings.HasValue)
|
|
|
|
{
|
|
|
|
item.IsoSpeedRating = Convert.ToInt32(image.ImageTag.ISOSpeedRatings.Value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
item.IsoSpeedRating = null;
|
2017-09-22 22:33:01 +02:00
|
|
|
}
|
2017-05-14 20:55:05 +02:00
|
|
|
}
|
2015-09-14 19:39:35 +02:00
|
|
|
}
|
2014-08-30 16:26:29 +02:00
|
|
|
}
|
2018-12-20 13:11:26 +01:00
|
|
|
catch (Exception ex)
|
2017-09-22 22:33:01 +02:00
|
|
|
{
|
2018-12-20 13:11:26 +01:00
|
|
|
_logger.LogError(ex, "Image Provider - Error reading image tag for {0}", item.Path);
|
2017-09-22 22:33:01 +02:00
|
|
|
}
|
2014-08-28 03:27:46 +02:00
|
|
|
}
|
2017-09-22 22:33:01 +02:00
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
if (item.Width <= 0 || item.Height <= 0)
|
2014-08-28 03:27:46 +02:00
|
|
|
{
|
2017-10-22 08:22:43 +02:00
|
|
|
var img = item.GetImageInfo(ImageType.Primary, 0);
|
2017-09-22 22:33:01 +02:00
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
try
|
|
|
|
{
|
2019-02-05 19:53:50 +01:00
|
|
|
var size = _imageProcessor.GetImageDimensions(item, img, false);
|
2018-09-12 19:26:21 +02:00
|
|
|
|
|
|
|
if (size.Width > 0 && size.Height > 0)
|
|
|
|
{
|
2019-01-26 13:16:47 +01:00
|
|
|
item.Width = size.Width;
|
|
|
|
item.Height = size.Height;
|
2018-09-12 19:26:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (ArgumentException)
|
2017-10-22 08:22:43 +02:00
|
|
|
{
|
2018-09-12 19:26:21 +02:00
|
|
|
// format not supported
|
2017-10-22 08:22:43 +02:00
|
|
|
}
|
2014-08-28 03:27:46 +02:00
|
|
|
}
|
2014-02-13 06:11:54 +01:00
|
|
|
|
|
|
|
const ItemUpdateType result = ItemUpdateType.ImageUpdate | ItemUpdateType.MetadataImport;
|
|
|
|
return Task.FromResult(result);
|
|
|
|
}
|
|
|
|
|
2019-01-13 21:31:14 +01:00
|
|
|
public string Name => "Embedded Information";
|
2014-02-13 06:11:54 +01:00
|
|
|
}
|
|
|
|
}
|