ImageProcessor.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  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 (item == null && string.Equals(options.ItemType, typeof(Photo).Name, StringComparison.OrdinalIgnoreCase))
  258. {
  259. item = _libraryManager().GetItemById(options.ItemId);
  260. }
  261. if (options.CropWhiteSpace && !SupportsTransparency(originalImagePath))
  262. {
  263. options.CropWhiteSpace = false;
  264. }
  265. var resultPath = _imageEncoder.EncodeImage(originalImagePath, dateModified, tmpPath, autoOrient, orientation, quality, options, outputFormat);
  266. if (string.Equals(resultPath, originalImagePath, StringComparison.OrdinalIgnoreCase))
  267. {
  268. return new Tuple<string, string, DateTime>(originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified);
  269. }
  270. _fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(cacheFilePath));
  271. CopyFile(tmpPath, cacheFilePath);
  272. return new Tuple<string, string, DateTime>(tmpPath, GetMimeType(outputFormat, cacheFilePath), _fileSystem.GetLastWriteTimeUtc(tmpPath));
  273. }
  274. return new Tuple<string, string, DateTime>(cacheFilePath, GetMimeType(outputFormat, cacheFilePath), _fileSystem.GetLastWriteTimeUtc(cacheFilePath));
  275. }
  276. catch (Exception ex)
  277. {
  278. // If it fails for whatever reason, return the original image
  279. _logger.ErrorException("Error encoding image", ex);
  280. // Just spit out the original file if all the options are default
  281. return new Tuple<string, string, DateTime>(originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified);
  282. }
  283. }
  284. private void CopyFile(string src, string destination)
  285. {
  286. try
  287. {
  288. _fileSystem.CopyFile(src, destination, true);
  289. }
  290. catch
  291. {
  292. }
  293. }
  294. //private static int[][] OPERATIONS = new int[][] {
  295. // TopLeft
  296. //new int[] { 0, NONE},
  297. // TopRight
  298. //new int[] { 0, HORIZONTAL},
  299. //new int[] {180, NONE},
  300. // LeftTop
  301. //new int[] { 0, VERTICAL},
  302. //new int[] { 90, HORIZONTAL},
  303. // RightTop
  304. //new int[] { 90, NONE},
  305. //new int[] {-90, HORIZONTAL},
  306. //new int[] {-90, NONE},
  307. //};
  308. private string GetMimeType(ImageFormat format, string path)
  309. {
  310. if (format == ImageFormat.Bmp)
  311. {
  312. return MimeTypes.GetMimeType("i.bmp");
  313. }
  314. if (format == ImageFormat.Gif)
  315. {
  316. return MimeTypes.GetMimeType("i.gif");
  317. }
  318. if (format == ImageFormat.Jpg)
  319. {
  320. return MimeTypes.GetMimeType("i.jpg");
  321. }
  322. if (format == ImageFormat.Png)
  323. {
  324. return MimeTypes.GetMimeType("i.png");
  325. }
  326. if (format == ImageFormat.Webp)
  327. {
  328. return MimeTypes.GetMimeType("i.webp");
  329. }
  330. return MimeTypes.GetMimeType(path);
  331. }
  332. private ImageFormat GetOutputFormat(ImageFormat requestedFormat)
  333. {
  334. if (requestedFormat == ImageFormat.Webp && !_imageEncoder.SupportedOutputFormats.Contains(ImageFormat.Webp))
  335. {
  336. return ImageFormat.Png;
  337. }
  338. return requestedFormat;
  339. }
  340. private Tuple<string, DateTime> GetResult(string path)
  341. {
  342. return new Tuple<string, DateTime>(path, _fileSystem.GetLastWriteTimeUtc(path));
  343. }
  344. /// <summary>
  345. /// Increment this when there's a change requiring caches to be invalidated
  346. /// </summary>
  347. private const string Version = "3";
  348. /// <summary>
  349. /// Gets the cache file path based on a set of parameters
  350. /// </summary>
  351. 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)
  352. {
  353. var filename = originalPath;
  354. filename += "width=" + outputSize.Width;
  355. filename += "height=" + outputSize.Height;
  356. filename += "quality=" + quality;
  357. filename += "datemodified=" + dateModified.Ticks;
  358. filename += "f=" + format;
  359. if (addPlayedIndicator)
  360. {
  361. filename += "pl=true";
  362. }
  363. if (percentPlayed > 0)
  364. {
  365. filename += "p=" + percentPlayed;
  366. }
  367. if (unwatchedCount.HasValue)
  368. {
  369. filename += "p=" + unwatchedCount.Value;
  370. }
  371. if (blur.HasValue)
  372. {
  373. filename += "blur=" + blur.Value;
  374. }
  375. if (!string.IsNullOrEmpty(backgroundColor))
  376. {
  377. filename += "b=" + backgroundColor;
  378. }
  379. if (!string.IsNullOrEmpty(foregroundLayer))
  380. {
  381. filename += "fl=" + foregroundLayer;
  382. }
  383. filename += "v=" + Version;
  384. return GetCachePath(ResizedImageCachePath, filename, "." + format.ToString().ToLower());
  385. }
  386. public ImageSize GetImageSize(ItemImageInfo info, bool allowSlowMethods)
  387. {
  388. return GetImageSize(info.Path, info.DateModified, allowSlowMethods);
  389. }
  390. public ImageSize GetImageSize(ItemImageInfo info)
  391. {
  392. return GetImageSize(info.Path, info.DateModified, false);
  393. }
  394. public ImageSize GetImageSize(string path)
  395. {
  396. return GetImageSize(path, _fileSystem.GetLastWriteTimeUtc(path), false);
  397. }
  398. /// <summary>
  399. /// Gets the size of the image.
  400. /// </summary>
  401. /// <param name="path">The path.</param>
  402. /// <param name="imageDateModified">The image date modified.</param>
  403. /// <param name="allowSlowMethod">if set to <c>true</c> [allow slow method].</param>
  404. /// <returns>ImageSize.</returns>
  405. /// <exception cref="System.ArgumentNullException">path</exception>
  406. private ImageSize GetImageSize(string path, DateTime imageDateModified, bool allowSlowMethod)
  407. {
  408. if (string.IsNullOrEmpty(path))
  409. {
  410. throw new ArgumentNullException("path");
  411. }
  412. ImageSize size;
  413. var cacheHash = GetImageSizeKey(path, imageDateModified);
  414. if (!_cachedImagedSizes.TryGetValue(cacheHash, out size))
  415. {
  416. size = GetImageSizeInternal(path, allowSlowMethod);
  417. SaveImageSize(size, cacheHash, false);
  418. }
  419. return size;
  420. }
  421. public void SaveImageSize(string path, DateTime imageDateModified, ImageSize size)
  422. {
  423. var cacheHash = GetImageSizeKey(path, imageDateModified);
  424. SaveImageSize(size, cacheHash, true);
  425. }
  426. private void SaveImageSize(ImageSize size, Guid cacheHash, bool checkExists)
  427. {
  428. if (size.Width <= 0 || size.Height <= 0)
  429. {
  430. return;
  431. }
  432. if (checkExists && _cachedImagedSizes.ContainsKey(cacheHash))
  433. {
  434. return;
  435. }
  436. if (checkExists)
  437. {
  438. if (_cachedImagedSizes.TryAdd(cacheHash, size))
  439. {
  440. StartSaveImageSizeTimer();
  441. }
  442. }
  443. else
  444. {
  445. StartSaveImageSizeTimer();
  446. _cachedImagedSizes.AddOrUpdate(cacheHash, size, (keyName, oldValue) => size);
  447. }
  448. }
  449. private Guid GetImageSizeKey(string path, DateTime imageDateModified)
  450. {
  451. var name = path + "datemodified=" + imageDateModified.Ticks;
  452. return name.GetMD5();
  453. }
  454. public ImageSize? GetSavedImageSize(string path, DateTime imageDateModified)
  455. {
  456. ImageSize size;
  457. var cacheHash = GetImageSizeKey(path, imageDateModified);
  458. if (_cachedImagedSizes.TryGetValue(cacheHash, out size))
  459. {
  460. return size;
  461. }
  462. return null;
  463. }
  464. /// <summary>
  465. /// Gets the image size internal.
  466. /// </summary>
  467. /// <param name="path">The path.</param>
  468. /// <param name="allowSlowMethod">if set to <c>true</c> [allow slow method].</param>
  469. /// <returns>ImageSize.</returns>
  470. private ImageSize GetImageSizeInternal(string path, bool allowSlowMethod)
  471. {
  472. //try
  473. //{
  474. // using (var fileStream = _fileSystem.OpenRead(path))
  475. // {
  476. // using (var file = TagLib.File.Create(new StreamFileAbstraction(Path.GetFileName(path), fileStream, null)))
  477. // {
  478. // var image = file as TagLib.Image.File;
  479. // if (image != null)
  480. // {
  481. // var properties = image.Properties;
  482. // return new ImageSize
  483. // {
  484. // Height = properties.PhotoHeight,
  485. // Width = properties.PhotoWidth
  486. // };
  487. // }
  488. // }
  489. // }
  490. //}
  491. //catch
  492. //{
  493. //}
  494. try
  495. {
  496. return ImageHeader.GetDimensions(path, _logger, _fileSystem);
  497. }
  498. catch
  499. {
  500. if (allowSlowMethod)
  501. {
  502. return _imageEncoder.GetImageSize(path);
  503. }
  504. throw;
  505. }
  506. }
  507. private readonly ITimer _saveImageSizeTimer;
  508. private const int SaveImageSizeTimeout = 5000;
  509. private readonly object _saveImageSizeLock = new object();
  510. private void StartSaveImageSizeTimer()
  511. {
  512. _saveImageSizeTimer.Change(SaveImageSizeTimeout, Timeout.Infinite);
  513. }
  514. private void SaveImageSizeCallback(object state)
  515. {
  516. lock (_saveImageSizeLock)
  517. {
  518. try
  519. {
  520. var path = ImageSizeFile;
  521. _fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(path));
  522. _jsonSerializer.SerializeToFile(_cachedImagedSizes, path);
  523. }
  524. catch (Exception ex)
  525. {
  526. _logger.ErrorException("Error saving image size file", ex);
  527. }
  528. }
  529. }
  530. private string ImageSizeFile
  531. {
  532. get
  533. {
  534. return Path.Combine(_appPaths.DataPath, "imagesizes.json");
  535. }
  536. }
  537. /// <summary>
  538. /// Gets the image cache tag.
  539. /// </summary>
  540. /// <param name="item">The item.</param>
  541. /// <param name="image">The image.</param>
  542. /// <returns>Guid.</returns>
  543. /// <exception cref="System.ArgumentNullException">item</exception>
  544. public string GetImageCacheTag(IHasMetadata item, ItemImageInfo image)
  545. {
  546. if (item == null)
  547. {
  548. throw new ArgumentNullException("item");
  549. }
  550. if (image == null)
  551. {
  552. throw new ArgumentNullException("image");
  553. }
  554. var supportedEnhancers = GetSupportedEnhancers(item, image.Type);
  555. return GetImageCacheTag(item, image, supportedEnhancers);
  556. }
  557. /// <summary>
  558. /// Gets the image cache tag.
  559. /// </summary>
  560. /// <param name="item">The item.</param>
  561. /// <param name="image">The image.</param>
  562. /// <param name="imageEnhancers">The image enhancers.</param>
  563. /// <returns>Guid.</returns>
  564. /// <exception cref="System.ArgumentNullException">item</exception>
  565. public string GetImageCacheTag(IHasMetadata item, ItemImageInfo image, List<IImageEnhancer> imageEnhancers)
  566. {
  567. if (item == null)
  568. {
  569. throw new ArgumentNullException("item");
  570. }
  571. if (imageEnhancers == null)
  572. {
  573. throw new ArgumentNullException("imageEnhancers");
  574. }
  575. if (image == null)
  576. {
  577. throw new ArgumentNullException("image");
  578. }
  579. var originalImagePath = image.Path;
  580. var dateModified = image.DateModified;
  581. var imageType = image.Type;
  582. // Optimization
  583. if (imageEnhancers.Count == 0)
  584. {
  585. return (originalImagePath + dateModified.Ticks).GetMD5().ToString("N");
  586. }
  587. // Cache name is created with supported enhancers combined with the last config change so we pick up new config changes
  588. var cacheKeys = imageEnhancers.Select(i => i.GetConfigurationCacheKey(item, imageType)).ToList();
  589. cacheKeys.Add(originalImagePath + dateModified.Ticks);
  590. return string.Join("|", cacheKeys.ToArray(cacheKeys.Count)).GetMD5().ToString("N");
  591. }
  592. private async Task<Tuple<string, DateTime>> GetSupportedImage(string originalImagePath, DateTime dateModified)
  593. {
  594. var inputFormat = (Path.GetExtension(originalImagePath) ?? string.Empty)
  595. .TrimStart('.')
  596. .Replace("jpeg", "jpg", StringComparison.OrdinalIgnoreCase);
  597. if (!_imageEncoder.SupportedInputFormats.Contains(inputFormat, StringComparer.OrdinalIgnoreCase))
  598. {
  599. try
  600. {
  601. var filename = (originalImagePath + dateModified.Ticks.ToString(UsCulture)).GetMD5().ToString("N");
  602. var outputPath = Path.Combine(_appPaths.ImageCachePath, "converted-images", filename + ".webp");
  603. var file = _fileSystem.GetFileInfo(outputPath);
  604. if (!file.Exists)
  605. {
  606. await _mediaEncoder().ConvertImage(originalImagePath, outputPath).ConfigureAwait(false);
  607. dateModified = _fileSystem.GetLastWriteTimeUtc(outputPath);
  608. }
  609. else
  610. {
  611. dateModified = file.LastWriteTimeUtc;
  612. }
  613. originalImagePath = outputPath;
  614. }
  615. catch (Exception ex)
  616. {
  617. _logger.ErrorException("Image conversion failed for {0}", ex, originalImagePath);
  618. }
  619. }
  620. return new Tuple<string, DateTime>(originalImagePath, dateModified);
  621. }
  622. /// <summary>
  623. /// Gets the enhanced image.
  624. /// </summary>
  625. /// <param name="item">The item.</param>
  626. /// <param name="imageType">Type of the image.</param>
  627. /// <param name="imageIndex">Index of the image.</param>
  628. /// <returns>Task{System.String}.</returns>
  629. public async Task<string> GetEnhancedImage(IHasMetadata item, ImageType imageType, int imageIndex)
  630. {
  631. var enhancers = GetSupportedEnhancers(item, imageType);
  632. var imageInfo = item.GetImageInfo(imageType, imageIndex);
  633. var result = await GetEnhancedImage(imageInfo, item, imageIndex, enhancers);
  634. return result.Item1;
  635. }
  636. private async Task<Tuple<string, DateTime>> GetEnhancedImage(ItemImageInfo image,
  637. IHasMetadata item,
  638. int imageIndex,
  639. List<IImageEnhancer> enhancers)
  640. {
  641. var originalImagePath = image.Path;
  642. var dateModified = image.DateModified;
  643. var imageType = image.Type;
  644. try
  645. {
  646. var cacheGuid = GetImageCacheTag(item, image, enhancers);
  647. // Enhance if we have enhancers
  648. var ehnancedImagePath = await GetEnhancedImageInternal(originalImagePath, item, imageType, imageIndex, enhancers, cacheGuid).ConfigureAwait(false);
  649. // If the path changed update dateModified
  650. if (!string.Equals(ehnancedImagePath, originalImagePath, StringComparison.OrdinalIgnoreCase))
  651. {
  652. return GetResult(ehnancedImagePath);
  653. }
  654. }
  655. catch (Exception ex)
  656. {
  657. _logger.Error("Error enhancing image", ex);
  658. }
  659. return new Tuple<string, DateTime>(originalImagePath, dateModified);
  660. }
  661. /// <summary>
  662. /// Gets the enhanced image internal.
  663. /// </summary>
  664. /// <param name="originalImagePath">The original image path.</param>
  665. /// <param name="item">The item.</param>
  666. /// <param name="imageType">Type of the image.</param>
  667. /// <param name="imageIndex">Index of the image.</param>
  668. /// <param name="supportedEnhancers">The supported enhancers.</param>
  669. /// <param name="cacheGuid">The cache unique identifier.</param>
  670. /// <returns>Task&lt;System.String&gt;.</returns>
  671. /// <exception cref="ArgumentNullException">
  672. /// originalImagePath
  673. /// or
  674. /// item
  675. /// </exception>
  676. private async Task<string> GetEnhancedImageInternal(string originalImagePath,
  677. IHasMetadata item,
  678. ImageType imageType,
  679. int imageIndex,
  680. IEnumerable<IImageEnhancer> supportedEnhancers,
  681. string cacheGuid)
  682. {
  683. if (string.IsNullOrEmpty(originalImagePath))
  684. {
  685. throw new ArgumentNullException("originalImagePath");
  686. }
  687. if (item == null)
  688. {
  689. throw new ArgumentNullException("item");
  690. }
  691. // All enhanced images are saved as png to allow transparency
  692. var enhancedImagePath = GetCachePath(EnhancedImageCachePath, cacheGuid + ".png");
  693. // Check again in case of contention
  694. if (_fileSystem.FileExists(enhancedImagePath))
  695. {
  696. return enhancedImagePath;
  697. }
  698. _fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(enhancedImagePath));
  699. var tmpPath = Path.Combine(_appPaths.TempDirectory, Path.ChangeExtension(Guid.NewGuid().ToString(), Path.GetExtension(enhancedImagePath)));
  700. _fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(tmpPath));
  701. await ExecuteImageEnhancers(supportedEnhancers, originalImagePath, tmpPath, item, imageType, imageIndex).ConfigureAwait(false);
  702. try
  703. {
  704. _fileSystem.CopyFile(tmpPath, enhancedImagePath, true);
  705. }
  706. catch
  707. {
  708. }
  709. return tmpPath;
  710. }
  711. /// <summary>
  712. /// Executes the image enhancers.
  713. /// </summary>
  714. /// <param name="imageEnhancers">The image enhancers.</param>
  715. /// <param name="inputPath">The input path.</param>
  716. /// <param name="outputPath">The output path.</param>
  717. /// <param name="item">The item.</param>
  718. /// <param name="imageType">Type of the image.</param>
  719. /// <param name="imageIndex">Index of the image.</param>
  720. /// <returns>Task{EnhancedImage}.</returns>
  721. private async Task ExecuteImageEnhancers(IEnumerable<IImageEnhancer> imageEnhancers, string inputPath, string outputPath, IHasMetadata item, ImageType imageType, int imageIndex)
  722. {
  723. // Run the enhancers sequentially in order of priority
  724. foreach (var enhancer in imageEnhancers)
  725. {
  726. await enhancer.EnhanceImageAsync(item, inputPath, outputPath, imageType, imageIndex).ConfigureAwait(false);
  727. // Feed the output into the next enhancer as input
  728. inputPath = outputPath;
  729. }
  730. }
  731. /// <summary>
  732. /// Gets the cache path.
  733. /// </summary>
  734. /// <param name="path">The path.</param>
  735. /// <param name="uniqueName">Name of the unique.</param>
  736. /// <param name="fileExtension">The file extension.</param>
  737. /// <returns>System.String.</returns>
  738. /// <exception cref="System.ArgumentNullException">
  739. /// path
  740. /// or
  741. /// uniqueName
  742. /// or
  743. /// fileExtension
  744. /// </exception>
  745. public string GetCachePath(string path, string uniqueName, string fileExtension)
  746. {
  747. if (string.IsNullOrEmpty(path))
  748. {
  749. throw new ArgumentNullException("path");
  750. }
  751. if (string.IsNullOrEmpty(uniqueName))
  752. {
  753. throw new ArgumentNullException("uniqueName");
  754. }
  755. if (string.IsNullOrEmpty(fileExtension))
  756. {
  757. throw new ArgumentNullException("fileExtension");
  758. }
  759. var filename = uniqueName.GetMD5() + fileExtension;
  760. return GetCachePath(path, filename);
  761. }
  762. /// <summary>
  763. /// Gets the cache path.
  764. /// </summary>
  765. /// <param name="path">The path.</param>
  766. /// <param name="filename">The filename.</param>
  767. /// <returns>System.String.</returns>
  768. /// <exception cref="System.ArgumentNullException">
  769. /// path
  770. /// or
  771. /// filename
  772. /// </exception>
  773. public string GetCachePath(string path, string filename)
  774. {
  775. if (string.IsNullOrEmpty(path))
  776. {
  777. throw new ArgumentNullException("path");
  778. }
  779. if (string.IsNullOrEmpty(filename))
  780. {
  781. throw new ArgumentNullException("filename");
  782. }
  783. var prefix = filename.Substring(0, 1);
  784. path = Path.Combine(path, prefix);
  785. return Path.Combine(path, filename);
  786. }
  787. public void CreateImageCollage(ImageCollageOptions options)
  788. {
  789. _logger.Info("Creating image collage and saving to {0}", options.OutputPath);
  790. _imageEncoder.CreateImageCollage(options);
  791. _logger.Info("Completed creation of image collage and saved to {0}", options.OutputPath);
  792. }
  793. public List<IImageEnhancer> GetSupportedEnhancers(IHasMetadata item, ImageType imageType)
  794. {
  795. var list = new List<IImageEnhancer>();
  796. foreach (var i in ImageEnhancers)
  797. {
  798. try
  799. {
  800. if (i.Supports(item, imageType))
  801. {
  802. list.Add(i);
  803. }
  804. }
  805. catch (Exception ex)
  806. {
  807. _logger.ErrorException("Error in image enhancer: {0}", ex, i.GetType().Name);
  808. }
  809. }
  810. return list;
  811. }
  812. private bool _disposed;
  813. public void Dispose()
  814. {
  815. _disposed = true;
  816. _imageEncoder.Dispose();
  817. _saveImageSizeTimer.Dispose();
  818. }
  819. private void CheckDisposed()
  820. {
  821. if (_disposed)
  822. {
  823. throw new ObjectDisposedException(GetType().Name);
  824. }
  825. }
  826. }
  827. }