HttpPostedFile.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Net;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using MediaBrowser.Model.Services;
  9. public sealed class HttpPostedFile : IDisposable
  10. {
  11. private string _name;
  12. private string _contentType;
  13. private Stream _stream;
  14. private bool _disposed = false;
  15. internal HttpPostedFile(string name, string content_type, Stream base_stream, long offset, long length)
  16. {
  17. _name = name;
  18. _contentType = content_type;
  19. _stream = new ReadSubStream(base_stream, offset, length);
  20. }
  21. public string ContentType => _contentType;
  22. public int ContentLength => (int)_stream.Length;
  23. public string FileName => _name;
  24. public Stream InputStream => _stream;
  25. /// <summary>
  26. /// Releases the unmanaged resources and disposes of the managed resources used.
  27. /// </summary>
  28. public void Dispose()
  29. {
  30. if (_disposed)
  31. {
  32. return;
  33. }
  34. _stream.Dispose();
  35. _stream = null;
  36. _name = null;
  37. _contentType = null;
  38. _disposed = true;
  39. }
  40. private class ReadSubStream : Stream
  41. {
  42. private Stream _stream;
  43. private long _offset;
  44. private long _end;
  45. private long _position;
  46. public ReadSubStream(Stream s, long offset, long length)
  47. {
  48. _stream = s;
  49. _offset = offset;
  50. _end = offset + length;
  51. _position = offset;
  52. }
  53. public override void Flush()
  54. {
  55. }
  56. public override int Read(byte[] buffer, int dest_offset, int count)
  57. {
  58. if (buffer == null)
  59. {
  60. throw new ArgumentNullException(nameof(buffer));
  61. }
  62. if (dest_offset < 0)
  63. {
  64. throw new ArgumentOutOfRangeException(nameof(dest_offset), "< 0");
  65. }
  66. if (count < 0)
  67. {
  68. throw new ArgumentOutOfRangeException(nameof(count), "< 0");
  69. }
  70. int len = buffer.Length;
  71. if (dest_offset > len)
  72. {
  73. throw new ArgumentException("destination offset is beyond array size", nameof(dest_offset));
  74. }
  75. // reordered to avoid possible integer overflow
  76. if (dest_offset > len - count)
  77. {
  78. throw new ArgumentException("Reading would overrun buffer", nameof(count));
  79. }
  80. if (count > _end - _position)
  81. {
  82. count = (int)(_end - _position);
  83. }
  84. if (count <= 0)
  85. {
  86. return 0;
  87. }
  88. _stream.Position = _position;
  89. int result = _stream.Read(buffer, dest_offset, count);
  90. if (result > 0)
  91. {
  92. _position += result;
  93. }
  94. else
  95. {
  96. _position = _end;
  97. }
  98. return result;
  99. }
  100. public override int ReadByte()
  101. {
  102. if (_position >= _end)
  103. {
  104. return -1;
  105. }
  106. _stream.Position = _position;
  107. int result = _stream.ReadByte();
  108. if (result < 0)
  109. {
  110. _position = _end;
  111. }
  112. else
  113. {
  114. _position++;
  115. }
  116. return result;
  117. }
  118. public override long Seek(long d, SeekOrigin origin)
  119. {
  120. long real;
  121. switch (origin)
  122. {
  123. case SeekOrigin.Begin:
  124. real = _offset + d;
  125. break;
  126. case SeekOrigin.End:
  127. real = _end + d;
  128. break;
  129. case SeekOrigin.Current:
  130. real = _position + d;
  131. break;
  132. default:
  133. throw new ArgumentException("Unknown SeekOrigin value", nameof(origin));
  134. }
  135. long virt = real - _offset;
  136. if (virt < 0 || virt > Length)
  137. {
  138. throw new ArgumentException("Invalid position", nameof(d));
  139. }
  140. _position = _stream.Seek(real, SeekOrigin.Begin);
  141. return _position;
  142. }
  143. public override void SetLength(long value)
  144. {
  145. throw new NotSupportedException();
  146. }
  147. public override void Write(byte[] buffer, int offset, int count)
  148. {
  149. throw new NotSupportedException();
  150. }
  151. public override bool CanRead => true;
  152. public override bool CanSeek => true;
  153. public override bool CanWrite => false;
  154. public override long Length => _end - _offset;
  155. public override long Position
  156. {
  157. get => _position - _offset;
  158. set
  159. {
  160. if (value > Length)
  161. {
  162. throw new ArgumentOutOfRangeException(nameof(value));
  163. }
  164. _position = Seek(value, SeekOrigin.Begin);
  165. }
  166. }
  167. }
  168. }