jellyfin/MediaBrowser.Providers/Photos/PhotoProvider.cs

138 lines
5.8 KiB
C#
Raw Normal View History

2014-02-13 05:11:54 +00:00
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using System;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Providers.Photos
{
public class PhotoProvider : ICustomMetadataProvider<Photo>, IHasChangeMonitor
{
private readonly ILogger _logger;
public PhotoProvider(ILogger logger)
{
_logger = logger;
}
public Task<ItemUpdateType> FetchAsync(Photo item, IDirectoryService directoryService, CancellationToken cancellationToken)
{
item.SetImagePath(ImageType.Primary, item.Path);
if (item.Path.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) || item.Path.EndsWith(".jpeg", StringComparison.OrdinalIgnoreCase))
{
try
{
using (var reader = new ExifReader(item.Path))
{
double aperture = 0;
double shutterSpeed = 0;
DateTime dateTaken;
string manufacturer;
string model;
int xResolution;
int yResolution;
reader.GetTagValue(ExifTags.FNumber, out aperture);
reader.GetTagValue(ExifTags.ExposureTime, out shutterSpeed);
reader.GetTagValue(ExifTags.DateTimeOriginal, out dateTaken);
reader.GetTagValue(ExifTags.Make, out manufacturer);
reader.GetTagValue(ExifTags.Model, out model);
reader.GetTagValue(ExifTags.XResolution, out xResolution);
reader.GetTagValue(ExifTags.YResolution, out yResolution);
if (dateTaken > DateTime.MinValue)
{
item.DateCreated = dateTaken;
item.PremiereDate = dateTaken;
item.ProductionYear = dateTaken.Year;
}
var cameraModel = manufacturer ?? string.Empty;
cameraModel += " ";
cameraModel += model ?? string.Empty;
item.Overview = "Taken " + dateTaken.ToString("F") + "\n" +
(!string.IsNullOrWhiteSpace(cameraModel) ? "With a " + cameraModel : "") +
(aperture > 0 && shutterSpeed > 0 ? " at f" + aperture.ToString(CultureInfo.InvariantCulture) + " and " + PhotoHelper.Dec2Frac(shutterSpeed) + "s" : "") + "\n"
+ (xResolution > 0 ? "\n<br/>Resolution: " + xResolution + "x" + yResolution : "");
}
}
catch (Exception e)
{
_logger.ErrorException("Image Provider - Error reading image tag for {0}", e, item.Path);
}
}
//// Get additional tags from xmp
//try
//{
// using (var fs = new FileStream(item.Path, FileMode.Open, FileAccess.Read))
// {
// var bf = BitmapFrame.Create(fs);
// if (bf != null)
// {
// var data = (BitmapMetadata)bf.Metadata;
// if (data != null)
// {
// DateTime dateTaken;
// var cameraModel = "";
// DateTime.TryParse(data.DateTaken, out dateTaken);
// if (dateTaken > DateTime.MinValue) item.DateCreated = dateTaken;
// cameraModel = data.CameraModel;
// item.PremiereDate = dateTaken;
// item.ProductionYear = dateTaken.Year;
// item.Overview = "Taken " + dateTaken.ToString("F") + "\n" +
// (cameraModel != "" ? "With a " + cameraModel : "") +
// (aperture > 0 && shutterSpeed > 0 ? " at f" + aperture.ToString(CultureInfo.InvariantCulture) + " and " + PhotoHelper.Dec2Frac(shutterSpeed) + "s" : "") + "\n"
// + (bf.Width > 0 ? "\n<br/>Resolution: " + (int)bf.Width + "x" + (int)bf.Height : "");
// var photo = item as Photo;
// if (data.Keywords != null) item.Genres = photo.Tags = new List<string>(data.Keywords);
// item.Name = !string.IsNullOrWhiteSpace(data.Title) ? data.Title : item.Name;
// item.CommunityRating = data.Rating;
// if (!string.IsNullOrWhiteSpace(data.Subject)) photo.AddTagline(data.Subject);
// }
// }
// }
//}
//catch (NotSupportedException)
//{
// // No problem - move on
//}
//catch (Exception e)
//{
// _logger.ErrorException("Error trying to read extended data from {0}", e, item.Path);
//}
const ItemUpdateType result = ItemUpdateType.ImageUpdate | ItemUpdateType.MetadataImport;
return Task.FromResult(result);
}
public string Name
{
get { return "Embedded Information"; }
}
public bool HasChanged(IHasMetadata item, IDirectoryService directoryService, DateTime date)
{
return item.DateModified > date;
}
}
}