ImageProcessor.cs 32 KB

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