ImageWriter.cs 3.0 KB

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