ImageProcessor.cs 33 KB

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