ImageProcessor.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. using Imazen.WebP;
  2. using MediaBrowser.Common.Extensions;
  3. using MediaBrowser.Common.IO;
  4. using MediaBrowser.Controller;
  5. using MediaBrowser.Controller.Drawing;
  6. using MediaBrowser.Controller.Entities;
  7. using MediaBrowser.Controller.MediaEncoding;
  8. using MediaBrowser.Controller.Providers;
  9. using MediaBrowser.Model.Drawing;
  10. using MediaBrowser.Model.Entities;
  11. using MediaBrowser.Model.Logging;
  12. using MediaBrowser.Model.Serialization;
  13. using System;
  14. using System.Collections.Concurrent;
  15. using System.Collections.Generic;
  16. using System.Drawing;
  17. using System.Drawing.Drawing2D;
  18. using System.Drawing.Imaging;
  19. using System.Globalization;
  20. using System.IO;
  21. using System.Linq;
  22. using System.Threading;
  23. using System.Threading.Tasks;
  24. namespace MediaBrowser.Server.Implementations.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 IMediaEncoder _mediaEncoder;
  53. public ImageProcessor(ILogger logger, IServerApplicationPaths appPaths, IFileSystem fileSystem, IJsonSerializer jsonSerializer, IMediaEncoder mediaEncoder)
  54. {
  55. _logger = logger;
  56. _fileSystem = fileSystem;
  57. _jsonSerializer = jsonSerializer;
  58. _mediaEncoder = mediaEncoder;
  59. _appPaths = appPaths;
  60. _saveImageSizeTimer = new Timer(SaveImageSizeCallback, null, Timeout.Infinite, Timeout.Infinite);
  61. Dictionary<Guid, ImageSize> sizeDictionary;
  62. try
  63. {
  64. sizeDictionary = jsonSerializer.DeserializeFromFile<Dictionary<Guid, ImageSize>>(ImageSizeFile) ??
  65. new Dictionary<Guid, ImageSize>();
  66. }
  67. catch (FileNotFoundException)
  68. {
  69. // No biggie
  70. sizeDictionary = new Dictionary<Guid, ImageSize>();
  71. }
  72. catch (Exception ex)
  73. {
  74. logger.ErrorException("Error parsing image size cache file", ex);
  75. sizeDictionary = new Dictionary<Guid, ImageSize>();
  76. }
  77. _cachedImagedSizes = new ConcurrentDictionary<Guid, ImageSize>(sizeDictionary);
  78. }
  79. private string ResizedImageCachePath
  80. {
  81. get
  82. {
  83. return Path.Combine(_appPaths.ImageCachePath, "resized-images");
  84. }
  85. }
  86. private string EnhancedImageCachePath
  87. {
  88. get
  89. {
  90. return Path.Combine(_appPaths.ImageCachePath, "enhanced-images");
  91. }
  92. }
  93. private string CroppedWhitespaceImageCachePath
  94. {
  95. get
  96. {
  97. return Path.Combine(_appPaths.ImageCachePath, "cropped-images");
  98. }
  99. }
  100. public void AddParts(IEnumerable<IImageEnhancer> enhancers)
  101. {
  102. ImageEnhancers = enhancers.ToArray();
  103. }
  104. public async Task ProcessImage(ImageProcessingOptions options, Stream toStream)
  105. {
  106. var file = await ProcessImage(options).ConfigureAwait(false);
  107. using (var fileStream = _fileSystem.GetFileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read, true))
  108. {
  109. await fileStream.CopyToAsync(toStream).ConfigureAwait(false);
  110. }
  111. }
  112. public async Task<string> ProcessImage(ImageProcessingOptions options)
  113. {
  114. if (options == null)
  115. {
  116. throw new ArgumentNullException("options");
  117. }
  118. var originalImagePath = options.Image.Path;
  119. if (options.HasDefaultOptions(originalImagePath) && options.Enhancers.Count == 0 && !options.CropWhiteSpace)
  120. {
  121. // Just spit out the original file if all the options are default
  122. return originalImagePath;
  123. }
  124. var dateModified = options.Image.DateModified;
  125. if (options.CropWhiteSpace)
  126. {
  127. var tuple = await GetWhitespaceCroppedImage(originalImagePath, dateModified).ConfigureAwait(false);
  128. originalImagePath = tuple.Item1;
  129. dateModified = tuple.Item2;
  130. }
  131. if (options.Enhancers.Count > 0)
  132. {
  133. var tuple = await GetEnhancedImage(options.Image, options.Item, options.ImageIndex, options.Enhancers).ConfigureAwait(false);
  134. originalImagePath = tuple.Item1;
  135. dateModified = tuple.Item2;
  136. }
  137. var originalImageSize = GetImageSize(originalImagePath, dateModified);
  138. // Determine the output size based on incoming parameters
  139. var newSize = DrawingUtils.Resize(originalImageSize, options.Width, options.Height, options.MaxWidth, options.MaxHeight);
  140. if (options.HasDefaultOptionsWithoutSize(originalImagePath) && newSize.Equals(originalImageSize) && options.Enhancers.Count == 0)
  141. {
  142. // Just spit out the original file if the new size equals the old
  143. return originalImagePath;
  144. }
  145. var quality = options.Quality ?? 90;
  146. var cacheFilePath = GetCacheFilePath(originalImagePath, newSize, quality, dateModified, options.OutputFormat, options.AddPlayedIndicator, options.PercentPlayed, options.UnplayedCount, options.BackgroundColor);
  147. var semaphore = GetLock(cacheFilePath);
  148. await semaphore.WaitAsync().ConfigureAwait(false);
  149. // Check again in case of lock contention
  150. try
  151. {
  152. if (File.Exists(cacheFilePath))
  153. {
  154. semaphore.Release();
  155. return cacheFilePath;
  156. }
  157. }
  158. catch
  159. {
  160. semaphore.Release();
  161. throw;
  162. }
  163. try
  164. {
  165. var hasPostProcessing = !string.IsNullOrEmpty(options.BackgroundColor) || options.UnplayedCount.HasValue || options.AddPlayedIndicator || options.PercentPlayed > 0;
  166. using (var fileStream = _fileSystem.GetFileStream(originalImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, true))
  167. {
  168. // Copy to memory stream to avoid Image locking file
  169. using (var memoryStream = new MemoryStream())
  170. {
  171. await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false);
  172. using (var originalImage = Image.FromStream(memoryStream, true, false))
  173. {
  174. var newWidth = Convert.ToInt32(newSize.Width);
  175. var newHeight = Convert.ToInt32(newSize.Height);
  176. // Graphics.FromImage will throw an exception if the PixelFormat is Indexed, so we need to handle that here
  177. // Also, Webp only supports Format32bppArgb and Format32bppRgb
  178. var pixelFormat = options.OutputFormat == ImageOutputFormat.Webp
  179. ? PixelFormat.Format32bppArgb
  180. : PixelFormat.Format32bppPArgb;
  181. using (var thumbnail = new Bitmap(newWidth, newHeight, pixelFormat))
  182. {
  183. // Mono throw an exeception if assign 0 to SetResolution
  184. if (originalImage.HorizontalResolution > 0 && originalImage.VerticalResolution > 0)
  185. {
  186. // Preserve the original resolution
  187. thumbnail.SetResolution(originalImage.HorizontalResolution, originalImage.VerticalResolution);
  188. }
  189. using (var thumbnailGraph = Graphics.FromImage(thumbnail))
  190. {
  191. thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
  192. thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
  193. thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
  194. thumbnailGraph.PixelOffsetMode = PixelOffsetMode.HighQuality;
  195. thumbnailGraph.CompositingMode = !hasPostProcessing ?
  196. CompositingMode.SourceCopy :
  197. CompositingMode.SourceOver;
  198. SetBackgroundColor(thumbnailGraph, options);
  199. thumbnailGraph.DrawImage(originalImage, 0, 0, newWidth, newHeight);
  200. DrawIndicator(thumbnailGraph, newWidth, newHeight, options);
  201. var outputFormat = GetOutputFormat(originalImage, options.OutputFormat);
  202. Directory.CreateDirectory(Path.GetDirectoryName(cacheFilePath));
  203. // Save to the cache location
  204. using (var cacheFileStream = _fileSystem.GetFileStream(cacheFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, false))
  205. {
  206. if (options.OutputFormat == ImageOutputFormat.Webp)
  207. {
  208. new SimpleEncoder().Encode(thumbnail, cacheFileStream, quality, false);
  209. }
  210. else
  211. {
  212. // Save to the memory stream
  213. thumbnail.Save(outputFormat, cacheFileStream, quality);
  214. }
  215. }
  216. return cacheFilePath;
  217. }
  218. }
  219. }
  220. }
  221. }
  222. }
  223. finally
  224. {
  225. semaphore.Release();
  226. }
  227. }
  228. /// <summary>
  229. /// Sets the color of the background.
  230. /// </summary>
  231. /// <param name="graphics">The graphics.</param>
  232. /// <param name="options">The options.</param>
  233. private void SetBackgroundColor(Graphics graphics, ImageProcessingOptions options)
  234. {
  235. var color = options.BackgroundColor;
  236. if (!string.IsNullOrEmpty(color))
  237. {
  238. Color drawingColor;
  239. try
  240. {
  241. drawingColor = ColorTranslator.FromHtml(color);
  242. }
  243. catch
  244. {
  245. drawingColor = ColorTranslator.FromHtml("#" + color);
  246. }
  247. graphics.Clear(drawingColor);
  248. }
  249. }
  250. /// <summary>
  251. /// Draws the indicator.
  252. /// </summary>
  253. /// <param name="graphics">The graphics.</param>
  254. /// <param name="imageWidth">Width of the image.</param>
  255. /// <param name="imageHeight">Height of the image.</param>
  256. /// <param name="options">The options.</param>
  257. private void DrawIndicator(Graphics graphics, int imageWidth, int imageHeight, ImageProcessingOptions options)
  258. {
  259. if (!options.AddPlayedIndicator && !options.UnplayedCount.HasValue && options.PercentPlayed.Equals(0))
  260. {
  261. return;
  262. }
  263. try
  264. {
  265. if (options.AddPlayedIndicator)
  266. {
  267. var currentImageSize = new Size(imageWidth, imageHeight);
  268. new PlayedIndicatorDrawer().DrawPlayedIndicator(graphics, currentImageSize);
  269. }
  270. else if (options.UnplayedCount.HasValue)
  271. {
  272. var currentImageSize = new Size(imageWidth, imageHeight);
  273. new UnplayedCountIndicator().DrawUnplayedCountIndicator(graphics, currentImageSize, options.UnplayedCount.Value);
  274. }
  275. if (options.PercentPlayed >= 0)
  276. {
  277. var currentImageSize = new Size(imageWidth, imageHeight);
  278. new PercentPlayedDrawer().Process(graphics, currentImageSize, options.PercentPlayed);
  279. }
  280. }
  281. catch (Exception ex)
  282. {
  283. _logger.ErrorException("Error drawing indicator overlay", ex);
  284. }
  285. }
  286. /// <summary>
  287. /// Gets the output format.
  288. /// </summary>
  289. /// <param name="image">The image.</param>
  290. /// <param name="outputFormat">The output format.</param>
  291. /// <returns>ImageFormat.</returns>
  292. private System.Drawing.Imaging.ImageFormat GetOutputFormat(Image image, ImageOutputFormat outputFormat)
  293. {
  294. switch (outputFormat)
  295. {
  296. case ImageOutputFormat.Bmp:
  297. return System.Drawing.Imaging.ImageFormat.Bmp;
  298. case ImageOutputFormat.Gif:
  299. return System.Drawing.Imaging.ImageFormat.Gif;
  300. case ImageOutputFormat.Jpg:
  301. return System.Drawing.Imaging.ImageFormat.Jpeg;
  302. case ImageOutputFormat.Png:
  303. return System.Drawing.Imaging.ImageFormat.Png;
  304. default:
  305. return image.RawFormat;
  306. }
  307. }
  308. /// <summary>
  309. /// Crops whitespace from an image, caches the result, and returns the cached path
  310. /// </summary>
  311. /// <param name="originalImagePath">The original image path.</param>
  312. /// <param name="dateModified">The date modified.</param>
  313. /// <returns>System.String.</returns>
  314. private async Task<Tuple<string, DateTime>> GetWhitespaceCroppedImage(string originalImagePath, DateTime dateModified)
  315. {
  316. var name = originalImagePath;
  317. name += "datemodified=" + dateModified.Ticks;
  318. var croppedImagePath = GetCachePath(CroppedWhitespaceImageCachePath, name, Path.GetExtension(originalImagePath));
  319. var semaphore = GetLock(croppedImagePath);
  320. await semaphore.WaitAsync().ConfigureAwait(false);
  321. // Check again in case of contention
  322. if (File.Exists(croppedImagePath))
  323. {
  324. semaphore.Release();
  325. return new Tuple<string, DateTime>(croppedImagePath, _fileSystem.GetLastWriteTimeUtc(croppedImagePath));
  326. }
  327. try
  328. {
  329. using (var fileStream = _fileSystem.GetFileStream(originalImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, true))
  330. {
  331. // Copy to memory stream to avoid Image locking file
  332. using (var memoryStream = new MemoryStream())
  333. {
  334. await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false);
  335. using (var originalImage = (Bitmap)Image.FromStream(memoryStream, true, false))
  336. {
  337. var outputFormat = originalImage.RawFormat;
  338. using (var croppedImage = originalImage.CropWhitespace())
  339. {
  340. Directory.CreateDirectory(Path.GetDirectoryName(croppedImagePath));
  341. using (var outputStream = _fileSystem.GetFileStream(croppedImagePath, FileMode.Create, FileAccess.Write, FileShare.Read, false))
  342. {
  343. croppedImage.Save(outputFormat, outputStream, 100);
  344. }
  345. }
  346. }
  347. }
  348. }
  349. }
  350. catch (Exception ex)
  351. {
  352. // We have to have a catch-all here because some of the .net image methods throw a plain old Exception
  353. _logger.ErrorException("Error cropping image {0}", ex, originalImagePath);
  354. return new Tuple<string, DateTime>(originalImagePath, dateModified);
  355. }
  356. finally
  357. {
  358. semaphore.Release();
  359. }
  360. return new Tuple<string, DateTime>(croppedImagePath, _fileSystem.GetLastWriteTimeUtc(croppedImagePath));
  361. }
  362. /// <summary>
  363. /// Increment this when indicator drawings change
  364. /// </summary>
  365. private const string IndicatorVersion = "2";
  366. /// <summary>
  367. /// Gets the cache file path based on a set of parameters
  368. /// </summary>
  369. private string GetCacheFilePath(string originalPath, ImageSize outputSize, int quality, DateTime dateModified, ImageOutputFormat format, bool addPlayedIndicator, double percentPlayed, int? unwatchedCount, string backgroundColor)
  370. {
  371. var filename = originalPath;
  372. filename += "width=" + outputSize.Width;
  373. filename += "height=" + outputSize.Height;
  374. filename += "quality=" + quality;
  375. filename += "datemodified=" + dateModified.Ticks;
  376. if (format != ImageOutputFormat.Original)
  377. {
  378. filename += "f=" + format;
  379. }
  380. var hasIndicator = false;
  381. if (addPlayedIndicator)
  382. {
  383. filename += "pl=true";
  384. hasIndicator = true;
  385. }
  386. if (percentPlayed > 0)
  387. {
  388. filename += "p=" + percentPlayed;
  389. hasIndicator = true;
  390. }
  391. if (unwatchedCount.HasValue)
  392. {
  393. filename += "p=" + unwatchedCount.Value;
  394. hasIndicator = true;
  395. }
  396. if (hasIndicator)
  397. {
  398. filename += "iv=" + IndicatorVersion;
  399. }
  400. if (!string.IsNullOrEmpty(backgroundColor))
  401. {
  402. filename += "b=" + backgroundColor;
  403. }
  404. return GetCachePath(ResizedImageCachePath, filename, Path.GetExtension(originalPath));
  405. }
  406. /// <summary>
  407. /// Gets the size of the image.
  408. /// </summary>
  409. /// <param name="path">The path.</param>
  410. /// <returns>ImageSize.</returns>
  411. public ImageSize GetImageSize(string path)
  412. {
  413. return GetImageSize(path, File.GetLastWriteTimeUtc(path));
  414. }
  415. /// <summary>
  416. /// Gets the size of the image.
  417. /// </summary>
  418. /// <param name="path">The path.</param>
  419. /// <param name="imageDateModified">The image date modified.</param>
  420. /// <returns>ImageSize.</returns>
  421. /// <exception cref="System.ArgumentNullException">path</exception>
  422. public ImageSize GetImageSize(string path, DateTime imageDateModified)
  423. {
  424. if (string.IsNullOrEmpty(path))
  425. {
  426. throw new ArgumentNullException("path");
  427. }
  428. var name = path + "datemodified=" + imageDateModified.Ticks;
  429. ImageSize size;
  430. var cacheHash = name.GetMD5();
  431. if (!_cachedImagedSizes.TryGetValue(cacheHash, out size))
  432. {
  433. size = GetImageSizeInternal(path);
  434. _cachedImagedSizes.AddOrUpdate(cacheHash, size, (keyName, oldValue) => size);
  435. }
  436. return size;
  437. }
  438. /// <summary>
  439. /// Gets the image size internal.
  440. /// </summary>
  441. /// <param name="path">The path.</param>
  442. /// <returns>ImageSize.</returns>
  443. private ImageSize GetImageSizeInternal(string path)
  444. {
  445. var size = ImageHeader.GetDimensions(path, _logger, _fileSystem);
  446. StartSaveImageSizeTimer();
  447. return new ImageSize { Width = size.Width, Height = size.Height };
  448. }
  449. private readonly Timer _saveImageSizeTimer;
  450. private const int SaveImageSizeTimeout = 5000;
  451. private readonly object _saveImageSizeLock = new object();
  452. private void StartSaveImageSizeTimer()
  453. {
  454. _saveImageSizeTimer.Change(SaveImageSizeTimeout, Timeout.Infinite);
  455. }
  456. private void SaveImageSizeCallback(object state)
  457. {
  458. lock (_saveImageSizeLock)
  459. {
  460. try
  461. {
  462. var path = ImageSizeFile;
  463. Directory.CreateDirectory(Path.GetDirectoryName(path));
  464. _jsonSerializer.SerializeToFile(_cachedImagedSizes, path);
  465. }
  466. catch (Exception ex)
  467. {
  468. _logger.ErrorException("Error saving image size file", ex);
  469. }
  470. }
  471. }
  472. private string ImageSizeFile
  473. {
  474. get
  475. {
  476. return Path.Combine(_appPaths.DataPath, "imagesizes.json");
  477. }
  478. }
  479. /// <summary>
  480. /// Gets the image cache tag.
  481. /// </summary>
  482. /// <param name="item">The item.</param>
  483. /// <param name="image">The image.</param>
  484. /// <returns>Guid.</returns>
  485. /// <exception cref="System.ArgumentNullException">item</exception>
  486. public string GetImageCacheTag(IHasImages item, ItemImageInfo image)
  487. {
  488. if (item == null)
  489. {
  490. throw new ArgumentNullException("item");
  491. }
  492. if (image == null)
  493. {
  494. throw new ArgumentNullException("image");
  495. }
  496. var supportedEnhancers = GetSupportedEnhancers(item, image.Type);
  497. return GetImageCacheTag(item, image, supportedEnhancers.ToList());
  498. }
  499. /// <summary>
  500. /// Gets the image cache tag.
  501. /// </summary>
  502. /// <param name="item">The item.</param>
  503. /// <param name="image">The image.</param>
  504. /// <param name="imageEnhancers">The image enhancers.</param>
  505. /// <returns>Guid.</returns>
  506. /// <exception cref="System.ArgumentNullException">item</exception>
  507. public string GetImageCacheTag(IHasImages item, ItemImageInfo image, List<IImageEnhancer> imageEnhancers)
  508. {
  509. if (item == null)
  510. {
  511. throw new ArgumentNullException("item");
  512. }
  513. if (imageEnhancers == null)
  514. {
  515. throw new ArgumentNullException("imageEnhancers");
  516. }
  517. if (image == null)
  518. {
  519. throw new ArgumentNullException("image");
  520. }
  521. var originalImagePath = image.Path;
  522. var dateModified = image.DateModified;
  523. var imageType = image.Type;
  524. // Optimization
  525. if (imageEnhancers.Count == 0)
  526. {
  527. return (originalImagePath + dateModified.Ticks).GetMD5().ToString("N");
  528. }
  529. // Cache name is created with supported enhancers combined with the last config change so we pick up new config changes
  530. var cacheKeys = imageEnhancers.Select(i => i.GetConfigurationCacheKey(item, imageType)).ToList();
  531. cacheKeys.Add(originalImagePath + dateModified.Ticks);
  532. return string.Join("|", cacheKeys.ToArray()).GetMD5().ToString("N");
  533. }
  534. /// <summary>
  535. /// Gets the enhanced image.
  536. /// </summary>
  537. /// <param name="item">The item.</param>
  538. /// <param name="imageType">Type of the image.</param>
  539. /// <param name="imageIndex">Index of the image.</param>
  540. /// <returns>Task{System.String}.</returns>
  541. public async Task<string> GetEnhancedImage(IHasImages item, ImageType imageType, int imageIndex)
  542. {
  543. var enhancers = GetSupportedEnhancers(item, imageType).ToList();
  544. var imageInfo = item.GetImageInfo(imageType, imageIndex);
  545. var result = await GetEnhancedImage(imageInfo, item, imageIndex, enhancers);
  546. return result.Item1;
  547. }
  548. private async Task<Tuple<string, DateTime>> GetEnhancedImage(ItemImageInfo image,
  549. IHasImages item,
  550. int imageIndex,
  551. List<IImageEnhancer> enhancers)
  552. {
  553. var originalImagePath = image.Path;
  554. var dateModified = image.DateModified;
  555. var imageType = image.Type;
  556. try
  557. {
  558. var cacheGuid = GetImageCacheTag(item, image, enhancers);
  559. // Enhance if we have enhancers
  560. var ehnancedImagePath = await GetEnhancedImageInternal(originalImagePath, item, imageType, imageIndex, enhancers, cacheGuid).ConfigureAwait(false);
  561. // If the path changed update dateModified
  562. if (!ehnancedImagePath.Equals(originalImagePath, StringComparison.OrdinalIgnoreCase))
  563. {
  564. dateModified = _fileSystem.GetLastWriteTimeUtc(ehnancedImagePath);
  565. return new Tuple<string, DateTime>(ehnancedImagePath, dateModified);
  566. }
  567. }
  568. catch (Exception ex)
  569. {
  570. _logger.Error("Error enhancing image", ex);
  571. }
  572. return new Tuple<string, DateTime>(originalImagePath, dateModified);
  573. }
  574. /// <summary>
  575. /// Runs an image through the image enhancers, caches the result, and returns the cached path
  576. /// </summary>
  577. /// <param name="originalImagePath">The original image path.</param>
  578. /// <param name="item">The item.</param>
  579. /// <param name="imageType">Type of the image.</param>
  580. /// <param name="imageIndex">Index of the image.</param>
  581. /// <param name="supportedEnhancers">The supported enhancers.</param>
  582. /// <param name="cacheGuid">The cache unique identifier.</param>
  583. /// <returns>System.String.</returns>
  584. /// <exception cref="System.ArgumentNullException">originalImagePath</exception>
  585. private async Task<string> GetEnhancedImageInternal(string originalImagePath,
  586. IHasImages item,
  587. ImageType imageType,
  588. int imageIndex,
  589. IEnumerable<IImageEnhancer> supportedEnhancers,
  590. string cacheGuid)
  591. {
  592. if (string.IsNullOrEmpty(originalImagePath))
  593. {
  594. throw new ArgumentNullException("originalImagePath");
  595. }
  596. if (item == null)
  597. {
  598. throw new ArgumentNullException("item");
  599. }
  600. // All enhanced images are saved as png to allow transparency
  601. var enhancedImagePath = GetCachePath(EnhancedImageCachePath, cacheGuid + ".png");
  602. var semaphore = GetLock(enhancedImagePath);
  603. await semaphore.WaitAsync().ConfigureAwait(false);
  604. // Check again in case of contention
  605. if (File.Exists(enhancedImagePath))
  606. {
  607. semaphore.Release();
  608. return enhancedImagePath;
  609. }
  610. try
  611. {
  612. using (var fileStream = _fileSystem.GetFileStream(originalImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, true))
  613. {
  614. // Copy to memory stream to avoid Image locking file
  615. using (var memoryStream = new MemoryStream())
  616. {
  617. await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false);
  618. using (var originalImage = Image.FromStream(memoryStream, true, false))
  619. {
  620. //Pass the image through registered enhancers
  621. using (var newImage = await ExecuteImageEnhancers(supportedEnhancers, originalImage, item, imageType, imageIndex).ConfigureAwait(false))
  622. {
  623. var parentDirectory = Path.GetDirectoryName(enhancedImagePath);
  624. Directory.CreateDirectory(parentDirectory);
  625. //And then save it in the cache
  626. using (var outputStream = _fileSystem.GetFileStream(enhancedImagePath, FileMode.Create, FileAccess.Write, FileShare.Read, false))
  627. {
  628. newImage.Save(System.Drawing.Imaging.ImageFormat.Png, outputStream, 100);
  629. }
  630. }
  631. }
  632. }
  633. }
  634. }
  635. finally
  636. {
  637. semaphore.Release();
  638. }
  639. return enhancedImagePath;
  640. }
  641. /// <summary>
  642. /// Executes the image enhancers.
  643. /// </summary>
  644. /// <param name="imageEnhancers">The image enhancers.</param>
  645. /// <param name="originalImage">The original image.</param>
  646. /// <param name="item">The item.</param>
  647. /// <param name="imageType">Type of the image.</param>
  648. /// <param name="imageIndex">Index of the image.</param>
  649. /// <returns>Task{EnhancedImage}.</returns>
  650. private async Task<Image> ExecuteImageEnhancers(IEnumerable<IImageEnhancer> imageEnhancers, Image originalImage, IHasImages item, ImageType imageType, int imageIndex)
  651. {
  652. var result = originalImage;
  653. // Run the enhancers sequentially in order of priority
  654. foreach (var enhancer in imageEnhancers)
  655. {
  656. var typeName = enhancer.GetType().Name;
  657. try
  658. {
  659. result = await enhancer.EnhanceImageAsync(item, result, imageType, imageIndex).ConfigureAwait(false);
  660. }
  661. catch (Exception ex)
  662. {
  663. _logger.ErrorException("{0} failed enhancing {1}", ex, typeName, item.Name);
  664. throw;
  665. }
  666. }
  667. return result;
  668. }
  669. /// <summary>
  670. /// The _semaphoreLocks
  671. /// </summary>
  672. private readonly ConcurrentDictionary<string, object> _locks = new ConcurrentDictionary<string, object>();
  673. /// <summary>
  674. /// Gets the lock.
  675. /// </summary>
  676. /// <param name="filename">The filename.</param>
  677. /// <returns>System.Object.</returns>
  678. private object GetObjectLock(string filename)
  679. {
  680. return _locks.GetOrAdd(filename, key => new object());
  681. }
  682. /// <summary>
  683. /// The _semaphoreLocks
  684. /// </summary>
  685. private readonly ConcurrentDictionary<string, SemaphoreSlim> _semaphoreLocks = new ConcurrentDictionary<string, SemaphoreSlim>();
  686. /// <summary>
  687. /// Gets the lock.
  688. /// </summary>
  689. /// <param name="filename">The filename.</param>
  690. /// <returns>System.Object.</returns>
  691. private SemaphoreSlim GetLock(string filename)
  692. {
  693. return _semaphoreLocks.GetOrAdd(filename, key => new SemaphoreSlim(1, 1));
  694. }
  695. /// <summary>
  696. /// Gets the cache path.
  697. /// </summary>
  698. /// <param name="path">The path.</param>
  699. /// <param name="uniqueName">Name of the unique.</param>
  700. /// <param name="fileExtension">The file extension.</param>
  701. /// <returns>System.String.</returns>
  702. /// <exception cref="System.ArgumentNullException">
  703. /// path
  704. /// or
  705. /// uniqueName
  706. /// or
  707. /// fileExtension
  708. /// </exception>
  709. public string GetCachePath(string path, string uniqueName, string fileExtension)
  710. {
  711. if (string.IsNullOrEmpty(path))
  712. {
  713. throw new ArgumentNullException("path");
  714. }
  715. if (string.IsNullOrEmpty(uniqueName))
  716. {
  717. throw new ArgumentNullException("uniqueName");
  718. }
  719. if (string.IsNullOrEmpty(fileExtension))
  720. {
  721. throw new ArgumentNullException("fileExtension");
  722. }
  723. var filename = uniqueName.GetMD5() + fileExtension;
  724. return GetCachePath(path, filename);
  725. }
  726. /// <summary>
  727. /// Gets the cache path.
  728. /// </summary>
  729. /// <param name="path">The path.</param>
  730. /// <param name="filename">The filename.</param>
  731. /// <returns>System.String.</returns>
  732. /// <exception cref="System.ArgumentNullException">
  733. /// path
  734. /// or
  735. /// filename
  736. /// </exception>
  737. public string GetCachePath(string path, string filename)
  738. {
  739. if (string.IsNullOrEmpty(path))
  740. {
  741. throw new ArgumentNullException("path");
  742. }
  743. if (string.IsNullOrEmpty(filename))
  744. {
  745. throw new ArgumentNullException("filename");
  746. }
  747. var prefix = filename.Substring(0, 1);
  748. path = Path.Combine(path, prefix);
  749. return Path.Combine(path, filename);
  750. }
  751. public IEnumerable<IImageEnhancer> GetSupportedEnhancers(IHasImages item, ImageType imageType)
  752. {
  753. return ImageEnhancers.Where(i =>
  754. {
  755. try
  756. {
  757. return i.Supports(item, imageType);
  758. }
  759. catch (Exception ex)
  760. {
  761. _logger.ErrorException("Error in image enhancer: {0}", ex, i.GetType().Name);
  762. return false;
  763. }
  764. });
  765. }
  766. public void Dispose()
  767. {
  768. _saveImageSizeTimer.Dispose();
  769. }
  770. }
  771. }