PhotoProvider.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using MediaBrowser.Controller.Entities;
  7. using MediaBrowser.Controller.Library;
  8. using MediaBrowser.Controller.Providers;
  9. using MediaBrowser.Model.Entities;
  10. using MediaBrowser.Model.IO;
  11. using MediaBrowser.Model.Logging;
  12. using TagLib;
  13. using TagLib.IFD;
  14. using TagLib.IFD.Entries;
  15. using TagLib.IFD.Tags;
  16. namespace Emby.Photos
  17. {
  18. public class PhotoProvider : ICustomMetadataProvider<Photo>, IHasItemChangeMonitor, IForcedProvider
  19. {
  20. private readonly ILogger _logger;
  21. private readonly IFileSystem _fileSystem;
  22. public PhotoProvider(ILogger logger, IFileSystem fileSystem)
  23. {
  24. _logger = logger;
  25. _fileSystem = fileSystem;
  26. }
  27. public Task<ItemUpdateType> FetchAsync(Photo item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  28. {
  29. item.SetImagePath(ImageType.Primary, item.Path);
  30. // Examples: https://github.com/mono/taglib-sharp/blob/a5f6949a53d09ce63ee7495580d6802921a21f14/tests/fixtures/TagLib.Tests.Images/NullOrientationTest.cs
  31. try
  32. {
  33. using (var file = TagLib.File.Create(new StreamFileAbstraction(Path.GetFileName(item.Path), _fileSystem.OpenRead(item.Path), null)))
  34. {
  35. var image = file as TagLib.Image.File;
  36. var tag = file.GetTag(TagTypes.TiffIFD) as IFDTag;
  37. if (tag != null)
  38. {
  39. var structure = tag.Structure;
  40. if (structure != null)
  41. {
  42. var exif = structure.GetEntry(0, (ushort)IFDEntryTag.ExifIFD) as SubIFDEntry;
  43. if (exif != null)
  44. {
  45. var exifStructure = exif.Structure;
  46. if (exifStructure != null)
  47. {
  48. var entry = exifStructure.GetEntry(0, (ushort)ExifEntryTag.ApertureValue) as RationalIFDEntry;
  49. if (entry != null)
  50. {
  51. double val = entry.Value.Numerator;
  52. val /= entry.Value.Denominator;
  53. item.Aperture = val;
  54. }
  55. entry = exifStructure.GetEntry(0, (ushort)ExifEntryTag.ShutterSpeedValue) as RationalIFDEntry;
  56. if (entry != null)
  57. {
  58. double val = entry.Value.Numerator;
  59. val /= entry.Value.Denominator;
  60. item.ShutterSpeed = val;
  61. }
  62. }
  63. }
  64. }
  65. }
  66. item.CameraMake = image.ImageTag.Make;
  67. item.CameraModel = image.ImageTag.Model;
  68. item.Width = image.Properties.PhotoWidth;
  69. item.Height = image.Properties.PhotoHeight;
  70. var rating = image.ImageTag.Rating;
  71. if (rating.HasValue)
  72. {
  73. item.CommunityRating = rating;
  74. }
  75. else
  76. {
  77. item.CommunityRating = null;
  78. }
  79. item.Overview = image.ImageTag.Comment;
  80. if (!string.IsNullOrWhiteSpace(image.ImageTag.Title))
  81. {
  82. item.Name = image.ImageTag.Title;
  83. }
  84. var dateTaken = image.ImageTag.DateTime;
  85. if (dateTaken.HasValue)
  86. {
  87. item.DateCreated = dateTaken.Value;
  88. item.PremiereDate = dateTaken.Value;
  89. item.ProductionYear = dateTaken.Value.Year;
  90. }
  91. item.Genres = image.ImageTag.Genres.ToList();
  92. item.Tags = image.ImageTag.Keywords.ToList();
  93. item.Software = image.ImageTag.Software;
  94. if (image.ImageTag.Orientation == TagLib.Image.ImageOrientation.None)
  95. {
  96. item.Orientation = null;
  97. }
  98. else
  99. {
  100. MediaBrowser.Model.Drawing.ImageOrientation orientation;
  101. if (Enum.TryParse(image.ImageTag.Orientation.ToString(), true, out orientation))
  102. {
  103. item.Orientation = orientation;
  104. }
  105. }
  106. item.ExposureTime = image.ImageTag.ExposureTime;
  107. item.FocalLength = image.ImageTag.FocalLength;
  108. item.Latitude = image.ImageTag.Latitude;
  109. item.Longitude = image.ImageTag.Longitude;
  110. item.Altitude = image.ImageTag.Altitude;
  111. if (image.ImageTag.ISOSpeedRatings.HasValue)
  112. {
  113. item.IsoSpeedRating = Convert.ToInt32(image.ImageTag.ISOSpeedRatings.Value);
  114. }
  115. else
  116. {
  117. item.IsoSpeedRating = null;
  118. }
  119. }
  120. }
  121. catch (Exception e)
  122. {
  123. _logger.ErrorException("Image Provider - Error reading image tag for {0}", e, item.Path);
  124. }
  125. const ItemUpdateType result = ItemUpdateType.ImageUpdate | ItemUpdateType.MetadataImport;
  126. return Task.FromResult(result);
  127. }
  128. public string Name
  129. {
  130. get { return "Embedded Information"; }
  131. }
  132. public bool HasChanged(IHasMetadata item, IDirectoryService directoryService)
  133. {
  134. if (item.EnableRefreshOnDateModifiedChange && !string.IsNullOrWhiteSpace(item.Path) && item.LocationType == LocationType.FileSystem)
  135. {
  136. var file = directoryService.GetFile(item.Path);
  137. if (file != null && file.LastWriteTimeUtc != item.DateModified)
  138. {
  139. return true;
  140. }
  141. }
  142. return false;
  143. }
  144. }
  145. }