ImageWriter.cs 2.5 KB

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