ImageProcessor.cs 32 KB

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