ImageProcessor.cs 34 KB

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