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 bool CanRead => true;
  54. public override bool CanSeek => true;
  55. public override bool CanWrite => false;
  56. public override long Length => _end - _offset;
  57. public override long Position
  58. {
  59. get => _position - _offset;
  60. set
  61. {
  62. if (value > Length)
  63. {
  64. throw new ArgumentOutOfRangeException(nameof(value));
  65. }
  66. _position = Seek(value, SeekOrigin.Begin);
  67. }
  68. }
  69. public override void Flush()
  70. {
  71. }
  72. public override int Read(byte[] buffer, int dest_offset, int count)
  73. {
  74. if (buffer == null)
  75. {
  76. throw new ArgumentNullException(nameof(buffer));
  77. }
  78. if (dest_offset < 0)
  79. {
  80. throw new ArgumentOutOfRangeException(nameof(dest_offset), "< 0");
  81. }
  82. if (count < 0)
  83. {
  84. throw new ArgumentOutOfRangeException(nameof(count), "< 0");
  85. }
  86. int len = buffer.Length;
  87. if (dest_offset > len)
  88. {
  89. throw new ArgumentException("destination offset is beyond array size", nameof(dest_offset));
  90. }
  91. // reordered to avoid possible integer overflow
  92. if (dest_offset > len - count)
  93. {
  94. throw new ArgumentException("Reading would overrun buffer", nameof(count));
  95. }
  96. if (count > _end - _position)
  97. {
  98. count = (int)(_end - _position);
  99. }
  100. if (count <= 0)
  101. {
  102. return 0;
  103. }
  104. _stream.Position = _position;
  105. int result = _stream.Read(buffer, dest_offset, count);
  106. if (result > 0)
  107. {
  108. _position += result;
  109. }
  110. else
  111. {
  112. _position = _end;
  113. }
  114. return result;
  115. }
  116. public override int ReadByte()
  117. {
  118. if (_position >= _end)
  119. {
  120. return -1;
  121. }
  122. _stream.Position = _position;
  123. int result = _stream.ReadByte();
  124. if (result < 0)
  125. {
  126. _position = _end;
  127. }
  128. else
  129. {
  130. _position++;
  131. }
  132. return result;
  133. }
  134. public override long Seek(long d, SeekOrigin origin)
  135. {
  136. long real;
  137. switch (origin)
  138. {
  139. case SeekOrigin.Begin:
  140. real = _offset + d;
  141. break;
  142. case SeekOrigin.End:
  143. real = _end + d;
  144. break;
  145. case SeekOrigin.Current:
  146. real = _position + d;
  147. break;
  148. default:
  149. throw new ArgumentException("Unknown SeekOrigin value", nameof(origin));
  150. }
  151. long virt = real - _offset;
  152. if (virt < 0 || virt > Length)
  153. {
  154. throw new ArgumentException("Invalid position", nameof(d));
  155. }
  156. _position = _stream.Seek(real, SeekOrigin.Begin);
  157. return _position;
  158. }
  159. public override void SetLength(long value)
  160. {
  161. throw new NotSupportedException();
  162. }
  163. public override void Write(byte[] buffer, int offset, int count)
  164. {
  165. throw new NotSupportedException();
  166. }
  167. }
  168. }