ImageProcessor.cs 32 KB

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