ImageProcessor.cs 33 KB

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