2
0

ImageWriter.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. /// Gets or sets the type of the content.
  37. /// </summary>
  38. /// <value>The type of the content.</value>
  39. public string ContentType { get; set; }
  40. /// <summary>
  41. /// Writes to.
  42. /// </summary>
  43. /// <param name="responseStream">The response stream.</param>
  44. public void WriteTo(Stream responseStream)
  45. {
  46. Options["Content-Type"] = ContentType;
  47. var task = WriteToAsync(responseStream);
  48. Task.WaitAll(task);
  49. }
  50. /// <summary>
  51. /// Writes to async.
  52. /// </summary>
  53. /// <param name="responseStream">The response stream.</param>
  54. /// <returns>Task.</returns>
  55. private Task WriteToAsync(Stream responseStream)
  56. {
  57. return Kernel.Instance.ImageManager.ProcessImage(Item, Request.Type, Request.Index ?? 0, CropWhiteSpace,
  58. OriginalImageDateModified, responseStream, Request.Width, Request.Height, Request.MaxWidth,
  59. Request.MaxHeight, Request.Quality);
  60. }
  61. /// <summary>
  62. /// The _options
  63. /// </summary>
  64. private readonly IDictionary<string, string> _options = new Dictionary<string, string> { };
  65. /// <summary>
  66. /// Gets the options.
  67. /// </summary>
  68. /// <value>The options.</value>
  69. public IDictionary<string, string> Options
  70. {
  71. get { return _options; }
  72. }
  73. }
  74. }