ImageProcessor.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  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, DateTime>> 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. var dateModified = originalImage.DateModified;
  157. if (!_imageEncoder.SupportsImageEncoding)
  158. {
  159. return new Tuple<string, string, DateTime>(originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified);
  160. }
  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, DateTime>(originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified);
  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, DateTime>(originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified);
  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 imageProcessingLockTaken = false;
  202. try
  203. {
  204. CheckDisposed();
  205. if (!_fileSystem.FileExists(cacheFilePath))
  206. {
  207. var newWidth = Convert.ToInt32(newSize.Width);
  208. var newHeight = Convert.ToInt32(newSize.Height);
  209. _fileSystem.CreateDirectory(Path.GetDirectoryName(cacheFilePath));
  210. var tmpPath = Path.ChangeExtension(Path.Combine(_appPaths.TempDirectory, Guid.NewGuid().ToString("N")), Path.GetExtension(cacheFilePath));
  211. _fileSystem.CreateDirectory(Path.GetDirectoryName(tmpPath));
  212. await _imageProcessingSemaphore.WaitAsync().ConfigureAwait(false);
  213. imageProcessingLockTaken = true;
  214. _imageEncoder.EncodeImage(originalImagePath, tmpPath, AutoOrient(options.Item), newWidth, newHeight, quality, options, outputFormat);
  215. CopyFile(tmpPath, cacheFilePath);
  216. return new Tuple<string, string, DateTime>(tmpPath, GetMimeType(outputFormat, cacheFilePath), _fileSystem.GetLastWriteTimeUtc(tmpPath));
  217. }
  218. return new Tuple<string, string, DateTime>(cacheFilePath, GetMimeType(outputFormat, cacheFilePath), _fileSystem.GetLastWriteTimeUtc(cacheFilePath));
  219. }
  220. catch (Exception ex)
  221. {
  222. // If it fails for whatever reason, return the original image
  223. _logger.ErrorException("Error encoding image", ex);
  224. // Just spit out the original file if all the options are default
  225. return new Tuple<string, string, DateTime>(originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified);
  226. }
  227. finally
  228. {
  229. if (imageProcessingLockTaken)
  230. {
  231. _imageProcessingSemaphore.Release();
  232. }
  233. }
  234. }
  235. private void CopyFile(string src, string destination)
  236. {
  237. try
  238. {
  239. File.Copy(src, destination, true);
  240. }
  241. catch
  242. {
  243. }
  244. }
  245. private bool AutoOrient(IHasImages item)
  246. {
  247. var photo = item as Photo;
  248. if (photo != null && photo.Orientation.HasValue)
  249. {
  250. return true;
  251. }
  252. return false;
  253. }
  254. //private static int[][] OPERATIONS = new int[][] {
  255. // TopLeft
  256. //new int[] { 0, NONE},
  257. // TopRight
  258. //new int[] { 0, HORIZONTAL},
  259. //new int[] {180, NONE},
  260. // LeftTop
  261. //new int[] { 0, VERTICAL},
  262. //new int[] { 90, HORIZONTAL},
  263. // RightTop
  264. //new int[] { 90, NONE},
  265. //new int[] {-90, HORIZONTAL},
  266. //new int[] {-90, NONE},
  267. //};
  268. private string GetMimeType(ImageFormat format, string path)
  269. {
  270. if (format == ImageFormat.Bmp)
  271. {
  272. return MimeTypes.GetMimeType("i.bmp");
  273. }
  274. if (format == ImageFormat.Gif)
  275. {
  276. return MimeTypes.GetMimeType("i.gif");
  277. }
  278. if (format == ImageFormat.Jpg)
  279. {
  280. return MimeTypes.GetMimeType("i.jpg");
  281. }
  282. if (format == ImageFormat.Png)
  283. {
  284. return MimeTypes.GetMimeType("i.png");
  285. }
  286. if (format == ImageFormat.Webp)
  287. {
  288. return MimeTypes.GetMimeType("i.webp");
  289. }
  290. return MimeTypes.GetMimeType(path);
  291. }
  292. private ImageSize GetNewImageSize(ImageProcessingOptions options, ImageSize? originalImageSize)
  293. {
  294. if (originalImageSize.HasValue)
  295. {
  296. // Determine the output size based on incoming parameters
  297. var newSize = DrawingUtils.Resize(originalImageSize.Value, options.Width, options.Height, options.MaxWidth, options.MaxHeight);
  298. return newSize;
  299. }
  300. return GetSizeEstimate(options);
  301. }
  302. private ImageSize GetSizeEstimate(ImageProcessingOptions options)
  303. {
  304. if (options.Width.HasValue && options.Height.HasValue)
  305. {
  306. return new ImageSize(options.Width.Value, options.Height.Value);
  307. }
  308. var aspect = GetEstimatedAspectRatio(options.Image.Type);
  309. var width = options.Width ?? options.MaxWidth;
  310. if (width.HasValue)
  311. {
  312. var heightValue = aspect / width.Value;
  313. return new ImageSize(width.Value, Convert.ToInt32(heightValue));
  314. }
  315. var height = options.Height ?? options.MaxHeight ?? 200;
  316. var widthValue = aspect * height;
  317. return new ImageSize(Convert.ToInt32(widthValue), height);
  318. }
  319. private double GetEstimatedAspectRatio(ImageType type)
  320. {
  321. switch (type)
  322. {
  323. case ImageType.Art:
  324. case ImageType.Backdrop:
  325. case ImageType.Chapter:
  326. case ImageType.Screenshot:
  327. case ImageType.Thumb:
  328. return 1.78;
  329. case ImageType.Banner:
  330. return 5.4;
  331. case ImageType.Box:
  332. case ImageType.BoxRear:
  333. case ImageType.Disc:
  334. case ImageType.Menu:
  335. return 1;
  336. case ImageType.Logo:
  337. return 2.58;
  338. case ImageType.Primary:
  339. return .667;
  340. default:
  341. return 1;
  342. }
  343. }
  344. private ImageFormat GetOutputFormat(ImageFormat requestedFormat)
  345. {
  346. if (requestedFormat == ImageFormat.Webp && !_imageEncoder.SupportedOutputFormats.Contains(ImageFormat.Webp))
  347. {
  348. return ImageFormat.Png;
  349. }
  350. return requestedFormat;
  351. }
  352. /// <summary>
  353. /// Crops whitespace from an image, caches the result, and returns the cached path
  354. /// </summary>
  355. private async Task<Tuple<string, DateTime>> GetWhitespaceCroppedImage(string originalImagePath, DateTime dateModified)
  356. {
  357. var name = originalImagePath;
  358. name += "datemodified=" + dateModified.Ticks;
  359. var croppedImagePath = GetCachePath(CroppedWhitespaceImageCachePath, name, Path.GetExtension(originalImagePath));
  360. // Check again in case of contention
  361. if (_fileSystem.FileExists(croppedImagePath))
  362. {
  363. return GetResult(croppedImagePath);
  364. }
  365. var imageProcessingLockTaken = false;
  366. try
  367. {
  368. _fileSystem.CreateDirectory(Path.GetDirectoryName(croppedImagePath));
  369. var tmpPath = Path.ChangeExtension(Path.Combine(_appPaths.TempDirectory, Guid.NewGuid().ToString("N")), Path.GetExtension(croppedImagePath));
  370. _fileSystem.CreateDirectory(Path.GetDirectoryName(tmpPath));
  371. await _imageProcessingSemaphore.WaitAsync().ConfigureAwait(false);
  372. imageProcessingLockTaken = true;
  373. _imageEncoder.CropWhiteSpace(originalImagePath, tmpPath);
  374. CopyFile(tmpPath, croppedImagePath);
  375. return GetResult(tmpPath);
  376. }
  377. catch (NotImplementedException)
  378. {
  379. // No need to spam the log with an error message
  380. return new Tuple<string, DateTime>(originalImagePath, dateModified);
  381. }
  382. catch (Exception ex)
  383. {
  384. // We have to have a catch-all here because some of the .net image methods throw a plain old Exception
  385. _logger.ErrorException("Error cropping image {0}", ex, originalImagePath);
  386. return new Tuple<string, DateTime>(originalImagePath, dateModified);
  387. }
  388. finally
  389. {
  390. if (imageProcessingLockTaken)
  391. {
  392. _imageProcessingSemaphore.Release();
  393. }
  394. }
  395. }
  396. private Tuple<string, DateTime> GetResult(string path)
  397. {
  398. return new Tuple<string, DateTime>(path, _fileSystem.GetLastWriteTimeUtc(path));
  399. }
  400. /// <summary>
  401. /// Increment this when there's a change requiring caches to be invalidated
  402. /// </summary>
  403. private const string Version = "3";
  404. /// <summary>
  405. /// Gets the cache file path based on a set of parameters
  406. /// </summary>
  407. private string GetCacheFilePath(string originalPath, ImageSize outputSize, int quality, DateTime dateModified, ImageFormat format, bool addPlayedIndicator, double percentPlayed, int? unwatchedCount, string backgroundColor, string foregroundLayer)
  408. {
  409. var filename = originalPath;
  410. filename += "width=" + outputSize.Width;
  411. filename += "height=" + outputSize.Height;
  412. filename += "quality=" + quality;
  413. filename += "datemodified=" + dateModified.Ticks;
  414. filename += "f=" + format;
  415. if (addPlayedIndicator)
  416. {
  417. filename += "pl=true";
  418. }
  419. if (percentPlayed > 0)
  420. {
  421. filename += "p=" + percentPlayed;
  422. }
  423. if (unwatchedCount.HasValue)
  424. {
  425. filename += "p=" + unwatchedCount.Value;
  426. }
  427. if (!string.IsNullOrEmpty(backgroundColor))
  428. {
  429. filename += "b=" + backgroundColor;
  430. }
  431. if (!string.IsNullOrEmpty(foregroundLayer))
  432. {
  433. filename += "fl=" + foregroundLayer;
  434. }
  435. filename += "v=" + Version;
  436. return GetCachePath(ResizedImageCachePath, filename, "." + format.ToString().ToLower());
  437. }
  438. public ImageSize GetImageSize(ItemImageInfo info)
  439. {
  440. return GetImageSize(info.Path, info.DateModified, false);
  441. }
  442. public ImageSize GetImageSize(string path)
  443. {
  444. return GetImageSize(path, _fileSystem.GetLastWriteTimeUtc(path), false);
  445. }
  446. /// <summary>
  447. /// Gets the size of the image.
  448. /// </summary>
  449. /// <param name="path">The path.</param>
  450. /// <param name="imageDateModified">The image date modified.</param>
  451. /// <param name="allowSlowMethod">if set to <c>true</c> [allow slow method].</param>
  452. /// <returns>ImageSize.</returns>
  453. /// <exception cref="System.ArgumentNullException">path</exception>
  454. private ImageSize GetImageSize(string path, DateTime imageDateModified, bool allowSlowMethod)
  455. {
  456. if (string.IsNullOrEmpty(path))
  457. {
  458. throw new ArgumentNullException("path");
  459. }
  460. var name = path + "datemodified=" + imageDateModified.Ticks;
  461. ImageSize size;
  462. var cacheHash = name.GetMD5();
  463. if (!_cachedImagedSizes.TryGetValue(cacheHash, out size))
  464. {
  465. size = GetImageSizeInternal(path, allowSlowMethod);
  466. if (size.Width > 0 && size.Height > 0)
  467. {
  468. StartSaveImageSizeTimer();
  469. _cachedImagedSizes.AddOrUpdate(cacheHash, size, (keyName, oldValue) => size);
  470. }
  471. }
  472. return size;
  473. }
  474. /// <summary>
  475. /// Gets the image size internal.
  476. /// </summary>
  477. /// <param name="path">The path.</param>
  478. /// <param name="allowSlowMethod">if set to <c>true</c> [allow slow method].</param>
  479. /// <returns>ImageSize.</returns>
  480. private ImageSize GetImageSizeInternal(string path, bool allowSlowMethod)
  481. {
  482. try
  483. {
  484. using (var file = TagLib.File.Create(path))
  485. {
  486. var image = file as TagLib.Image.File;
  487. var properties = image.Properties;
  488. return new ImageSize
  489. {
  490. Height = properties.PhotoHeight,
  491. Width = properties.PhotoWidth
  492. };
  493. }
  494. }
  495. catch
  496. {
  497. }
  498. return ImageHeader.GetDimensions(path, _logger, _fileSystem);
  499. }
  500. private readonly Timer _saveImageSizeTimer;
  501. private const int SaveImageSizeTimeout = 5000;
  502. private readonly object _saveImageSizeLock = new object();
  503. private void StartSaveImageSizeTimer()
  504. {
  505. _saveImageSizeTimer.Change(SaveImageSizeTimeout, Timeout.Infinite);
  506. }
  507. private void SaveImageSizeCallback(object state)
  508. {
  509. lock (_saveImageSizeLock)
  510. {
  511. try
  512. {
  513. var path = ImageSizeFile;
  514. _fileSystem.CreateDirectory(Path.GetDirectoryName(path));
  515. _jsonSerializer.SerializeToFile(_cachedImagedSizes, path);
  516. }
  517. catch (Exception ex)
  518. {
  519. _logger.ErrorException("Error saving image size file", ex);
  520. }
  521. }
  522. }
  523. private string ImageSizeFile
  524. {
  525. get
  526. {
  527. return Path.Combine(_appPaths.DataPath, "imagesizes.json");
  528. }
  529. }
  530. /// <summary>
  531. /// Gets the image cache tag.
  532. /// </summary>
  533. /// <param name="item">The item.</param>
  534. /// <param name="image">The image.</param>
  535. /// <returns>Guid.</returns>
  536. /// <exception cref="System.ArgumentNullException">item</exception>
  537. public string GetImageCacheTag(IHasImages item, ItemImageInfo image)
  538. {
  539. if (item == null)
  540. {
  541. throw new ArgumentNullException("item");
  542. }
  543. if (image == null)
  544. {
  545. throw new ArgumentNullException("image");
  546. }
  547. var supportedEnhancers = GetSupportedEnhancers(item, image.Type);
  548. return GetImageCacheTag(item, image, supportedEnhancers.ToList());
  549. }
  550. /// <summary>
  551. /// Gets the image cache tag.
  552. /// </summary>
  553. /// <param name="item">The item.</param>
  554. /// <param name="image">The image.</param>
  555. /// <param name="imageEnhancers">The image enhancers.</param>
  556. /// <returns>Guid.</returns>
  557. /// <exception cref="System.ArgumentNullException">item</exception>
  558. public string GetImageCacheTag(IHasImages item, ItemImageInfo image, List<IImageEnhancer> imageEnhancers)
  559. {
  560. if (item == null)
  561. {
  562. throw new ArgumentNullException("item");
  563. }
  564. if (imageEnhancers == null)
  565. {
  566. throw new ArgumentNullException("imageEnhancers");
  567. }
  568. if (image == null)
  569. {
  570. throw new ArgumentNullException("image");
  571. }
  572. var originalImagePath = image.Path;
  573. var dateModified = image.DateModified;
  574. var imageType = image.Type;
  575. // Optimization
  576. if (imageEnhancers.Count == 0)
  577. {
  578. return (originalImagePath + dateModified.Ticks).GetMD5().ToString("N");
  579. }
  580. // Cache name is created with supported enhancers combined with the last config change so we pick up new config changes
  581. var cacheKeys = imageEnhancers.Select(i => i.GetConfigurationCacheKey(item, imageType)).ToList();
  582. cacheKeys.Add(originalImagePath + dateModified.Ticks);
  583. return string.Join("|", cacheKeys.ToArray()).GetMD5().ToString("N");
  584. }
  585. /// <summary>
  586. /// Gets the enhanced image.
  587. /// </summary>
  588. /// <param name="item">The item.</param>
  589. /// <param name="imageType">Type of the image.</param>
  590. /// <param name="imageIndex">Index of the image.</param>
  591. /// <returns>Task{System.String}.</returns>
  592. public async Task<string> GetEnhancedImage(IHasImages item, ImageType imageType, int imageIndex)
  593. {
  594. var enhancers = GetSupportedEnhancers(item, imageType).ToList();
  595. var imageInfo = item.GetImageInfo(imageType, imageIndex);
  596. var result = await GetEnhancedImage(imageInfo, item, imageIndex, enhancers);
  597. return result.Item1;
  598. }
  599. private async Task<Tuple<string, DateTime>> GetEnhancedImage(ItemImageInfo image,
  600. IHasImages item,
  601. int imageIndex,
  602. List<IImageEnhancer> enhancers)
  603. {
  604. var originalImagePath = image.Path;
  605. var dateModified = image.DateModified;
  606. var imageType = image.Type;
  607. try
  608. {
  609. var cacheGuid = GetImageCacheTag(item, image, enhancers);
  610. // Enhance if we have enhancers
  611. var ehnancedImagePath = await GetEnhancedImageInternal(originalImagePath, item, imageType, imageIndex, enhancers, cacheGuid).ConfigureAwait(false);
  612. // If the path changed update dateModified
  613. if (!ehnancedImagePath.Equals(originalImagePath, StringComparison.OrdinalIgnoreCase))
  614. {
  615. return GetResult(ehnancedImagePath);
  616. }
  617. }
  618. catch (Exception ex)
  619. {
  620. _logger.Error("Error enhancing image", ex);
  621. }
  622. return new Tuple<string, DateTime>(originalImagePath, dateModified);
  623. }
  624. /// <summary>
  625. /// Gets the enhanced image internal.
  626. /// </summary>
  627. /// <param name="originalImagePath">The original image path.</param>
  628. /// <param name="item">The item.</param>
  629. /// <param name="imageType">Type of the image.</param>
  630. /// <param name="imageIndex">Index of the image.</param>
  631. /// <param name="supportedEnhancers">The supported enhancers.</param>
  632. /// <param name="cacheGuid">The cache unique identifier.</param>
  633. /// <returns>Task&lt;System.String&gt;.</returns>
  634. /// <exception cref="ArgumentNullException">
  635. /// originalImagePath
  636. /// or
  637. /// item
  638. /// </exception>
  639. private async Task<string> GetEnhancedImageInternal(string originalImagePath,
  640. IHasImages item,
  641. ImageType imageType,
  642. int imageIndex,
  643. IEnumerable<IImageEnhancer> supportedEnhancers,
  644. string cacheGuid)
  645. {
  646. if (string.IsNullOrEmpty(originalImagePath))
  647. {
  648. throw new ArgumentNullException("originalImagePath");
  649. }
  650. if (item == null)
  651. {
  652. throw new ArgumentNullException("item");
  653. }
  654. // All enhanced images are saved as png to allow transparency
  655. var enhancedImagePath = GetCachePath(EnhancedImageCachePath, cacheGuid + ".png");
  656. var semaphore = GetLock(enhancedImagePath);
  657. await semaphore.WaitAsync().ConfigureAwait(false);
  658. // Check again in case of contention
  659. if (_fileSystem.FileExists(enhancedImagePath))
  660. {
  661. semaphore.Release();
  662. return enhancedImagePath;
  663. }
  664. var imageProcessingLockTaken = false;
  665. try
  666. {
  667. _fileSystem.CreateDirectory(Path.GetDirectoryName(enhancedImagePath));
  668. await _imageProcessingSemaphore.WaitAsync().ConfigureAwait(false);
  669. imageProcessingLockTaken = true;
  670. await ExecuteImageEnhancers(supportedEnhancers, originalImagePath, enhancedImagePath, item, imageType, imageIndex).ConfigureAwait(false);
  671. }
  672. finally
  673. {
  674. if (imageProcessingLockTaken)
  675. {
  676. _imageProcessingSemaphore.Release();
  677. }
  678. semaphore.Release();
  679. }
  680. return enhancedImagePath;
  681. }
  682. /// <summary>
  683. /// Executes the image enhancers.
  684. /// </summary>
  685. /// <param name="imageEnhancers">The image enhancers.</param>
  686. /// <param name="inputPath">The input path.</param>
  687. /// <param name="outputPath">The output path.</param>
  688. /// <param name="item">The item.</param>
  689. /// <param name="imageType">Type of the image.</param>
  690. /// <param name="imageIndex">Index of the image.</param>
  691. /// <returns>Task{EnhancedImage}.</returns>
  692. private async Task ExecuteImageEnhancers(IEnumerable<IImageEnhancer> imageEnhancers, string inputPath, string outputPath, IHasImages item, ImageType imageType, int imageIndex)
  693. {
  694. // Run the enhancers sequentially in order of priority
  695. foreach (var enhancer in imageEnhancers)
  696. {
  697. var typeName = enhancer.GetType().Name;
  698. try
  699. {
  700. await enhancer.EnhanceImageAsync(item, inputPath, outputPath, imageType, imageIndex).ConfigureAwait(false);
  701. }
  702. catch (Exception ex)
  703. {
  704. _logger.ErrorException("{0} failed enhancing {1}", ex, typeName, item.Name);
  705. throw;
  706. }
  707. // Feed the output into the next enhancer as input
  708. inputPath = outputPath;
  709. }
  710. }
  711. /// <summary>
  712. /// The _semaphoreLocks
  713. /// </summary>
  714. private readonly ConcurrentDictionary<string, SemaphoreSlim> _semaphoreLocks = new ConcurrentDictionary<string, SemaphoreSlim>();
  715. /// <summary>
  716. /// Gets the lock.
  717. /// </summary>
  718. /// <param name="filename">The filename.</param>
  719. /// <returns>System.Object.</returns>
  720. private SemaphoreSlim GetLock(string filename)
  721. {
  722. return _semaphoreLocks.GetOrAdd(filename, key => new SemaphoreSlim(1, 1));
  723. }
  724. /// <summary>
  725. /// Gets the cache path.
  726. /// </summary>
  727. /// <param name="path">The path.</param>
  728. /// <param name="uniqueName">Name of the unique.</param>
  729. /// <param name="fileExtension">The file extension.</param>
  730. /// <returns>System.String.</returns>
  731. /// <exception cref="System.ArgumentNullException">
  732. /// path
  733. /// or
  734. /// uniqueName
  735. /// or
  736. /// fileExtension
  737. /// </exception>
  738. public string GetCachePath(string path, string uniqueName, string fileExtension)
  739. {
  740. if (string.IsNullOrEmpty(path))
  741. {
  742. throw new ArgumentNullException("path");
  743. }
  744. if (string.IsNullOrEmpty(uniqueName))
  745. {
  746. throw new ArgumentNullException("uniqueName");
  747. }
  748. if (string.IsNullOrEmpty(fileExtension))
  749. {
  750. throw new ArgumentNullException("fileExtension");
  751. }
  752. var filename = uniqueName.GetMD5() + fileExtension;
  753. return GetCachePath(path, filename);
  754. }
  755. /// <summary>
  756. /// Gets the cache path.
  757. /// </summary>
  758. /// <param name="path">The path.</param>
  759. /// <param name="filename">The filename.</param>
  760. /// <returns>System.String.</returns>
  761. /// <exception cref="System.ArgumentNullException">
  762. /// path
  763. /// or
  764. /// filename
  765. /// </exception>
  766. public string GetCachePath(string path, string filename)
  767. {
  768. if (string.IsNullOrEmpty(path))
  769. {
  770. throw new ArgumentNullException("path");
  771. }
  772. if (string.IsNullOrEmpty(filename))
  773. {
  774. throw new ArgumentNullException("filename");
  775. }
  776. var prefix = filename.Substring(0, 1);
  777. path = Path.Combine(path, prefix);
  778. return Path.Combine(path, filename);
  779. }
  780. public async Task CreateImageCollage(ImageCollageOptions options)
  781. {
  782. await _imageProcessingSemaphore.WaitAsync().ConfigureAwait(false);
  783. try
  784. {
  785. _logger.Info("Creating image collage and saving to {0}", options.OutputPath);
  786. _imageEncoder.CreateImageCollage(options);
  787. _logger.Info("Completed creation of image collage and saved to {0}", options.OutputPath);
  788. }
  789. finally
  790. {
  791. _imageProcessingSemaphore.Release();
  792. }
  793. }
  794. public IEnumerable<IImageEnhancer> GetSupportedEnhancers(IHasImages item, ImageType imageType)
  795. {
  796. return ImageEnhancers.Where(i =>
  797. {
  798. try
  799. {
  800. return i.Supports(item, imageType);
  801. }
  802. catch (Exception ex)
  803. {
  804. _logger.ErrorException("Error in image enhancer: {0}", ex, i.GetType().Name);
  805. return false;
  806. }
  807. });
  808. }
  809. private bool _disposed;
  810. public void Dispose()
  811. {
  812. _disposed = true;
  813. _imageEncoder.Dispose();
  814. _saveImageSizeTimer.Dispose();
  815. }
  816. private void CheckDisposed()
  817. {
  818. if (_disposed)
  819. {
  820. throw new ObjectDisposedException(GetType().Name);
  821. }
  822. }
  823. }
  824. }