ImageProcessor.cs 32 KB

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