PhotoProvider.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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.Entities;
  11. using MediaBrowser.Model.IO;
  12. using MediaBrowser.Model.Logging;
  13. using TagLib;
  14. using TagLib.IFD;
  15. using TagLib.IFD.Entries;
  16. using TagLib.IFD.Tags;
  17. using MediaBrowser.Model.MediaInfo;
  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 fileStream = _fileSystem.OpenRead(item.Path))
  54. {
  55. using (var file = TagLib.File.Create(new StreamFileAbstraction(Path.GetFileName(item.Path), fileStream, null)))
  56. {
  57. var image = file as TagLib.Image.File;
  58. var tag = file.GetTag(TagTypes.TiffIFD) as IFDTag;
  59. if (tag != null)
  60. {
  61. var structure = tag.Structure;
  62. if (structure != null)
  63. {
  64. var exif = structure.GetEntry(0, (ushort)IFDEntryTag.ExifIFD) as SubIFDEntry;
  65. if (exif != null)
  66. {
  67. var exifStructure = exif.Structure;
  68. if (exifStructure != null)
  69. {
  70. var entry = exifStructure.GetEntry(0, (ushort)ExifEntryTag.ApertureValue) as RationalIFDEntry;
  71. if (entry != null)
  72. {
  73. double val = entry.Value.Numerator;
  74. val /= entry.Value.Denominator;
  75. item.Aperture = val;
  76. }
  77. entry = exifStructure.GetEntry(0, (ushort)ExifEntryTag.ShutterSpeedValue) as RationalIFDEntry;
  78. if (entry != null)
  79. {
  80. double val = entry.Value.Numerator;
  81. val /= entry.Value.Denominator;
  82. item.ShutterSpeed = val;
  83. }
  84. }
  85. }
  86. }
  87. }
  88. if (image != null)
  89. {
  90. item.CameraMake = image.ImageTag.Make;
  91. item.CameraModel = image.ImageTag.Model;
  92. item.Width = image.Properties.PhotoWidth;
  93. item.Height = image.Properties.PhotoHeight;
  94. var rating = image.ImageTag.Rating;
  95. if (rating.HasValue)
  96. {
  97. item.CommunityRating = rating;
  98. }
  99. else
  100. {
  101. item.CommunityRating = null;
  102. }
  103. item.Overview = image.ImageTag.Comment;
  104. if (!string.IsNullOrWhiteSpace(image.ImageTag.Title))
  105. {
  106. if (!item.LockedFields.Contains(MetadataFields.Name))
  107. {
  108. item.Name = image.ImageTag.Title;
  109. }
  110. }
  111. var dateTaken = image.ImageTag.DateTime;
  112. if (dateTaken.HasValue)
  113. {
  114. item.DateCreated = dateTaken.Value;
  115. item.PremiereDate = dateTaken.Value;
  116. item.ProductionYear = dateTaken.Value.Year;
  117. }
  118. item.Genres = image.ImageTag.Genres;
  119. item.Tags = image.ImageTag.Keywords;
  120. item.Software = image.ImageTag.Software;
  121. if (image.ImageTag.Orientation == TagLib.Image.ImageOrientation.None)
  122. {
  123. item.Orientation = null;
  124. }
  125. else
  126. {
  127. MediaBrowser.Model.Drawing.ImageOrientation orientation;
  128. if (Enum.TryParse(image.ImageTag.Orientation.ToString(), true, out orientation))
  129. {
  130. item.Orientation = orientation;
  131. }
  132. }
  133. item.ExposureTime = image.ImageTag.ExposureTime;
  134. item.FocalLength = image.ImageTag.FocalLength;
  135. item.Latitude = image.ImageTag.Latitude;
  136. item.Longitude = image.ImageTag.Longitude;
  137. item.Altitude = image.ImageTag.Altitude;
  138. if (image.ImageTag.ISOSpeedRatings.HasValue)
  139. {
  140. item.IsoSpeedRating = Convert.ToInt32(image.ImageTag.ISOSpeedRatings.Value);
  141. }
  142. else
  143. {
  144. item.IsoSpeedRating = null;
  145. }
  146. }
  147. }
  148. }
  149. }
  150. catch (Exception e)
  151. {
  152. _logger.ErrorException("Image Provider - Error reading image tag for {0}", e, item.Path);
  153. }
  154. }
  155. if (item.Width <= 0 || item.Height <= 0)
  156. {
  157. var img = item.GetImageInfo(ImageType.Primary, 0);
  158. try
  159. {
  160. var size = _imageProcessor.GetImageSize(item, img, false, false);
  161. if (size.Width > 0 && size.Height > 0)
  162. {
  163. item.Width = Convert.ToInt32(size.Width);
  164. item.Height = Convert.ToInt32(size.Height);
  165. }
  166. }
  167. catch (ArgumentException)
  168. {
  169. // format not supported
  170. }
  171. }
  172. const ItemUpdateType result = ItemUpdateType.ImageUpdate | ItemUpdateType.MetadataImport;
  173. return Task.FromResult(result);
  174. }
  175. public string Name
  176. {
  177. get { return "Embedded Information"; }
  178. }
  179. }
  180. }