ImageWriter.cs 2.4 KB

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