PhotoProvider.cs 8.3 KB

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