PhotoProvider.cs 8.1 KB

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