jellyfin/Emby.Photos/PhotoProvider.cs

177 lines
6.9 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;
using MediaBrowser.Controller.Entities;
2014-02-12 22:11:54 -07:00
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
2016-10-31 00:42:14 -07:00
using MediaBrowser.Model.IO;
2014-02-12 22:11:54 -07:00
using MediaBrowser.Model.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
{
2015-01-22 23:15:15 -07:00
public class PhotoProvider : ICustomMetadataProvider<Photo>, IHasItemChangeMonitor, IForcedProvider
2014-02-12 22:11:54 -07:00
{
private readonly ILogger _logger;
2016-10-31 00:42:14 -07:00
private readonly IFileSystem _fileSystem;
2014-02-12 22:11:54 -07:00
2016-10-31 00:42:14 -07:00
public PhotoProvider(ILogger logger, IFileSystem fileSystem)
2014-02-12 22:11:54 -07:00
{
_logger = logger;
2016-10-31 00:42:14 -07:00
_fileSystem = fileSystem;
2014-02-12 22:11:54 -07:00
}
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
try
2014-02-12 22:11:54 -07:00
{
2017-05-14 11:55:05 -07:00
using (var fileStream = _fileSystem.OpenRead(item.Path))
{
2017-05-14 11:55:05 -07:00
using (var file = TagLib.File.Create(new StreamFileAbstraction(Path.GetFileName(item.Path), fileStream, null)))
{
2017-05-14 11:55:05 -07:00
var image = file as TagLib.Image.File;
var tag = file.GetTag(TagTypes.TiffIFD) as IFDTag;
2014-02-12 22:11:54 -07:00
2017-05-14 11:55:05 -07:00
if (tag != null)
2014-02-12 22:11:54 -07:00
{
2017-05-14 11:55:05 -07:00
var structure = tag.Structure;
2017-05-14 11:55:05 -07:00
if (structure != null)
{
2017-05-14 11:55:05 -07:00
var exif = structure.GetEntry(0, (ushort)IFDEntryTag.ExifIFD) as SubIFDEntry;
2017-05-14 11:55:05 -07:00
if (exif != null)
{
2017-05-14 11:55:05 -07:00
var exifStructure = exif.Structure;
2015-09-14 10:39:35 -07:00
2017-05-14 11:55:05 -07:00
if (exifStructure != null)
2015-09-14 10:39:35 -07:00
{
2017-05-14 11:55:05 -07:00
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;
}
2015-09-14 10:39:35 -07:00
}
}
}
2014-02-12 22:11:54 -07:00
}
2017-05-14 11:55:05 -07:00
item.CameraMake = image.ImageTag.Make;
item.CameraModel = image.ImageTag.Model;
2015-03-12 07:51:41 -07:00
2017-05-14 11:55:05 -07:00
item.Width = image.Properties.PhotoWidth;
item.Height = image.Properties.PhotoHeight;
2014-02-19 09:24:06 -07:00
2017-05-14 11:55:05 -07:00
var rating = image.ImageTag.Rating;
if (rating.HasValue)
{
item.CommunityRating = rating;
}
else
{
item.CommunityRating = null;
}
2014-02-12 22:11:54 -07:00
2017-05-14 11:55:05 -07:00
item.Overview = image.ImageTag.Comment;
2017-05-14 11:55:05 -07:00
if (!string.IsNullOrWhiteSpace(image.ImageTag.Title))
{
item.Name = image.ImageTag.Title;
}
2014-02-12 22:11:54 -07:00
2017-05-14 11:55:05 -07:00
var dateTaken = image.ImageTag.DateTime;
if (dateTaken.HasValue)
{
item.DateCreated = dateTaken.Value;
item.PremiereDate = dateTaken.Value;
item.ProductionYear = dateTaken.Value.Year;
}
2017-05-14 11:55:05 -07:00
item.Genres = image.ImageTag.Genres.ToList();
item.Tags = image.ImageTag.Keywords;
2017-05-14 11:55:05 -07:00
item.Software = image.ImageTag.Software;
2015-09-14 10:39:35 -07:00
2017-05-14 11:55:05 -07:00
if (image.ImageTag.Orientation == TagLib.Image.ImageOrientation.None)
2015-09-14 10:39:35 -07:00
{
2017-05-14 11:55:05 -07:00
item.Orientation = null;
}
else
{
MediaBrowser.Model.Drawing.ImageOrientation orientation;
if (Enum.TryParse(image.ImageTag.Orientation.ToString(), true, out orientation))
{
item.Orientation = orientation;
}
2015-09-14 10:39:35 -07:00
}
2017-05-14 11:55:05 -07:00
item.ExposureTime = image.ImageTag.ExposureTime;
item.FocalLength = image.ImageTag.FocalLength;
2014-08-30 07:26:29 -07:00
2017-05-14 11:55:05 -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
2017-05-14 11:55:05 -07:00
if (image.ImageTag.ISOSpeedRatings.HasValue)
{
item.IsoSpeedRating = Convert.ToInt32(image.ImageTag.ISOSpeedRatings.Value);
}
else
{
item.IsoSpeedRating = null;
}
2015-09-14 10:39:35 -07:00
}
2014-08-30 07:26:29 -07:00
}
}
catch (Exception e)
{
_logger.ErrorException("Image Provider - Error reading image tag for {0}", e, item.Path);
}
2014-02-12 22:11:54 -07:00
const ItemUpdateType result = ItemUpdateType.ImageUpdate | ItemUpdateType.MetadataImport;
return Task.FromResult(result);
}
public string Name
{
get { return "Embedded Information"; }
}
2016-04-08 11:32:38 -07:00
public bool HasChanged(IHasMetadata item, IDirectoryService directoryService)
2014-02-12 22:11:54 -07:00
{
2016-08-23 23:13:15 -07:00
if (item.EnableRefreshOnDateModifiedChange && !string.IsNullOrWhiteSpace(item.Path) && item.LocationType == LocationType.FileSystem)
{
2016-08-23 23:13:15 -07:00
var file = directoryService.GetFile(item.Path);
if (file != null && file.LastWriteTimeUtc != item.DateModified)
{
return true;
}
}
2014-09-10 18:57:11 -07:00
return false;
2014-02-12 22:11:54 -07:00
}
}
}