2
0

ImageProcessor.cs 32 KB

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