ImageProcessor.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  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.Blur, 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. try
  370. {
  371. _fileSystem.CreateDirectory(Path.GetDirectoryName(croppedImagePath));
  372. var tmpPath = Path.ChangeExtension(Path.Combine(_appPaths.TempDirectory, Guid.NewGuid().ToString("N")), Path.GetExtension(croppedImagePath));
  373. _fileSystem.CreateDirectory(Path.GetDirectoryName(tmpPath));
  374. _imageEncoder.CropWhiteSpace(originalImagePath, tmpPath);
  375. CopyFile(tmpPath, croppedImagePath);
  376. return GetResult(tmpPath);
  377. }
  378. catch (NotImplementedException)
  379. {
  380. // No need to spam the log with an error message
  381. return new Tuple<string, DateTime>(originalImagePath, dateModified);
  382. }
  383. catch (Exception ex)
  384. {
  385. // We have to have a catch-all here because some of the .net image methods throw a plain old Exception
  386. _logger.ErrorException("Error cropping image {0}", ex, originalImagePath);
  387. return new Tuple<string, DateTime>(originalImagePath, dateModified);
  388. }
  389. }
  390. private Tuple<string, DateTime> GetResult(string path)
  391. {
  392. return new Tuple<string, DateTime>(path, _fileSystem.GetLastWriteTimeUtc(path));
  393. }
  394. /// <summary>
  395. /// Increment this when there's a change requiring caches to be invalidated
  396. /// </summary>
  397. private const string Version = "3";
  398. /// <summary>
  399. /// Gets the cache file path based on a set of parameters
  400. /// </summary>
  401. private string GetCacheFilePath(string originalPath, ImageSize outputSize, int quality, DateTime dateModified, ImageFormat format, bool addPlayedIndicator, double percentPlayed, int? unwatchedCount, int? blur, string backgroundColor, string foregroundLayer)
  402. {
  403. var filename = originalPath;
  404. filename += "width=" + outputSize.Width;
  405. filename += "height=" + outputSize.Height;
  406. filename += "quality=" + quality;
  407. filename += "datemodified=" + dateModified.Ticks;
  408. filename += "f=" + format;
  409. if (addPlayedIndicator)
  410. {
  411. filename += "pl=true";
  412. }
  413. if (percentPlayed > 0)
  414. {
  415. filename += "p=" + percentPlayed;
  416. }
  417. if (unwatchedCount.HasValue)
  418. {
  419. filename += "p=" + unwatchedCount.Value;
  420. }
  421. if (blur.HasValue)
  422. {
  423. filename += "blur=" + blur.Value;
  424. }
  425. if (!string.IsNullOrEmpty(backgroundColor))
  426. {
  427. filename += "b=" + backgroundColor;
  428. }
  429. if (!string.IsNullOrEmpty(foregroundLayer))
  430. {
  431. filename += "fl=" + foregroundLayer;
  432. }
  433. filename += "v=" + Version;
  434. return GetCachePath(ResizedImageCachePath, filename, "." + format.ToString().ToLower());
  435. }
  436. public ImageSize GetImageSize(ItemImageInfo info)
  437. {
  438. return GetImageSize(info.Path, info.DateModified, false);
  439. }
  440. public ImageSize GetImageSize(string path)
  441. {
  442. return GetImageSize(path, _fileSystem.GetLastWriteTimeUtc(path), false);
  443. }
  444. /// <summary>
  445. /// Gets the size of the image.
  446. /// </summary>
  447. /// <param name="path">The path.</param>
  448. /// <param name="imageDateModified">The image date modified.</param>
  449. /// <param name="allowSlowMethod">if set to <c>true</c> [allow slow method].</param>
  450. /// <returns>ImageSize.</returns>
  451. /// <exception cref="System.ArgumentNullException">path</exception>
  452. private ImageSize GetImageSize(string path, DateTime imageDateModified, bool allowSlowMethod)
  453. {
  454. if (string.IsNullOrEmpty(path))
  455. {
  456. throw new ArgumentNullException("path");
  457. }
  458. var name = path + "datemodified=" + imageDateModified.Ticks;
  459. ImageSize size;
  460. var cacheHash = name.GetMD5();
  461. if (!_cachedImagedSizes.TryGetValue(cacheHash, out size))
  462. {
  463. size = GetImageSizeInternal(path, allowSlowMethod);
  464. if (size.Width > 0 && size.Height > 0)
  465. {
  466. StartSaveImageSizeTimer();
  467. _cachedImagedSizes.AddOrUpdate(cacheHash, size, (keyName, oldValue) => size);
  468. }
  469. }
  470. return size;
  471. }
  472. /// <summary>
  473. /// Gets the image size internal.
  474. /// </summary>
  475. /// <param name="path">The path.</param>
  476. /// <param name="allowSlowMethod">if set to <c>true</c> [allow slow method].</param>
  477. /// <returns>ImageSize.</returns>
  478. private ImageSize GetImageSizeInternal(string path, bool allowSlowMethod)
  479. {
  480. try
  481. {
  482. using (var file = TagLib.File.Create(new StreamFileAbstraction(Path.GetFileName(path), _fileSystem.OpenRead(path), null)))
  483. {
  484. var image = file as TagLib.Image.File;
  485. var properties = image.Properties;
  486. return new ImageSize
  487. {
  488. Height = properties.PhotoHeight,
  489. Width = properties.PhotoWidth
  490. };
  491. }
  492. }
  493. catch
  494. {
  495. }
  496. return ImageHeader.GetDimensions(path, _logger, _fileSystem);
  497. }
  498. private readonly ITimer _saveImageSizeTimer;
  499. private const int SaveImageSizeTimeout = 5000;
  500. private readonly object _saveImageSizeLock = new object();
  501. private void StartSaveImageSizeTimer()
  502. {
  503. _saveImageSizeTimer.Change(SaveImageSizeTimeout, Timeout.Infinite);
  504. }
  505. private void SaveImageSizeCallback(object state)
  506. {
  507. lock (_saveImageSizeLock)
  508. {
  509. try
  510. {
  511. var path = ImageSizeFile;
  512. _fileSystem.CreateDirectory(Path.GetDirectoryName(path));
  513. _jsonSerializer.SerializeToFile(_cachedImagedSizes, path);
  514. }
  515. catch (Exception ex)
  516. {
  517. _logger.ErrorException("Error saving image size file", ex);
  518. }
  519. }
  520. }
  521. private string ImageSizeFile
  522. {
  523. get
  524. {
  525. return Path.Combine(_appPaths.DataPath, "imagesizes.json");
  526. }
  527. }
  528. /// <summary>
  529. /// Gets the image cache tag.
  530. /// </summary>
  531. /// <param name="item">The item.</param>
  532. /// <param name="image">The image.</param>
  533. /// <returns>Guid.</returns>
  534. /// <exception cref="System.ArgumentNullException">item</exception>
  535. public string GetImageCacheTag(IHasImages item, ItemImageInfo image)
  536. {
  537. if (item == null)
  538. {
  539. throw new ArgumentNullException("item");
  540. }
  541. if (image == null)
  542. {
  543. throw new ArgumentNullException("image");
  544. }
  545. var supportedEnhancers = GetSupportedEnhancers(item, image.Type);
  546. return GetImageCacheTag(item, image, supportedEnhancers.ToList());
  547. }
  548. /// <summary>
  549. /// Gets the image cache tag.
  550. /// </summary>
  551. /// <param name="item">The item.</param>
  552. /// <param name="image">The image.</param>
  553. /// <param name="imageEnhancers">The image enhancers.</param>
  554. /// <returns>Guid.</returns>
  555. /// <exception cref="System.ArgumentNullException">item</exception>
  556. public string GetImageCacheTag(IHasImages item, ItemImageInfo image, List<IImageEnhancer> imageEnhancers)
  557. {
  558. if (item == null)
  559. {
  560. throw new ArgumentNullException("item");
  561. }
  562. if (imageEnhancers == null)
  563. {
  564. throw new ArgumentNullException("imageEnhancers");
  565. }
  566. if (image == null)
  567. {
  568. throw new ArgumentNullException("image");
  569. }
  570. var originalImagePath = image.Path;
  571. var dateModified = image.DateModified;
  572. var imageType = image.Type;
  573. // Optimization
  574. if (imageEnhancers.Count == 0)
  575. {
  576. return (originalImagePath + dateModified.Ticks).GetMD5().ToString("N");
  577. }
  578. // Cache name is created with supported enhancers combined with the last config change so we pick up new config changes
  579. var cacheKeys = imageEnhancers.Select(i => i.GetConfigurationCacheKey(item, imageType)).ToList();
  580. cacheKeys.Add(originalImagePath + dateModified.Ticks);
  581. return string.Join("|", cacheKeys.ToArray()).GetMD5().ToString("N");
  582. }
  583. /// <summary>
  584. /// Gets the enhanced image.
  585. /// </summary>
  586. /// <param name="item">The item.</param>
  587. /// <param name="imageType">Type of the image.</param>
  588. /// <param name="imageIndex">Index of the image.</param>
  589. /// <returns>Task{System.String}.</returns>
  590. public async Task<string> GetEnhancedImage(IHasImages item, ImageType imageType, int imageIndex)
  591. {
  592. var enhancers = GetSupportedEnhancers(item, imageType).ToList();
  593. var imageInfo = item.GetImageInfo(imageType, imageIndex);
  594. var result = await GetEnhancedImage(imageInfo, item, imageIndex, enhancers);
  595. return result.Item1;
  596. }
  597. private async Task<Tuple<string, DateTime>> GetEnhancedImage(ItemImageInfo image,
  598. IHasImages item,
  599. int imageIndex,
  600. List<IImageEnhancer> enhancers)
  601. {
  602. var originalImagePath = image.Path;
  603. var dateModified = image.DateModified;
  604. var imageType = image.Type;
  605. try
  606. {
  607. var cacheGuid = GetImageCacheTag(item, image, enhancers);
  608. // Enhance if we have enhancers
  609. var ehnancedImagePath = await GetEnhancedImageInternal(originalImagePath, item, imageType, imageIndex, enhancers, cacheGuid).ConfigureAwait(false);
  610. // If the path changed update dateModified
  611. if (!ehnancedImagePath.Equals(originalImagePath, StringComparison.OrdinalIgnoreCase))
  612. {
  613. return GetResult(ehnancedImagePath);
  614. }
  615. }
  616. catch (Exception ex)
  617. {
  618. _logger.Error("Error enhancing image", ex);
  619. }
  620. return new Tuple<string, DateTime>(originalImagePath, dateModified);
  621. }
  622. /// <summary>
  623. /// Gets the enhanced image internal.
  624. /// </summary>
  625. /// <param name="originalImagePath">The original image path.</param>
  626. /// <param name="item">The item.</param>
  627. /// <param name="imageType">Type of the image.</param>
  628. /// <param name="imageIndex">Index of the image.</param>
  629. /// <param name="supportedEnhancers">The supported enhancers.</param>
  630. /// <param name="cacheGuid">The cache unique identifier.</param>
  631. /// <returns>Task&lt;System.String&gt;.</returns>
  632. /// <exception cref="ArgumentNullException">
  633. /// originalImagePath
  634. /// or
  635. /// item
  636. /// </exception>
  637. private async Task<string> GetEnhancedImageInternal(string originalImagePath,
  638. IHasImages item,
  639. ImageType imageType,
  640. int imageIndex,
  641. IEnumerable<IImageEnhancer> supportedEnhancers,
  642. string cacheGuid)
  643. {
  644. if (string.IsNullOrEmpty(originalImagePath))
  645. {
  646. throw new ArgumentNullException("originalImagePath");
  647. }
  648. if (item == null)
  649. {
  650. throw new ArgumentNullException("item");
  651. }
  652. // All enhanced images are saved as png to allow transparency
  653. var enhancedImagePath = GetCachePath(EnhancedImageCachePath, cacheGuid + ".png");
  654. // Check again in case of contention
  655. if (_fileSystem.FileExists(enhancedImagePath))
  656. {
  657. return enhancedImagePath;
  658. }
  659. _fileSystem.CreateDirectory(Path.GetDirectoryName(enhancedImagePath));
  660. var tmpPath = Path.Combine(_appPaths.TempDirectory, Path.ChangeExtension(Guid.NewGuid().ToString(), Path.GetExtension(enhancedImagePath)));
  661. _fileSystem.CreateDirectory(Path.GetDirectoryName(tmpPath));
  662. await _imageProcessingSemaphore.WaitAsync().ConfigureAwait(false);
  663. try
  664. {
  665. await ExecuteImageEnhancers(supportedEnhancers, originalImagePath, tmpPath, item, imageType, imageIndex).ConfigureAwait(false);
  666. try
  667. {
  668. _fileSystem.CopyFile(tmpPath, enhancedImagePath, true);
  669. }
  670. catch
  671. {
  672. }
  673. }
  674. finally
  675. {
  676. _imageProcessingSemaphore.Release();
  677. }
  678. return tmpPath;
  679. }
  680. /// <summary>
  681. /// Executes the image enhancers.
  682. /// </summary>
  683. /// <param name="imageEnhancers">The image enhancers.</param>
  684. /// <param name="inputPath">The input path.</param>
  685. /// <param name="outputPath">The output path.</param>
  686. /// <param name="item">The item.</param>
  687. /// <param name="imageType">Type of the image.</param>
  688. /// <param name="imageIndex">Index of the image.</param>
  689. /// <returns>Task{EnhancedImage}.</returns>
  690. private async Task ExecuteImageEnhancers(IEnumerable<IImageEnhancer> imageEnhancers, string inputPath, string outputPath, IHasImages item, ImageType imageType, int imageIndex)
  691. {
  692. // Run the enhancers sequentially in order of priority
  693. foreach (var enhancer in imageEnhancers)
  694. {
  695. await enhancer.EnhanceImageAsync(item, inputPath, outputPath, imageType, imageIndex).ConfigureAwait(false);
  696. // Feed the output into the next enhancer as input
  697. inputPath = outputPath;
  698. }
  699. }
  700. /// <summary>
  701. /// Gets the cache path.
  702. /// </summary>
  703. /// <param name="path">The path.</param>
  704. /// <param name="uniqueName">Name of the unique.</param>
  705. /// <param name="fileExtension">The file extension.</param>
  706. /// <returns>System.String.</returns>
  707. /// <exception cref="System.ArgumentNullException">
  708. /// path
  709. /// or
  710. /// uniqueName
  711. /// or
  712. /// fileExtension
  713. /// </exception>
  714. public string GetCachePath(string path, string uniqueName, string fileExtension)
  715. {
  716. if (string.IsNullOrEmpty(path))
  717. {
  718. throw new ArgumentNullException("path");
  719. }
  720. if (string.IsNullOrEmpty(uniqueName))
  721. {
  722. throw new ArgumentNullException("uniqueName");
  723. }
  724. if (string.IsNullOrEmpty(fileExtension))
  725. {
  726. throw new ArgumentNullException("fileExtension");
  727. }
  728. var filename = uniqueName.GetMD5() + fileExtension;
  729. return GetCachePath(path, filename);
  730. }
  731. /// <summary>
  732. /// Gets the cache path.
  733. /// </summary>
  734. /// <param name="path">The path.</param>
  735. /// <param name="filename">The filename.</param>
  736. /// <returns>System.String.</returns>
  737. /// <exception cref="System.ArgumentNullException">
  738. /// path
  739. /// or
  740. /// filename
  741. /// </exception>
  742. public string GetCachePath(string path, string filename)
  743. {
  744. if (string.IsNullOrEmpty(path))
  745. {
  746. throw new ArgumentNullException("path");
  747. }
  748. if (string.IsNullOrEmpty(filename))
  749. {
  750. throw new ArgumentNullException("filename");
  751. }
  752. var prefix = filename.Substring(0, 1);
  753. path = Path.Combine(path, prefix);
  754. return Path.Combine(path, filename);
  755. }
  756. public async Task CreateImageCollage(ImageCollageOptions options)
  757. {
  758. _logger.Info("Creating image collage and saving to {0}", options.OutputPath);
  759. _imageEncoder.CreateImageCollage(options);
  760. _logger.Info("Completed creation of image collage and saved to {0}", options.OutputPath);
  761. }
  762. public IEnumerable<IImageEnhancer> GetSupportedEnhancers(IHasImages item, ImageType imageType)
  763. {
  764. return ImageEnhancers.Where(i =>
  765. {
  766. try
  767. {
  768. return i.Supports(item, imageType);
  769. }
  770. catch (Exception ex)
  771. {
  772. _logger.ErrorException("Error in image enhancer: {0}", ex, i.GetType().Name);
  773. return false;
  774. }
  775. });
  776. }
  777. private bool _disposed;
  778. public void Dispose()
  779. {
  780. _disposed = true;
  781. _imageEncoder.Dispose();
  782. _saveImageSizeTimer.Dispose();
  783. }
  784. private void CheckDisposed()
  785. {
  786. if (_disposed)
  787. {
  788. throw new ObjectDisposedException(GetType().Name);
  789. }
  790. }
  791. }
  792. }