ImageProcessor.cs 33 KB

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