Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Commit 869090a

Browse files
committed
Changed to IFileInfo instead of file name string
1 parent 93a2ca8 commit 869090a

File tree

4 files changed

+25
-14
lines changed

4 files changed

+25
-14
lines changed

src/Microsoft.AspNet.Http.Abstractions/HttpResponse.cs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Threading;
77
using System.Threading.Tasks;
88
using Microsoft.AspNet.Http.Features;
9+
using Microsoft.Extensions.FileProviders;
910

1011
namespace Microsoft.AspNet.Http
1112
{
@@ -109,41 +110,50 @@ public abstract class HttpResponse
109110
/// <summary>
110111
/// Sends the given file using the SendFile extension.
111112
/// </summary>
112-
/// <param name="fileName">The full path to the file.</param>
113+
/// <param name="file">The file to send.</param>
113114
/// <returns></returns>
114-
public virtual Task SendFileAsync(string fileName)
115+
public virtual Task SendFileAsync(IFileInfo file)
115116
{
116-
if (fileName == null)
117+
if (file == null)
117118
{
118-
throw new ArgumentNullException(nameof(fileName));
119+
throw new ArgumentNullException(nameof(file));
119120
}
120121

121-
return SendFileAsync(fileName, 0, null, CancellationToken.None);
122+
return SendFileAsync(file, 0, null, CancellationToken.None);
122123
}
123124

124125
/// <summary>
125126
/// Sends the given file using the SendFile extension.
126127
/// </summary>
127-
/// <param name="fileName">The full path to the file.</param>
128+
/// <param name="file">The file to send.</param>
128129
/// <param name="offset">The offset in the file.</param>
129130
/// <param name="count">The number of types to send, or null to send the remainder of the file.</param>
130131
/// <param name="cancellationToken"></param>
131132
/// <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))
133134
{
134-
if (fileName == null)
135+
if (file == null)
135136
{
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);
137147
}
138148

139149
var sendFile = HttpContext.Features.Get<IHttpSendFileFeature>();
140150

141151
if (sendFile == null)
142152
{
143-
return SendFileAsync(Body, fileName, offset, count, cancellationToken);
153+
return SendFileAsync(Body, file.PhysicalPath, offset, count, cancellationToken);
144154
}
145155

146-
return sendFile.SendFileAsync(fileName, offset, count, cancellationToken);
156+
return sendFile.SendFileAsync(file.PhysicalPath, offset, count, cancellationToken);
147157
}
148158

149159
// Not safe for overlapped writes.

src/Microsoft.AspNet.Http.Abstractions/project.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
},
1212
"dependencies": {
1313
"Microsoft.AspNet.Http.Features": "1.0.0-*",
14+
"Microsoft.Extensions.FileProviders.Abstractions": "1.0.0-*",
1415
"Microsoft.Extensions.ActivatorUtilities.Sources": {
1516
"type": "build",
1617
"version": "1.0.0-*"

src/Microsoft.AspNet.Http.Extensions/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616
},
1717
"frameworks": {
1818
"net451": {},
19-
"dotnet5.4": { }
19+
"dotnet5.4": {}
2020
}
2121
}

test/Microsoft.AspNet.Http.Extensions.Tests/SendFileResponseExtensionsTests.cs renamed to test/Microsoft.AspNet.Http.Abstractions.Tests/SendFileTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
using Microsoft.AspNet.Http.Internal;
99
using Xunit;
1010

11-
namespace Microsoft.AspNet.Http.Extensions.Tests
11+
namespace Microsoft.AspNet.Http.Abstractions.Tests
1212
{
13-
public class SendFileResponseExtensionsTests
13+
public class SendFileTests
1414
{
1515
[Fact]
1616
public Task SendFileWhenFileNotFoundThrows()

0 commit comments

Comments
 (0)