ImageProcessor.cs 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Controller;
  3. using MediaBrowser.Controller.Drawing;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Controller.Providers;
  6. using MediaBrowser.Model.Drawing;
  7. using MediaBrowser.Model.Entities;
  8. using MediaBrowser.Model.Logging;
  9. using MediaBrowser.Model.Serialization;
  10. using System;
  11. using System.Collections.Concurrent;
  12. using System.Collections.Generic;
  13. using System.Globalization;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. using MediaBrowser.Model.IO;
  19. using Emby.Drawing.Common;
  20. using MediaBrowser.Controller.Library;
  21. using MediaBrowser.Controller.MediaEncoding;
  22. using MediaBrowser.Model.Net;
  23. using MediaBrowser.Model.Threading;
  24. using MediaBrowser.Model.Extensions;
  25. namespace Emby.Drawing
  26. {
  27. /// <summary>
  28. /// Class ImageProcessor
  29. /// </summary>
  30. public class ImageProcessor : IImageProcessor, IDisposable
  31. {
  32. /// <summary>
  33. /// The us culture
  34. /// </summary>
  35. protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
  36. /// <summary>
  37. /// The _cached imaged sizes
  38. /// </summary>
  39. private readonly ConcurrentDictionary<Guid, ImageSize> _cachedImagedSizes;
  40. /// <summary>
  41. /// Gets the list of currently registered image processors
  42. /// Image processors are specialized metadata providers that run after the normal ones
  43. /// </summary>
  44. /// <value>The image enhancers.</value>
  45. public IImageEnhancer[] ImageEnhancers { get; private set; }
  46. /// <summary>
  47. /// The _logger
  48. /// </summary>
  49. private readonly ILogger _logger;
  50. private readonly IFileSystem _fileSystem;
  51. private readonly IJsonSerializer _jsonSerializer;
  52. private readonly IServerApplicationPaths _appPaths;
  53. private IImageEncoder _imageEncoder;
  54. private readonly Func<ILibraryManager> _libraryManager;
  55. private readonly Func<IMediaEncoder> _mediaEncoder;
  56. public ImageProcessor(ILogger logger,
  57. IServerApplicationPaths appPaths,
  58. IFileSystem fileSystem,
  59. IJsonSerializer jsonSerializer,
  60. IImageEncoder imageEncoder,
  61. Func<ILibraryManager> libraryManager, ITimerFactory timerFactory, Func<IMediaEncoder> mediaEncoder)
  62. {
  63. _logger = logger;
  64. _fileSystem = fileSystem;
  65. _jsonSerializer = jsonSerializer;
  66. _imageEncoder = imageEncoder;
  67. _libraryManager = libraryManager;
  68. _mediaEncoder = mediaEncoder;
  69. _appPaths = appPaths;
  70. ImageEnhancers = new IImageEnhancer[] { };
  71. _saveImageSizeTimer = timerFactory.Create(SaveImageSizeCallback, null, Timeout.Infinite, Timeout.Infinite);
  72. ImageHelper.ImageProcessor = this;
  73. Dictionary<Guid, ImageSize> sizeDictionary;
  74. try
  75. {
  76. sizeDictionary = jsonSerializer.DeserializeFromFile<Dictionary<Guid, ImageSize>>(ImageSizeFile) ??
  77. new Dictionary<Guid, ImageSize>();
  78. }
  79. catch (FileNotFoundException)
  80. {
  81. // No biggie
  82. sizeDictionary = new Dictionary<Guid, ImageSize>();
  83. }
  84. catch (IOException)
  85. {
  86. // No biggie
  87. sizeDictionary = new Dictionary<Guid, ImageSize>();
  88. }
  89. catch (Exception ex)
  90. {
  91. logger.ErrorException("Error parsing image size cache file", ex);
  92. sizeDictionary = new Dictionary<Guid, ImageSize>();
  93. }
  94. _cachedImagedSizes = new ConcurrentDictionary<Guid, ImageSize>(sizeDictionary);
  95. }
  96. public IImageEncoder ImageEncoder
  97. {
  98. get { return _imageEncoder; }
  99. set
  100. {
  101. if (value == null)
  102. {
  103. throw new ArgumentNullException("value");
  104. }
  105. _imageEncoder = value;
  106. }
  107. }
  108. public string[] SupportedInputFormats
  109. {
  110. get
  111. {
  112. return new string[]
  113. {
  114. "tiff",
  115. "tif",
  116. "jpeg",
  117. "jpg",
  118. "png",
  119. "aiff",
  120. "cr2",
  121. "crw",
  122. "dng",
  123. // Remove until supported
  124. //"nef",
  125. "orf",
  126. "pef",
  127. "arw",
  128. "webp",
  129. "gif",
  130. "bmp",
  131. "erf",
  132. "raf",
  133. "rw2",
  134. "nrw",
  135. "dng",
  136. "ico",
  137. "astc",
  138. "ktx",
  139. "pkm",
  140. "wbmp"
  141. };
  142. }
  143. }
  144. public bool SupportsImageCollageCreation
  145. {
  146. get
  147. {
  148. return _imageEncoder.SupportsImageCollageCreation;
  149. }
  150. }
  151. private string ResizedImageCachePath
  152. {
  153. get
  154. {
  155. return Path.Combine(_appPaths.ImageCachePath, "resized-images");
  156. }
  157. }
  158. private string EnhancedImageCachePath
  159. {
  160. get
  161. {
  162. return Path.Combine(_appPaths.ImageCachePath, "enhanced-images");
  163. }
  164. }
  165. public void AddParts(IEnumerable<IImageEnhancer> enhancers)
  166. {
  167. ImageEnhancers = enhancers.ToArray();
  168. }
  169. public async Task ProcessImage(ImageProcessingOptions options, Stream toStream)
  170. {
  171. var file = await ProcessImage(options).ConfigureAwait(false);
  172. using (var fileStream = _fileSystem.GetFileStream(file.Item1, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read, true))
  173. {
  174. await fileStream.CopyToAsync(toStream).ConfigureAwait(false);
  175. }
  176. }
  177. public ImageFormat[] GetSupportedImageOutputFormats()
  178. {
  179. return _imageEncoder.SupportedOutputFormats;
  180. }
  181. private static readonly string[] TransparentImageTypes = new string[] { ".png", ".webp" };
  182. public bool SupportsTransparency(string path)
  183. {
  184. return TransparentImageTypes.Contains(Path.GetExtension(path) ?? string.Empty);
  185. }
  186. public async Task<Tuple<string, string, DateTime>> ProcessImage(ImageProcessingOptions options)
  187. {
  188. if (options == null)
  189. {
  190. throw new ArgumentNullException("options");
  191. }
  192. var originalImage = options.Image;
  193. IHasMetadata item = options.Item;
  194. if (!originalImage.IsLocalFile)
  195. {
  196. if (item == null)
  197. {
  198. item = _libraryManager().GetItemById(options.ItemId);
  199. }
  200. originalImage = await _libraryManager().ConvertImageToLocal(item, originalImage, options.ImageIndex).ConfigureAwait(false);
  201. }
  202. var originalImagePath = originalImage.Path;
  203. var dateModified = originalImage.DateModified;
  204. if (!_imageEncoder.SupportsImageEncoding)
  205. {
  206. return new Tuple<string, string, DateTime>(originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified);
  207. }
  208. var supportedImageInfo = await GetSupportedImage(originalImagePath, dateModified).ConfigureAwait(false);
  209. originalImagePath = supportedImageInfo.Item1;
  210. dateModified = supportedImageInfo.Item2;
  211. var requiresTransparency = TransparentImageTypes.Contains(Path.GetExtension(originalImagePath) ?? string.Empty);
  212. if (options.Enhancers.Count > 0)
  213. {
  214. if (item == null)
  215. {
  216. item = _libraryManager().GetItemById(options.ItemId);
  217. }
  218. var tuple = await GetEnhancedImage(new ItemImageInfo
  219. {
  220. DateModified = dateModified,
  221. Type = originalImage.Type,
  222. Path = originalImagePath
  223. }, requiresTransparency, item, options.ImageIndex, options.Enhancers).ConfigureAwait(false);
  224. originalImagePath = tuple.Item1;
  225. dateModified = tuple.Item2;
  226. requiresTransparency = tuple.Item3;
  227. }
  228. var photo = item as Photo;
  229. var autoOrient = false;
  230. ImageOrientation? orientation = null;
  231. if (photo != null && photo.Orientation.HasValue && photo.Orientation.Value != ImageOrientation.TopLeft)
  232. {
  233. autoOrient = true;
  234. orientation = photo.Orientation;
  235. }
  236. if (options.HasDefaultOptions(originalImagePath) && (!autoOrient || !options.RequiresAutoOrientation))
  237. {
  238. // Just spit out the original file if all the options are default
  239. return new Tuple<string, string, DateTime>(originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified);
  240. }
  241. ImageSize? originalImageSize = GetSavedImageSize(originalImagePath, dateModified);
  242. if (originalImageSize.HasValue && options.HasDefaultOptions(originalImagePath, originalImageSize.Value) && !autoOrient)
  243. {
  244. // Just spit out the original file if all the options are default
  245. _logger.Info("Returning original image {0}", originalImagePath);
  246. return new Tuple<string, string, DateTime>(originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified);
  247. }
  248. var newSize = ImageHelper.GetNewImageSize(options, originalImageSize);
  249. var quality = options.Quality;
  250. var outputFormat = GetOutputFormat(options.SupportedOutputFormats, requiresTransparency);
  251. var cacheFilePath = GetCacheFilePath(originalImagePath, newSize, quality, dateModified, outputFormat, options.AddPlayedIndicator, options.PercentPlayed, options.UnplayedCount, options.Blur, options.BackgroundColor, options.ForegroundLayer);
  252. try
  253. {
  254. CheckDisposed();
  255. if (!_fileSystem.FileExists(cacheFilePath))
  256. {
  257. var tmpPath = Path.ChangeExtension(Path.Combine(_appPaths.TempDirectory, Guid.NewGuid().ToString("N")), Path.GetExtension(cacheFilePath));
  258. _fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(tmpPath));
  259. if (options.CropWhiteSpace && !SupportsTransparency(originalImagePath))
  260. {
  261. options.CropWhiteSpace = false;
  262. }
  263. var resultPath = _imageEncoder.EncodeImage(originalImagePath, dateModified, tmpPath, autoOrient, orientation, quality, options, outputFormat);
  264. if (string.Equals(resultPath, originalImagePath, StringComparison.OrdinalIgnoreCase))
  265. {
  266. return new Tuple<string, string, DateTime>(originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified);
  267. }
  268. _fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(cacheFilePath));
  269. CopyFile(tmpPath, cacheFilePath);
  270. return new Tuple<string, string, DateTime>(tmpPath, GetMimeType(outputFormat, cacheFilePath), _fileSystem.GetLastWriteTimeUtc(tmpPath));
  271. }
  272. return new Tuple<string, string, DateTime>(cacheFilePath, GetMimeType(outputFormat, cacheFilePath), _fileSystem.GetLastWriteTimeUtc(cacheFilePath));
  273. }
  274. catch (ArgumentOutOfRangeException ex)
  275. {
  276. // Decoder failed to decode it
  277. #if DEBUG
  278. _logger.ErrorException("Error encoding image", ex);
  279. #endif
  280. // Just spit out the original file if all the options are default
  281. return new Tuple<string, string, DateTime>(originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified);
  282. }
  283. catch (Exception ex)
  284. {
  285. // If it fails for whatever reason, return the original image
  286. _logger.ErrorException("Error encoding image", ex);
  287. // Just spit out the original file if all the options are default
  288. return new Tuple<string, string, DateTime>(originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified);
  289. }
  290. }
  291. private ImageFormat GetOutputFormat(ImageFormat[] clientSupportedFormats, bool requiresTransparency)
  292. {
  293. var serverFormats = GetSupportedImageOutputFormats();
  294. // Client doesn't care about format, so start with webp if supported
  295. if (serverFormats.Contains(ImageFormat.Webp) && clientSupportedFormats.Contains(ImageFormat.Webp))
  296. {
  297. return ImageFormat.Webp;
  298. }
  299. // If transparency is needed and webp isn't supported, than png is the only option
  300. if (requiresTransparency)
  301. {
  302. return ImageFormat.Png;
  303. }
  304. foreach (var format in clientSupportedFormats)
  305. {
  306. if (serverFormats.Contains(format))
  307. {
  308. return format;
  309. }
  310. }
  311. // We should never actually get here
  312. return ImageFormat.Jpg;
  313. }
  314. private void CopyFile(string src, string destination)
  315. {
  316. try
  317. {
  318. _fileSystem.CopyFile(src, destination, true);
  319. }
  320. catch
  321. {
  322. }
  323. }
  324. //private static int[][] OPERATIONS = new int[][] {
  325. // TopLeft
  326. //new int[] { 0, NONE},
  327. // TopRight
  328. //new int[] { 0, HORIZONTAL},
  329. //new int[] {180, NONE},
  330. // LeftTop
  331. //new int[] { 0, VERTICAL},
  332. //new int[] { 90, HORIZONTAL},
  333. // RightTop
  334. //new int[] { 90, NONE},
  335. //new int[] {-90, HORIZONTAL},
  336. //new int[] {-90, NONE},
  337. //};
  338. private string GetMimeType(ImageFormat format, string path)
  339. {
  340. if (format == ImageFormat.Bmp)
  341. {
  342. return MimeTypes.GetMimeType("i.bmp");
  343. }
  344. if (format == ImageFormat.Gif)
  345. {
  346. return MimeTypes.GetMimeType("i.gif");
  347. }
  348. if (format == ImageFormat.Jpg)
  349. {
  350. return MimeTypes.GetMimeType("i.jpg");
  351. }
  352. if (format == ImageFormat.Png)
  353. {
  354. return MimeTypes.GetMimeType("i.png");
  355. }
  356. if (format == ImageFormat.Webp)
  357. {
  358. return MimeTypes.GetMimeType("i.webp");
  359. }
  360. return MimeTypes.GetMimeType(path);
  361. }
  362. /// <summary>
  363. /// Increment this when there's a change requiring caches to be invalidated
  364. /// </summary>
  365. private const string Version = "3";
  366. /// <summary>
  367. /// Gets the cache file path based on a set of parameters
  368. /// </summary>
  369. private string GetCacheFilePath(string originalPath, ImageSize outputSize, int quality, DateTime dateModified, ImageFormat format, bool addPlayedIndicator, double percentPlayed, int? unwatchedCount, int? blur, string backgroundColor, string foregroundLayer)
  370. {
  371. var filename = originalPath;
  372. filename += "width=" + outputSize.Width;
  373. filename += "height=" + outputSize.Height;
  374. filename += "quality=" + quality;
  375. filename += "datemodified=" + dateModified.Ticks;
  376. filename += "f=" + format;
  377. if (addPlayedIndicator)
  378. {
  379. filename += "pl=true";
  380. }
  381. if (percentPlayed > 0)
  382. {
  383. filename += "p=" + percentPlayed;
  384. }
  385. if (unwatchedCount.HasValue)
  386. {
  387. filename += "p=" + unwatchedCount.Value;
  388. }
  389. if (blur.HasValue)
  390. {
  391. filename += "blur=" + blur.Value;
  392. }
  393. if (!string.IsNullOrEmpty(backgroundColor))
  394. {
  395. filename += "b=" + backgroundColor;
  396. }
  397. if (!string.IsNullOrEmpty(foregroundLayer))
  398. {
  399. filename += "fl=" + foregroundLayer;
  400. }
  401. filename += "v=" + Version;
  402. return GetCachePath(ResizedImageCachePath, filename, "." + format.ToString().ToLower());
  403. }
  404. public ImageSize GetImageSize(ItemImageInfo info, bool allowSlowMethods)
  405. {
  406. return GetImageSize(info.Path, info.DateModified, allowSlowMethods);
  407. }
  408. public ImageSize GetImageSize(ItemImageInfo info)
  409. {
  410. return GetImageSize(info.Path, info.DateModified, false);
  411. }
  412. public ImageSize GetImageSize(string path)
  413. {
  414. return GetImageSize(path, _fileSystem.GetLastWriteTimeUtc(path), false);
  415. }
  416. /// <summary>
  417. /// Gets the size of the image.
  418. /// </summary>
  419. /// <param name="path">The path.</param>
  420. /// <param name="imageDateModified">The image date modified.</param>
  421. /// <param name="allowSlowMethod">if set to <c>true</c> [allow slow method].</param>
  422. /// <returns>ImageSize.</returns>
  423. /// <exception cref="System.ArgumentNullException">path</exception>
  424. private ImageSize GetImageSize(string path, DateTime imageDateModified, bool allowSlowMethod)
  425. {
  426. if (string.IsNullOrEmpty(path))
  427. {
  428. throw new ArgumentNullException("path");
  429. }
  430. ImageSize size;
  431. var cacheHash = GetImageSizeKey(path, imageDateModified);
  432. if (!_cachedImagedSizes.TryGetValue(cacheHash, out size))
  433. {
  434. size = GetImageSizeInternal(path, allowSlowMethod);
  435. SaveImageSize(size, cacheHash, false);
  436. }
  437. return size;
  438. }
  439. public void SaveImageSize(string path, DateTime imageDateModified, ImageSize size)
  440. {
  441. var cacheHash = GetImageSizeKey(path, imageDateModified);
  442. SaveImageSize(size, cacheHash, true);
  443. }
  444. private void SaveImageSize(ImageSize size, Guid cacheHash, bool checkExists)
  445. {
  446. if (size.Width <= 0 || size.Height <= 0)
  447. {
  448. return;
  449. }
  450. if (checkExists && _cachedImagedSizes.ContainsKey(cacheHash))
  451. {
  452. return;
  453. }
  454. if (checkExists)
  455. {
  456. if (_cachedImagedSizes.TryAdd(cacheHash, size))
  457. {
  458. StartSaveImageSizeTimer();
  459. }
  460. }
  461. else
  462. {
  463. StartSaveImageSizeTimer();
  464. _cachedImagedSizes.AddOrUpdate(cacheHash, size, (keyName, oldValue) => size);
  465. }
  466. }
  467. private Guid GetImageSizeKey(string path, DateTime imageDateModified)
  468. {
  469. var name = path + "datemodified=" + imageDateModified.Ticks;
  470. return name.GetMD5();
  471. }
  472. public ImageSize? GetSavedImageSize(string path, DateTime imageDateModified)
  473. {
  474. ImageSize size;
  475. var cacheHash = GetImageSizeKey(path, imageDateModified);
  476. if (_cachedImagedSizes.TryGetValue(cacheHash, out size))
  477. {
  478. return size;
  479. }
  480. return null;
  481. }
  482. /// <summary>
  483. /// Gets the image size internal.
  484. /// </summary>
  485. /// <param name="path">The path.</param>
  486. /// <param name="allowSlowMethod">if set to <c>true</c> [allow slow method].</param>
  487. /// <returns>ImageSize.</returns>
  488. private ImageSize GetImageSizeInternal(string path, bool allowSlowMethod)
  489. {
  490. //try
  491. //{
  492. // using (var fileStream = _fileSystem.OpenRead(path))
  493. // {
  494. // using (var file = TagLib.File.Create(new StreamFileAbstraction(Path.GetFileName(path), fileStream, null)))
  495. // {
  496. // var image = file as TagLib.Image.File;
  497. // if (image != null)
  498. // {
  499. // var properties = image.Properties;
  500. // return new ImageSize
  501. // {
  502. // Height = properties.PhotoHeight,
  503. // Width = properties.PhotoWidth
  504. // };
  505. // }
  506. // }
  507. // }
  508. //}
  509. //catch
  510. //{
  511. //}
  512. try
  513. {
  514. return ImageHeader.GetDimensions(path, _logger, _fileSystem);
  515. }
  516. catch
  517. {
  518. if (allowSlowMethod)
  519. {
  520. return _imageEncoder.GetImageSize(path);
  521. }
  522. throw;
  523. }
  524. }
  525. private readonly ITimer _saveImageSizeTimer;
  526. private const int SaveImageSizeTimeout = 5000;
  527. private readonly object _saveImageSizeLock = new object();
  528. private void StartSaveImageSizeTimer()
  529. {
  530. _saveImageSizeTimer.Change(SaveImageSizeTimeout, Timeout.Infinite);
  531. }
  532. private void SaveImageSizeCallback(object state)
  533. {
  534. lock (_saveImageSizeLock)
  535. {
  536. try
  537. {
  538. var path = ImageSizeFile;
  539. _fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(path));
  540. _jsonSerializer.SerializeToFile(_cachedImagedSizes, path);
  541. }
  542. catch (Exception ex)
  543. {
  544. _logger.ErrorException("Error saving image size file", ex);
  545. }
  546. }
  547. }
  548. private string ImageSizeFile
  549. {
  550. get
  551. {
  552. return Path.Combine(_appPaths.DataPath, "imagesizes.json");
  553. }
  554. }
  555. /// <summary>
  556. /// Gets the image cache tag.
  557. /// </summary>
  558. /// <param name="item">The item.</param>
  559. /// <param name="image">The image.</param>
  560. /// <returns>Guid.</returns>
  561. /// <exception cref="System.ArgumentNullException">item</exception>
  562. public string GetImageCacheTag(IHasMetadata item, ItemImageInfo image)
  563. {
  564. if (item == null)
  565. {
  566. throw new ArgumentNullException("item");
  567. }
  568. if (image == null)
  569. {
  570. throw new ArgumentNullException("image");
  571. }
  572. var supportedEnhancers = GetSupportedEnhancers(item, image.Type);
  573. return GetImageCacheTag(item, image, supportedEnhancers);
  574. }
  575. /// <summary>
  576. /// Gets the image cache tag.
  577. /// </summary>
  578. /// <param name="item">The item.</param>
  579. /// <param name="image">The image.</param>
  580. /// <param name="imageEnhancers">The image enhancers.</param>
  581. /// <returns>Guid.</returns>
  582. /// <exception cref="System.ArgumentNullException">item</exception>
  583. public string GetImageCacheTag(IHasMetadata item, ItemImageInfo image, List<IImageEnhancer> imageEnhancers)
  584. {
  585. if (item == null)
  586. {
  587. throw new ArgumentNullException("item");
  588. }
  589. if (imageEnhancers == null)
  590. {
  591. throw new ArgumentNullException("imageEnhancers");
  592. }
  593. if (image == null)
  594. {
  595. throw new ArgumentNullException("image");
  596. }
  597. var originalImagePath = image.Path;
  598. var dateModified = image.DateModified;
  599. var imageType = image.Type;
  600. // Optimization
  601. if (imageEnhancers.Count == 0)
  602. {
  603. return (originalImagePath + dateModified.Ticks).GetMD5().ToString("N");
  604. }
  605. // Cache name is created with supported enhancers combined with the last config change so we pick up new config changes
  606. var cacheKeys = imageEnhancers.Select(i => i.GetConfigurationCacheKey(item, imageType)).ToList();
  607. cacheKeys.Add(originalImagePath + dateModified.Ticks);
  608. return string.Join("|", cacheKeys.ToArray(cacheKeys.Count)).GetMD5().ToString("N");
  609. }
  610. private async Task<Tuple<string, DateTime>> GetSupportedImage(string originalImagePath, DateTime dateModified)
  611. {
  612. var inputFormat = (Path.GetExtension(originalImagePath) ?? string.Empty)
  613. .TrimStart('.')
  614. .Replace("jpeg", "jpg", StringComparison.OrdinalIgnoreCase);
  615. // These are just jpg files renamed as tbn
  616. if (string.Equals(inputFormat, "tbn", StringComparison.OrdinalIgnoreCase))
  617. {
  618. return new Tuple<string, DateTime>(originalImagePath, dateModified);
  619. }
  620. if (!_imageEncoder.SupportedInputFormats.Contains(inputFormat, StringComparer.OrdinalIgnoreCase))
  621. {
  622. try
  623. {
  624. var filename = (originalImagePath + dateModified.Ticks.ToString(UsCulture)).GetMD5().ToString("N");
  625. var cacheExtension = _mediaEncoder().SupportsEncoder("libwebp") ? ".webp" : ".png";
  626. var outputPath = Path.Combine(_appPaths.ImageCachePath, "converted-images", filename + cacheExtension);
  627. var file = _fileSystem.GetFileInfo(outputPath);
  628. if (!file.Exists)
  629. {
  630. await _mediaEncoder().ConvertImage(originalImagePath, outputPath).ConfigureAwait(false);
  631. dateModified = _fileSystem.GetLastWriteTimeUtc(outputPath);
  632. }
  633. else
  634. {
  635. dateModified = file.LastWriteTimeUtc;
  636. }
  637. originalImagePath = outputPath;
  638. }
  639. catch (Exception ex)
  640. {
  641. _logger.ErrorException("Image conversion failed for {0}", ex, originalImagePath);
  642. }
  643. }
  644. return new Tuple<string, DateTime>(originalImagePath, dateModified);
  645. }
  646. /// <summary>
  647. /// Gets the enhanced image.
  648. /// </summary>
  649. /// <param name="item">The item.</param>
  650. /// <param name="imageType">Type of the image.</param>
  651. /// <param name="imageIndex">Index of the image.</param>
  652. /// <returns>Task{System.String}.</returns>
  653. public async Task<string> GetEnhancedImage(IHasMetadata item, ImageType imageType, int imageIndex)
  654. {
  655. var enhancers = GetSupportedEnhancers(item, imageType);
  656. var imageInfo = item.GetImageInfo(imageType, imageIndex);
  657. var inputImageSupportsTransparency = SupportsTransparency(imageInfo.Path);
  658. var result = await GetEnhancedImage(imageInfo, inputImageSupportsTransparency, item, imageIndex, enhancers);
  659. return result.Item1;
  660. }
  661. private async Task<Tuple<string, DateTime, bool>> GetEnhancedImage(ItemImageInfo image,
  662. bool inputImageSupportsTransparency,
  663. IHasMetadata item,
  664. int imageIndex,
  665. List<IImageEnhancer> enhancers)
  666. {
  667. var originalImagePath = image.Path;
  668. var dateModified = image.DateModified;
  669. var imageType = image.Type;
  670. try
  671. {
  672. var cacheGuid = GetImageCacheTag(item, image, enhancers);
  673. // Enhance if we have enhancers
  674. var ehnancedImageInfo = await GetEnhancedImageInternal(originalImagePath, item, imageType, imageIndex, enhancers, cacheGuid).ConfigureAwait(false);
  675. var ehnancedImagePath = ehnancedImageInfo.Item1;
  676. // If the path changed update dateModified
  677. if (!string.Equals(ehnancedImagePath, originalImagePath, StringComparison.OrdinalIgnoreCase))
  678. {
  679. var treatmentRequiresTransparency = ehnancedImageInfo.Item2;
  680. return new Tuple<string, DateTime, bool>(ehnancedImagePath, _fileSystem.GetLastWriteTimeUtc(ehnancedImagePath), treatmentRequiresTransparency);
  681. }
  682. }
  683. catch (Exception ex)
  684. {
  685. _logger.Error("Error enhancing image", ex);
  686. }
  687. return new Tuple<string, DateTime, bool>(originalImagePath, dateModified, inputImageSupportsTransparency);
  688. }
  689. /// <summary>
  690. /// Gets the enhanced image internal.
  691. /// </summary>
  692. /// <param name="originalImagePath">The original image path.</param>
  693. /// <param name="item">The item.</param>
  694. /// <param name="imageType">Type of the image.</param>
  695. /// <param name="imageIndex">Index of the image.</param>
  696. /// <param name="supportedEnhancers">The supported enhancers.</param>
  697. /// <param name="cacheGuid">The cache unique identifier.</param>
  698. /// <returns>Task&lt;System.String&gt;.</returns>
  699. /// <exception cref="ArgumentNullException">
  700. /// originalImagePath
  701. /// or
  702. /// item
  703. /// </exception>
  704. private async Task<Tuple<string, bool>> GetEnhancedImageInternal(string originalImagePath,
  705. IHasMetadata item,
  706. ImageType imageType,
  707. int imageIndex,
  708. List<IImageEnhancer> supportedEnhancers,
  709. string cacheGuid)
  710. {
  711. if (string.IsNullOrEmpty(originalImagePath))
  712. {
  713. throw new ArgumentNullException("originalImagePath");
  714. }
  715. if (item == null)
  716. {
  717. throw new ArgumentNullException("item");
  718. }
  719. var treatmentRequiresTransparency = false;
  720. foreach (var enhancer in supportedEnhancers)
  721. {
  722. if (!treatmentRequiresTransparency)
  723. {
  724. treatmentRequiresTransparency = enhancer.GetEnhancedImageInfo(item, originalImagePath, imageType, imageIndex).RequiresTransparency;
  725. }
  726. }
  727. // All enhanced images are saved as png to allow transparency
  728. var cacheExtension = _imageEncoder.SupportedOutputFormats.Contains(ImageFormat.Webp) ?
  729. ".webp" :
  730. (treatmentRequiresTransparency ? ".png" : ".jpg");
  731. var enhancedImagePath = GetCachePath(EnhancedImageCachePath, cacheGuid + cacheExtension);
  732. // Check again in case of contention
  733. if (_fileSystem.FileExists(enhancedImagePath))
  734. {
  735. return new Tuple<string, bool>(enhancedImagePath, treatmentRequiresTransparency);
  736. }
  737. _fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(enhancedImagePath));
  738. var tmpPath = Path.Combine(_appPaths.TempDirectory, Path.ChangeExtension(Guid.NewGuid().ToString(), Path.GetExtension(enhancedImagePath)));
  739. _fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(tmpPath));
  740. await ExecuteImageEnhancers(supportedEnhancers, originalImagePath, tmpPath, item, imageType, imageIndex).ConfigureAwait(false);
  741. try
  742. {
  743. _fileSystem.CopyFile(tmpPath, enhancedImagePath, true);
  744. }
  745. catch
  746. {
  747. }
  748. return new Tuple<string, bool>(tmpPath, treatmentRequiresTransparency);
  749. }
  750. /// <summary>
  751. /// Executes the image enhancers.
  752. /// </summary>
  753. /// <param name="imageEnhancers">The image enhancers.</param>
  754. /// <param name="inputPath">The input path.</param>
  755. /// <param name="outputPath">The output path.</param>
  756. /// <param name="item">The item.</param>
  757. /// <param name="imageType">Type of the image.</param>
  758. /// <param name="imageIndex">Index of the image.</param>
  759. /// <returns>Task{EnhancedImage}.</returns>
  760. private async Task ExecuteImageEnhancers(IEnumerable<IImageEnhancer> imageEnhancers, string inputPath, string outputPath, IHasMetadata item, ImageType imageType, int imageIndex)
  761. {
  762. // Run the enhancers sequentially in order of priority
  763. foreach (var enhancer in imageEnhancers)
  764. {
  765. await enhancer.EnhanceImageAsync(item, inputPath, outputPath, imageType, imageIndex).ConfigureAwait(false);
  766. // Feed the output into the next enhancer as input
  767. inputPath = outputPath;
  768. }
  769. }
  770. /// <summary>
  771. /// Gets the cache path.
  772. /// </summary>
  773. /// <param name="path">The path.</param>
  774. /// <param name="uniqueName">Name of the unique.</param>
  775. /// <param name="fileExtension">The file extension.</param>
  776. /// <returns>System.String.</returns>
  777. /// <exception cref="System.ArgumentNullException">
  778. /// path
  779. /// or
  780. /// uniqueName
  781. /// or
  782. /// fileExtension
  783. /// </exception>
  784. public string GetCachePath(string path, string uniqueName, string fileExtension)
  785. {
  786. if (string.IsNullOrEmpty(path))
  787. {
  788. throw new ArgumentNullException("path");
  789. }
  790. if (string.IsNullOrEmpty(uniqueName))
  791. {
  792. throw new ArgumentNullException("uniqueName");
  793. }
  794. if (string.IsNullOrEmpty(fileExtension))
  795. {
  796. throw new ArgumentNullException("fileExtension");
  797. }
  798. var filename = uniqueName.GetMD5() + fileExtension;
  799. return GetCachePath(path, filename);
  800. }
  801. /// <summary>
  802. /// Gets the cache path.
  803. /// </summary>
  804. /// <param name="path">The path.</param>
  805. /// <param name="filename">The filename.</param>
  806. /// <returns>System.String.</returns>
  807. /// <exception cref="System.ArgumentNullException">
  808. /// path
  809. /// or
  810. /// filename
  811. /// </exception>
  812. public string GetCachePath(string path, string filename)
  813. {
  814. if (string.IsNullOrEmpty(path))
  815. {
  816. throw new ArgumentNullException("path");
  817. }
  818. if (string.IsNullOrEmpty(filename))
  819. {
  820. throw new ArgumentNullException("filename");
  821. }
  822. var prefix = filename.Substring(0, 1);
  823. path = Path.Combine(path, prefix);
  824. return Path.Combine(path, filename);
  825. }
  826. public void CreateImageCollage(ImageCollageOptions options)
  827. {
  828. _logger.Info("Creating image collage and saving to {0}", options.OutputPath);
  829. _imageEncoder.CreateImageCollage(options);
  830. _logger.Info("Completed creation of image collage and saved to {0}", options.OutputPath);
  831. }
  832. public List<IImageEnhancer> GetSupportedEnhancers(IHasMetadata item, ImageType imageType)
  833. {
  834. var list = new List<IImageEnhancer>();
  835. foreach (var i in ImageEnhancers)
  836. {
  837. try
  838. {
  839. if (i.Supports(item, imageType))
  840. {
  841. list.Add(i);
  842. }
  843. }
  844. catch (Exception ex)
  845. {
  846. _logger.ErrorException("Error in image enhancer: {0}", ex, i.GetType().Name);
  847. }
  848. }
  849. return list;
  850. }
  851. private bool _disposed;
  852. public void Dispose()
  853. {
  854. _disposed = true;
  855. var disposable = _imageEncoder as IDisposable;
  856. if (disposable != null)
  857. {
  858. disposable.Dispose();
  859. }
  860. _saveImageSizeTimer.Dispose();
  861. GC.SuppressFinalize(this);
  862. }
  863. private void CheckDisposed()
  864. {
  865. if (_disposed)
  866. {
  867. throw new ObjectDisposedException(GetType().Name);
  868. }
  869. }
  870. }
  871. }