PhotoProvider.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 fileStream = _fileSystem.OpenRead(item.Path))
  34. {
  35. using (var file = TagLib.File.Create(new StreamFileAbstraction(Path.GetFileName(item.Path), fileStream, null)))
  36. {
  37. var image = file as TagLib.Image.File;
  38. var tag = file.GetTag(TagTypes.TiffIFD) as IFDTag;
  39. if (tag != null)
  40. {
  41. var structure = tag.Structure;
  42. if (structure != null)
  43. {
  44. var exif = structure.GetEntry(0, (ushort)IFDEntryTag.ExifIFD) as SubIFDEntry;
  45. if (exif != null)
  46. {
  47. var exifStructure = exif.Structure;
  48. if (exifStructure != null)
  49. {
  50. var entry = exifStructure.GetEntry(0, (ushort)ExifEntryTag.ApertureValue) as RationalIFDEntry;
  51. if (entry != null)
  52. {
  53. double val = entry.Value.Numerator;
  54. val /= entry.Value.Denominator;
  55. item.Aperture = val;
  56. }
  57. entry = exifStructure.GetEntry(0, (ushort)ExifEntryTag.ShutterSpeedValue) as RationalIFDEntry;
  58. if (entry != null)
  59. {
  60. double val = entry.Value.Numerator;
  61. val /= entry.Value.Denominator;
  62. item.ShutterSpeed = val;
  63. }
  64. }
  65. }
  66. }
  67. }
  68. item.CameraMake = image.ImageTag.Make;
  69. item.CameraModel = image.ImageTag.Model;
  70. item.Width = image.Properties.PhotoWidth;
  71. item.Height = image.Properties.PhotoHeight;
  72. var rating = image.ImageTag.Rating;
  73. if (rating.HasValue)
  74. {
  75. item.CommunityRating = rating;
  76. }
  77. else
  78. {
  79. item.CommunityRating = null;
  80. }
  81. item.Overview = image.ImageTag.Comment;
  82. if (!string.IsNullOrWhiteSpace(image.ImageTag.Title))
  83. {
  84. item.Name = image.ImageTag.Title;
  85. }
  86. var dateTaken = image.ImageTag.DateTime;
  87. if (dateTaken.HasValue)
  88. {
  89. item.DateCreated = dateTaken.Value;
  90. item.PremiereDate = dateTaken.Value;
  91. item.ProductionYear = dateTaken.Value.Year;
  92. }
  93. item.Genres = image.ImageTag.Genres.ToList();
  94. item.Tags = image.ImageTag.Keywords.ToList();
  95. item.Software = image.ImageTag.Software;
  96. if (image.ImageTag.Orientation == TagLib.Image.ImageOrientation.None)
  97. {
  98. item.Orientation = null;
  99. }
  100. else
  101. {
  102. MediaBrowser.Model.Drawing.ImageOrientation orientation;
  103. if (Enum.TryParse(image.ImageTag.Orientation.ToString(), true, out orientation))
  104. {
  105. item.Orientation = orientation;
  106. }
  107. }
  108. item.ExposureTime = image.ImageTag.ExposureTime;
  109. item.FocalLength = image.ImageTag.FocalLength;
  110. item.Latitude = image.ImageTag.Latitude;
  111. item.Longitude = image.ImageTag.Longitude;
  112. item.Altitude = image.ImageTag.Altitude;
  113. if (image.ImageTag.ISOSpeedRatings.HasValue)
  114. {
  115. item.IsoSpeedRating = Convert.ToInt32(image.ImageTag.ISOSpeedRatings.Value);
  116. }
  117. else
  118. {
  119. item.IsoSpeedRating = null;
  120. }
  121. }
  122. }
  123. }
  124. catch (Exception e)
  125. {
  126. _logger.ErrorException("Image Provider - Error reading image tag for {0}", e, item.Path);
  127. }
  128. const ItemUpdateType result = ItemUpdateType.ImageUpdate | ItemUpdateType.MetadataImport;
  129. return Task.FromResult(result);
  130. }
  131. public string Name
  132. {
  133. get { return "Embedded Information"; }
  134. }
  135. public bool HasChanged(IHasMetadata item, IDirectoryService directoryService)
  136. {
  137. if (item.EnableRefreshOnDateModifiedChange && !string.IsNullOrWhiteSpace(item.Path) && item.LocationType == LocationType.FileSystem)
  138. {
  139. var file = directoryService.GetFile(item.Path);
  140. if (file != null && file.LastWriteTimeUtc != item.DateModified)
  141. {
  142. return true;
  143. }
  144. }
  145. return false;
  146. }
  147. }
  148. }