ImageService.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Controller.Drawing;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Controller.Net;
  7. using MediaBrowser.Controller.Persistence;
  8. using MediaBrowser.Controller.Providers;
  9. using MediaBrowser.Model.Drawing;
  10. using MediaBrowser.Model.Dto;
  11. using MediaBrowser.Model.Entities;
  12. using ServiceStack;
  13. using ServiceStack.Text.Controller;
  14. using ServiceStack.Web;
  15. using System;
  16. using System.Collections.Generic;
  17. using System.IO;
  18. using System.Linq;
  19. using System.Threading;
  20. using System.Threading.Tasks;
  21. namespace MediaBrowser.Api.Images
  22. {
  23. /// <summary>
  24. /// Class GetItemImage
  25. /// </summary>
  26. [Route("/Items/{Id}/Images", "GET")]
  27. [Api(Description = "Gets information about an item's images")]
  28. [Authenticated]
  29. public class GetItemImageInfos : IReturn<List<ImageInfo>>
  30. {
  31. /// <summary>
  32. /// Gets or sets the id.
  33. /// </summary>
  34. /// <value>The id.</value>
  35. [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  36. public string Id { get; set; }
  37. }
  38. [Route("/Items/{Id}/Images/{Type}", "GET")]
  39. [Route("/Items/{Id}/Images/{Type}/{Index}", "GET")]
  40. [Route("/Items/{Id}/Images/{Type}", "HEAD")]
  41. [Route("/Items/{Id}/Images/{Type}/{Index}", "HEAD")]
  42. [Route("/Items/{Id}/Images/{Type}/{Index}/{Tag}/{Format}/{MaxWidth}/{MaxHeight}/{PercentPlayed}", "GET")]
  43. [Route("/Items/{Id}/Images/{Type}/{Index}/{Tag}/{Format}/{MaxWidth}/{MaxHeight}/{PercentPlayed}", "HEAD")]
  44. [Api(Description = "Gets an item image")]
  45. public class GetItemImage : ImageRequest
  46. {
  47. /// <summary>
  48. /// Gets or sets the id.
  49. /// </summary>
  50. /// <value>The id.</value>
  51. [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  52. public string Id { get; set; }
  53. }
  54. /// <summary>
  55. /// Class UpdateItemImageIndex
  56. /// </summary>
  57. [Route("/Items/{Id}/Images/{Type}/{Index}/Index", "POST")]
  58. [Api(Description = "Updates the index for an item image")]
  59. [Authenticated]
  60. public class UpdateItemImageIndex : IReturnVoid
  61. {
  62. /// <summary>
  63. /// Gets or sets the id.
  64. /// </summary>
  65. /// <value>The id.</value>
  66. [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  67. public Guid Id { get; set; }
  68. /// <summary>
  69. /// Gets or sets the type of the image.
  70. /// </summary>
  71. /// <value>The type of the image.</value>
  72. [ApiMember(Name = "Type", Description = "Image Type", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  73. public ImageType Type { get; set; }
  74. /// <summary>
  75. /// Gets or sets the index.
  76. /// </summary>
  77. /// <value>The index.</value>
  78. [ApiMember(Name = "Index", Description = "Image Index", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "POST")]
  79. public int Index { get; set; }
  80. /// <summary>
  81. /// Gets or sets the new index.
  82. /// </summary>
  83. /// <value>The new index.</value>
  84. [ApiMember(Name = "NewIndex", Description = "The new image index", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
  85. public int NewIndex { get; set; }
  86. }
  87. /// <summary>
  88. /// Class GetPersonImage
  89. /// </summary>
  90. [Route("/Artists/{Name}/Images/{Type}", "GET")]
  91. [Route("/Artists/{Name}/Images/{Type}/{Index}", "GET")]
  92. [Route("/Genres/{Name}/Images/{Type}", "GET")]
  93. [Route("/Genres/{Name}/Images/{Type}/{Index}", "GET")]
  94. [Route("/GameGenres/{Name}/Images/{Type}", "GET")]
  95. [Route("/GameGenres/{Name}/Images/{Type}/{Index}", "GET")]
  96. [Route("/MusicGenres/{Name}/Images/{Type}", "GET")]
  97. [Route("/MusicGenres/{Name}/Images/{Type}/{Index}", "GET")]
  98. [Route("/Persons/{Name}/Images/{Type}", "GET")]
  99. [Route("/Persons/{Name}/Images/{Type}/{Index}", "GET")]
  100. [Route("/Studios/{Name}/Images/{Type}", "GET")]
  101. [Route("/Studios/{Name}/Images/{Type}/{Index}", "GET")]
  102. [Route("/Years/{Year}/Images/{Type}", "GET")]
  103. [Route("/Years/{Year}/Images/{Type}/{Index}", "GET")]
  104. [Route("/Artists/{Name}/Images/{Type}", "HEAD")]
  105. [Route("/Artists/{Name}/Images/{Type}/{Index}", "HEAD")]
  106. [Route("/Genres/{Name}/Images/{Type}", "HEAD")]
  107. [Route("/Genres/{Name}/Images/{Type}/{Index}", "HEAD")]
  108. [Route("/GameGenres/{Name}/Images/{Type}", "HEAD")]
  109. [Route("/GameGenres/{Name}/Images/{Type}/{Index}", "HEAD")]
  110. [Route("/MusicGenres/{Name}/Images/{Type}", "HEAD")]
  111. [Route("/MusicGenres/{Name}/Images/{Type}/{Index}", "HEAD")]
  112. [Route("/Persons/{Name}/Images/{Type}", "HEAD")]
  113. [Route("/Persons/{Name}/Images/{Type}/{Index}", "HEAD")]
  114. [Route("/Studios/{Name}/Images/{Type}", "HEAD")]
  115. [Route("/Studios/{Name}/Images/{Type}/{Index}", "HEAD")]
  116. [Route("/Years/{Year}/Images/{Type}", "HEAD")]
  117. [Route("/Years/{Year}/Images/{Type}/{Index}", "HEAD")]
  118. [Api(Description = "Gets an item by name image")]
  119. public class GetItemByNameImage : ImageRequest
  120. {
  121. /// <summary>
  122. /// Gets or sets the name.
  123. /// </summary>
  124. /// <value>The name.</value>
  125. [ApiMember(Name = "Name", Description = "Item name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  126. public string Name { get; set; }
  127. }
  128. /// <summary>
  129. /// Class GetUserImage
  130. /// </summary>
  131. [Route("/Users/{Id}/Images/{Type}", "GET")]
  132. [Route("/Users/{Id}/Images/{Type}/{Index}", "GET")]
  133. [Route("/Users/{Id}/Images/{Type}", "HEAD")]
  134. [Route("/Users/{Id}/Images/{Type}/{Index}", "HEAD")]
  135. [Api(Description = "Gets a user image")]
  136. public class GetUserImage : ImageRequest
  137. {
  138. /// <summary>
  139. /// Gets or sets the id.
  140. /// </summary>
  141. /// <value>The id.</value>
  142. [ApiMember(Name = "Id", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  143. public Guid Id { get; set; }
  144. }
  145. /// <summary>
  146. /// Class DeleteItemImage
  147. /// </summary>
  148. [Route("/Items/{Id}/Images/{Type}", "DELETE")]
  149. [Route("/Items/{Id}/Images/{Type}/{Index}", "DELETE")]
  150. [Api(Description = "Deletes an item image")]
  151. [Authenticated]
  152. public class DeleteItemImage : DeleteImageRequest, IReturnVoid
  153. {
  154. /// <summary>
  155. /// Gets or sets the id.
  156. /// </summary>
  157. /// <value>The id.</value>
  158. [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
  159. public Guid Id { get; set; }
  160. }
  161. /// <summary>
  162. /// Class DeleteUserImage
  163. /// </summary>
  164. [Route("/Users/{Id}/Images/{Type}", "DELETE")]
  165. [Route("/Users/{Id}/Images/{Type}/{Index}", "DELETE")]
  166. [Api(Description = "Deletes a user image")]
  167. [Authenticated]
  168. public class DeleteUserImage : DeleteImageRequest, IReturnVoid
  169. {
  170. /// <summary>
  171. /// Gets or sets the id.
  172. /// </summary>
  173. /// <value>The id.</value>
  174. [ApiMember(Name = "Id", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
  175. public Guid Id { get; set; }
  176. }
  177. /// <summary>
  178. /// Class PostUserImage
  179. /// </summary>
  180. [Route("/Users/{Id}/Images/{Type}", "POST")]
  181. [Route("/Users/{Id}/Images/{Type}/{Index}", "POST")]
  182. [Api(Description = "Posts a user image")]
  183. [Authenticated]
  184. public class PostUserImage : DeleteImageRequest, IRequiresRequestStream, IReturnVoid
  185. {
  186. /// <summary>
  187. /// Gets or sets the id.
  188. /// </summary>
  189. /// <value>The id.</value>
  190. [ApiMember(Name = "Id", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  191. public Guid Id { get; set; }
  192. /// <summary>
  193. /// The raw Http Request Input Stream
  194. /// </summary>
  195. /// <value>The request stream.</value>
  196. public Stream RequestStream { get; set; }
  197. }
  198. /// <summary>
  199. /// Class PostItemImage
  200. /// </summary>
  201. [Route("/Items/{Id}/Images/{Type}", "POST")]
  202. [Route("/Items/{Id}/Images/{Type}/{Index}", "POST")]
  203. [Api(Description = "Posts an item image")]
  204. [Authenticated]
  205. public class PostItemImage : DeleteImageRequest, IRequiresRequestStream, IReturnVoid
  206. {
  207. /// <summary>
  208. /// Gets or sets the id.
  209. /// </summary>
  210. /// <value>The id.</value>
  211. [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  212. public string Id { get; set; }
  213. /// <summary>
  214. /// The raw Http Request Input Stream
  215. /// </summary>
  216. /// <value>The request stream.</value>
  217. public Stream RequestStream { get; set; }
  218. }
  219. /// <summary>
  220. /// Class ImageService
  221. /// </summary>
  222. public class ImageService : BaseApiService
  223. {
  224. private readonly IUserManager _userManager;
  225. private readonly ILibraryManager _libraryManager;
  226. private readonly IProviderManager _providerManager;
  227. private readonly IItemRepository _itemRepo;
  228. private readonly IImageProcessor _imageProcessor;
  229. private readonly IFileSystem _fileSystem;
  230. /// <summary>
  231. /// Initializes a new instance of the <see cref="ImageService" /> class.
  232. /// </summary>
  233. public ImageService(IUserManager userManager, ILibraryManager libraryManager, IProviderManager providerManager, IItemRepository itemRepo, IImageProcessor imageProcessor, IFileSystem fileSystem)
  234. {
  235. _userManager = userManager;
  236. _libraryManager = libraryManager;
  237. _providerManager = providerManager;
  238. _itemRepo = itemRepo;
  239. _imageProcessor = imageProcessor;
  240. _fileSystem = fileSystem;
  241. }
  242. /// <summary>
  243. /// Gets the specified request.
  244. /// </summary>
  245. /// <param name="request">The request.</param>
  246. /// <returns>System.Object.</returns>
  247. public object Get(GetItemImageInfos request)
  248. {
  249. var item = _libraryManager.GetItemById(request.Id);
  250. var result = GetItemImageInfos(item);
  251. return ToOptimizedSerializedResultUsingCache(result);
  252. }
  253. /// <summary>
  254. /// Gets the item image infos.
  255. /// </summary>
  256. /// <param name="item">The item.</param>
  257. /// <returns>Task{List{ImageInfo}}.</returns>
  258. public List<ImageInfo> GetItemImageInfos(BaseItem item)
  259. {
  260. var list = new List<ImageInfo>();
  261. foreach (var image in item.ImageInfos.Where(i => !item.AllowsMultipleImages(i.Type)))
  262. {
  263. var info = GetImageInfo(item, image, null);
  264. if (info != null)
  265. {
  266. list.Add(info);
  267. }
  268. }
  269. foreach (var imageType in item.ImageInfos.Select(i => i.Type).Distinct().Where(item.AllowsMultipleImages))
  270. {
  271. var index = 0;
  272. // Prevent implicitly captured closure
  273. var currentImageType = imageType;
  274. foreach (var image in item.ImageInfos.Where(i => i.Type == currentImageType))
  275. {
  276. var info = GetImageInfo(item, image, index);
  277. if (info != null)
  278. {
  279. list.Add(info);
  280. }
  281. index++;
  282. }
  283. }
  284. return list;
  285. }
  286. private ImageInfo GetImageInfo(IHasImages item, ItemImageInfo info, int? imageIndex)
  287. {
  288. try
  289. {
  290. var fileInfo = new FileInfo(info.Path);
  291. return new ImageInfo
  292. {
  293. Path = info.Path,
  294. ImageIndex = imageIndex,
  295. ImageType = info.Type,
  296. ImageTag = _imageProcessor.GetImageCacheTag(item, info),
  297. Size = fileInfo.Length,
  298. Width = info.Width ?? 0,
  299. Height = info.Height ?? 0
  300. };
  301. }
  302. catch (Exception ex)
  303. {
  304. Logger.ErrorException("Error getting image information for {0}", ex, info.Path);
  305. return null;
  306. }
  307. }
  308. /// <summary>
  309. /// Gets the specified request.
  310. /// </summary>
  311. /// <param name="request">The request.</param>
  312. /// <returns>System.Object.</returns>
  313. public object Get(GetItemImage request)
  314. {
  315. var item = string.IsNullOrEmpty(request.Id) ?
  316. _libraryManager.RootFolder :
  317. _libraryManager.GetItemById(request.Id);
  318. return GetImage(request, item, false);
  319. }
  320. /// <summary>
  321. /// Gets the specified request.
  322. /// </summary>
  323. /// <param name="request">The request.</param>
  324. /// <returns>System.Object.</returns>
  325. public object Head(GetItemImage request)
  326. {
  327. var item = string.IsNullOrEmpty(request.Id) ?
  328. _libraryManager.RootFolder :
  329. _libraryManager.GetItemById(request.Id);
  330. return GetImage(request, item, true);
  331. }
  332. /// <summary>
  333. /// Gets the specified request.
  334. /// </summary>
  335. /// <param name="request">The request.</param>
  336. /// <returns>System.Object.</returns>
  337. public object Get(GetUserImage request)
  338. {
  339. var item = _userManager.GetUserById(request.Id);
  340. return GetImage(request, item, false);
  341. }
  342. public object Head(GetUserImage request)
  343. {
  344. var item = _userManager.GetUserById(request.Id);
  345. return GetImage(request, item, true);
  346. }
  347. public object Get(GetItemByNameImage request)
  348. {
  349. var pathInfo = PathInfo.Parse(Request.PathInfo);
  350. var type = pathInfo.GetArgumentValue<string>(0);
  351. var item = GetItemByName(request.Name, type, _libraryManager);
  352. return GetImage(request, item, false);
  353. }
  354. public object Head(GetItemByNameImage request)
  355. {
  356. var pathInfo = PathInfo.Parse(Request.PathInfo);
  357. var type = pathInfo.GetArgumentValue<string>(0);
  358. var item = GetItemByName(request.Name, type, _libraryManager);
  359. return GetImage(request, item, true);
  360. }
  361. /// <summary>
  362. /// Posts the specified request.
  363. /// </summary>
  364. /// <param name="request">The request.</param>
  365. public void Post(PostUserImage request)
  366. {
  367. var pathInfo = PathInfo.Parse(Request.PathInfo);
  368. var id = new Guid(pathInfo.GetArgumentValue<string>(1));
  369. request.Type = (ImageType)Enum.Parse(typeof(ImageType), pathInfo.GetArgumentValue<string>(3), true);
  370. var item = _userManager.GetUserById(id);
  371. var task = PostImage(item, request.RequestStream, request.Type, Request.ContentType);
  372. Task.WaitAll(task);
  373. }
  374. /// <summary>
  375. /// Posts the specified request.
  376. /// </summary>
  377. /// <param name="request">The request.</param>
  378. public void Post(PostItemImage request)
  379. {
  380. var pathInfo = PathInfo.Parse(Request.PathInfo);
  381. var id = new Guid(pathInfo.GetArgumentValue<string>(1));
  382. request.Type = (ImageType)Enum.Parse(typeof(ImageType), pathInfo.GetArgumentValue<string>(3), true);
  383. var item = _libraryManager.GetItemById(id);
  384. var task = PostImage(item, request.RequestStream, request.Type, Request.ContentType);
  385. Task.WaitAll(task);
  386. }
  387. /// <summary>
  388. /// Deletes the specified request.
  389. /// </summary>
  390. /// <param name="request">The request.</param>
  391. public void Delete(DeleteUserImage request)
  392. {
  393. var item = _userManager.GetUserById(request.Id);
  394. var task = item.DeleteImage(request.Type, request.Index ?? 0);
  395. Task.WaitAll(task);
  396. }
  397. /// <summary>
  398. /// Deletes the specified request.
  399. /// </summary>
  400. /// <param name="request">The request.</param>
  401. public void Delete(DeleteItemImage request)
  402. {
  403. var item = _libraryManager.GetItemById(request.Id);
  404. var task = item.DeleteImage(request.Type, request.Index ?? 0);
  405. Task.WaitAll(task);
  406. }
  407. /// <summary>
  408. /// Posts the specified request.
  409. /// </summary>
  410. /// <param name="request">The request.</param>
  411. public void Post(UpdateItemImageIndex request)
  412. {
  413. var item = _libraryManager.GetItemById(request.Id);
  414. var task = UpdateItemIndex(item, request.Type, request.Index, request.NewIndex);
  415. Task.WaitAll(task);
  416. }
  417. /// <summary>
  418. /// Updates the index of the item.
  419. /// </summary>
  420. /// <param name="item">The item.</param>
  421. /// <param name="type">The type.</param>
  422. /// <param name="currentIndex">Index of the current.</param>
  423. /// <param name="newIndex">The new index.</param>
  424. /// <returns>Task.</returns>
  425. /// <exception cref="System.ArgumentException">The change index operation is only applicable to backdrops and screenshots</exception>
  426. private Task UpdateItemIndex(IHasImages item, ImageType type, int currentIndex, int newIndex)
  427. {
  428. return item.SwapImages(type, currentIndex, newIndex);
  429. }
  430. /// <summary>
  431. /// Gets the image.
  432. /// </summary>
  433. /// <param name="request">The request.</param>
  434. /// <param name="item">The item.</param>
  435. /// <param name="isHeadRequest">if set to <c>true</c> [is head request].</param>
  436. /// <returns>System.Object.</returns>
  437. /// <exception cref="ResourceNotFoundException"></exception>
  438. public object GetImage(ImageRequest request, IHasImages item, bool isHeadRequest)
  439. {
  440. var imageInfo = GetImageInfo(request, item);
  441. if (imageInfo == null)
  442. {
  443. throw new ResourceNotFoundException(string.Format("{0} does not have an image of type {1}", item.Name, request.Type));
  444. }
  445. var supportedImageEnhancers = request.EnableImageEnhancers ? _imageProcessor.ImageEnhancers.Where(i =>
  446. {
  447. try
  448. {
  449. return i.Supports(item, request.Type);
  450. }
  451. catch (Exception ex)
  452. {
  453. Logger.ErrorException("Error in image enhancer: {0}", ex, i.GetType().Name);
  454. return false;
  455. }
  456. }).ToList() : new List<IImageEnhancer>();
  457. var contentType = GetMimeType(request.Format, imageInfo.Path);
  458. var cacheGuid = new Guid(_imageProcessor.GetImageCacheTag(item, imageInfo, supportedImageEnhancers));
  459. TimeSpan? cacheDuration = null;
  460. if (!string.IsNullOrEmpty(request.Tag) && cacheGuid == new Guid(request.Tag))
  461. {
  462. cacheDuration = TimeSpan.FromDays(365);
  463. }
  464. var responseHeaders = new Dictionary<string, string>
  465. {
  466. {"transferMode.dlna.org", "Interactive"},
  467. {"realTimeInfo.dlna.org", "DLNA.ORG_TLAG=*"}
  468. };
  469. return GetImageResult(item,
  470. request,
  471. imageInfo,
  472. supportedImageEnhancers,
  473. contentType,
  474. cacheDuration,
  475. responseHeaders,
  476. isHeadRequest)
  477. .Result;
  478. }
  479. private async Task<object> GetImageResult(IHasImages item,
  480. ImageRequest request,
  481. ItemImageInfo image,
  482. List<IImageEnhancer> enhancers,
  483. string contentType,
  484. TimeSpan? cacheDuration,
  485. IDictionary<string, string> headers,
  486. bool isHeadRequest)
  487. {
  488. var cropwhitespace = request.Type == ImageType.Logo || request.Type == ImageType.Art;
  489. if (request.CropWhitespace.HasValue)
  490. {
  491. cropwhitespace = request.CropWhitespace.Value;
  492. }
  493. var options = new ImageProcessingOptions
  494. {
  495. CropWhiteSpace = cropwhitespace,
  496. Enhancers = enhancers,
  497. Height = request.Height,
  498. ImageIndex = request.Index ?? 0,
  499. Image = image,
  500. Item = item,
  501. MaxHeight = request.MaxHeight,
  502. MaxWidth = request.MaxWidth,
  503. Quality = request.Quality,
  504. Width = request.Width,
  505. OutputFormat = request.Format,
  506. AddPlayedIndicator = request.AddPlayedIndicator,
  507. PercentPlayed = request.PercentPlayed ?? 0,
  508. UnplayedCount = request.UnplayedCount,
  509. BackgroundColor = request.BackgroundColor
  510. };
  511. var file = await _imageProcessor.ProcessImage(options).ConfigureAwait(false);
  512. return ResultFactory.GetStaticFileResult(Request, new StaticFileResultOptions
  513. {
  514. CacheDuration = cacheDuration,
  515. ResponseHeaders = headers,
  516. ContentType = contentType,
  517. IsHeadRequest = isHeadRequest,
  518. Path = file
  519. });
  520. }
  521. private string GetMimeType(ImageOutputFormat format, string path)
  522. {
  523. if (format == ImageOutputFormat.Bmp)
  524. {
  525. return Common.Net.MimeTypes.GetMimeType("i.bmp");
  526. }
  527. if (format == ImageOutputFormat.Gif)
  528. {
  529. return Common.Net.MimeTypes.GetMimeType("i.gif");
  530. }
  531. if (format == ImageOutputFormat.Jpg)
  532. {
  533. return Common.Net.MimeTypes.GetMimeType("i.jpg");
  534. }
  535. if (format == ImageOutputFormat.Png)
  536. {
  537. return Common.Net.MimeTypes.GetMimeType("i.png");
  538. }
  539. if (format == ImageOutputFormat.Webp)
  540. {
  541. return Common.Net.MimeTypes.GetMimeType("i.webp");
  542. }
  543. return Common.Net.MimeTypes.GetMimeType(path);
  544. }
  545. /// <summary>
  546. /// Gets the image path.
  547. /// </summary>
  548. /// <param name="request">The request.</param>
  549. /// <param name="item">The item.</param>
  550. /// <returns>System.String.</returns>
  551. private ItemImageInfo GetImageInfo(ImageRequest request, IHasImages item)
  552. {
  553. var index = request.Index ?? 0;
  554. return item.GetImageInfo(request.Type, index);
  555. }
  556. /// <summary>
  557. /// Posts the image.
  558. /// </summary>
  559. /// <param name="entity">The entity.</param>
  560. /// <param name="inputStream">The input stream.</param>
  561. /// <param name="imageType">Type of the image.</param>
  562. /// <param name="mimeType">Type of the MIME.</param>
  563. /// <returns>Task.</returns>
  564. public async Task PostImage(BaseItem entity, Stream inputStream, ImageType imageType, string mimeType)
  565. {
  566. using (var reader = new StreamReader(inputStream))
  567. {
  568. var text = await reader.ReadToEndAsync().ConfigureAwait(false);
  569. var bytes = Convert.FromBase64String(text);
  570. var memoryStream = new MemoryStream(bytes)
  571. {
  572. Position = 0
  573. };
  574. // Handle image/png; charset=utf-8
  575. mimeType = mimeType.Split(';').FirstOrDefault();
  576. await _providerManager.SaveImage(entity, memoryStream, mimeType, imageType, null, CancellationToken.None).ConfigureAwait(false);
  577. await entity.UpdateToRepository(ItemUpdateType.ImageUpdate, CancellationToken.None).ConfigureAwait(false);
  578. }
  579. }
  580. }
  581. }