|
6 | 6 | using System.Threading;
|
7 | 7 | using System.Threading.Tasks;
|
8 | 8 | using Microsoft.AspNet.Http.Features;
|
| 9 | +using Microsoft.Extensions.FileProviders; |
9 | 10 |
|
10 | 11 | namespace Microsoft.AspNet.Http
|
11 | 12 | {
|
@@ -109,41 +110,50 @@ public abstract class HttpResponse
|
109 | 110 | /// <summary>
|
110 | 111 | /// Sends the given file using the SendFile extension.
|
111 | 112 | /// </summary>
|
112 |
| - /// <param name="fileName">The full path to the file.</param> |
| 113 | + /// <param name="file">The file to send.</param> |
113 | 114 | /// <returns></returns>
|
114 |
| - public virtual Task SendFileAsync(string fileName) |
| 115 | + public virtual Task SendFileAsync(IFileInfo file) |
115 | 116 | {
|
116 |
| - if (fileName == null) |
| 117 | + if (file == null) |
117 | 118 | {
|
118 |
| - throw new ArgumentNullException(nameof(fileName)); |
| 119 | + throw new ArgumentNullException(nameof(file)); |
119 | 120 | }
|
120 | 121 |
|
121 |
| - return SendFileAsync(fileName, 0, null, CancellationToken.None); |
| 122 | + return SendFileAsync(file, 0, null, CancellationToken.None); |
122 | 123 | }
|
123 | 124 |
|
124 | 125 | /// <summary>
|
125 | 126 | /// Sends the given file using the SendFile extension.
|
126 | 127 | /// </summary>
|
127 |
| - /// <param name="fileName">The full path to the file.</param> |
| 128 | + /// <param name="file">The file to send.</param> |
128 | 129 | /// <param name="offset">The offset in the file.</param>
|
129 | 130 | /// <param name="count">The number of types to send, or null to send the remainder of the file.</param>
|
130 | 131 | /// <param name="cancellationToken"></param>
|
131 | 132 | /// <returns></returns>
|
132 |
| - public virtual Task SendFileAsync(string fileName, long offset, long? count, CancellationToken cancellationToken = default(CancellationToken)) |
| 133 | + public virtual Task SendFileAsync(IFileInfo file, long offset, long? count, CancellationToken cancellationToken = default(CancellationToken)) |
133 | 134 | {
|
134 |
| - if (fileName == null) |
| 135 | + if (file == null) |
135 | 136 | {
|
136 |
| - throw new ArgumentNullException(nameof(fileName)); |
| 137 | + throw new ArgumentNullException(nameof(file)); |
| 138 | + } |
| 139 | + |
| 140 | + if (string.IsNullOrEmpty(file.PhysicalPath)) |
| 141 | + { |
| 142 | + var readStream = file.CreateReadStream(); |
| 143 | + |
| 144 | + readStream.Seek(offset, SeekOrigin.Begin); // TODO: What if !CanSeek? |
| 145 | + |
| 146 | + return StreamCopyOperation.CopyToAsync(readStream, Body, count, cancellationToken); |
137 | 147 | }
|
138 | 148 |
|
139 | 149 | var sendFile = HttpContext.Features.Get<IHttpSendFileFeature>();
|
140 | 150 |
|
141 | 151 | if (sendFile == null)
|
142 | 152 | {
|
143 |
| - return SendFileAsync(Body, fileName, offset, count, cancellationToken); |
| 153 | + return SendFileAsync(Body, file.PhysicalPath, offset, count, cancellationToken); |
144 | 154 | }
|
145 | 155 |
|
146 |
| - return sendFile.SendFileAsync(fileName, offset, count, cancellationToken); |
| 156 | + return sendFile.SendFileAsync(file.PhysicalPath, offset, count, cancellationToken); |
147 | 157 | }
|
148 | 158 |
|
149 | 159 | // Not safe for overlapped writes.
|
|
0 commit comments