2
0

ImageWriter.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using MediaBrowser.Controller;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Providers;
  4. using MediaBrowser.Model.Entities;
  5. using ServiceStack.Service;
  6. using ServiceStack.ServiceHost;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Threading.Tasks;
  11. namespace MediaBrowser.Api.Images
  12. {
  13. /// <summary>
  14. /// Class ImageWriter
  15. /// </summary>
  16. public class ImageWriter : IStreamWriter, IHasOptions
  17. {
  18. public List<IImageEnhancer> Enhancers;
  19. /// <summary>
  20. /// Gets or sets the request.
  21. /// </summary>
  22. /// <value>The request.</value>
  23. public ImageRequest Request { get; set; }
  24. /// <summary>
  25. /// Gets or sets the item.
  26. /// </summary>
  27. /// <value>The item.</value>
  28. public BaseItem Item { get; set; }
  29. /// <summary>
  30. /// The original image date modified
  31. /// </summary>
  32. public DateTime OriginalImageDateModified;
  33. public string OriginalImagePath;
  34. /// <summary>
  35. /// The _options
  36. /// </summary>
  37. private readonly IDictionary<string, string> _options = new Dictionary<string, string>();
  38. /// <summary>
  39. /// Gets the options.
  40. /// </summary>
  41. /// <value>The options.</value>
  42. public IDictionary<string, string> Options
  43. {
  44. get { return _options; }
  45. }
  46. /// <summary>
  47. /// Writes to.
  48. /// </summary>
  49. /// <param name="responseStream">The response stream.</param>
  50. public void WriteTo(Stream responseStream)
  51. {
  52. var task = WriteToAsync(responseStream);
  53. Task.WaitAll(task);
  54. }
  55. /// <summary>
  56. /// Writes to async.
  57. /// </summary>
  58. /// <param name="responseStream">The response stream.</param>
  59. /// <returns>Task.</returns>
  60. private Task WriteToAsync(Stream responseStream)
  61. {
  62. var cropwhitespace = Request.Type == ImageType.Logo || Request.Type == ImageType.Art;
  63. if (Request.CropWhitespace.HasValue)
  64. {
  65. cropwhitespace = Request.CropWhitespace.Value;
  66. }
  67. return Kernel.Instance.ImageManager.ProcessImage(Item, Request.Type, Request.Index ?? 0, OriginalImagePath, cropwhitespace,
  68. OriginalImageDateModified, responseStream, Request.Width, Request.Height, Request.MaxWidth,
  69. Request.MaxHeight, Request.Quality, Enhancers);
  70. }
  71. }
  72. }