Revert "Change PhysicalFileResult and VirtualFileResult to use SendFileAsync"

This reverts commit 5aa2e06305.
This commit is contained in:
David Fowler 2015-12-31 16:17:53 -08:00
parent 5aa2e06305
commit ba5fe60cf5
2 changed files with 50 additions and 4 deletions

View File

@ -3,8 +3,10 @@
using System; using System;
using System.IO; using System.IO;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Features;
using Microsoft.AspNet.Mvc.Core; using Microsoft.AspNet.Mvc.Core;
using Microsoft.Net.Http.Headers; using Microsoft.Net.Http.Headers;
@ -16,6 +18,7 @@ namespace Microsoft.AspNet.Mvc
/// </summary> /// </summary>
public class PhysicalFileResult : FileResult public class PhysicalFileResult : FileResult
{ {
private const int DefaultBufferSize = 0x1000;
private string _fileName; private string _fileName;
/// <summary> /// <summary>
@ -80,14 +83,52 @@ namespace Microsoft.AspNet.Mvc
} }
/// <inheritdoc /> /// <inheritdoc />
protected override Task WriteFileAsync(HttpResponse response) protected override async Task WriteFileAsync(HttpResponse response)
{ {
if (!Path.IsPathRooted(FileName)) if (!Path.IsPathRooted(FileName))
{ {
throw new NotSupportedException(Resources.FormatFileResult_PathNotRooted(FileName)); throw new NotSupportedException(Resources.FormatFileResult_PathNotRooted(FileName));
} }
return response.SendFileAsync(FileName); var sendFile = response.HttpContext.Features.Get<IHttpSendFileFeature>();
if (sendFile != null)
{
await sendFile.SendFileAsync(
FileName,
offset: 0,
length: null,
cancellation: default(CancellationToken));
}
else
{
var fileStream = GetFileStream(FileName);
using (fileStream)
{
await fileStream.CopyToAsync(response.Body, DefaultBufferSize);
}
}
}
/// <summary>
/// Returns <see cref="Stream"/> for the specified <paramref name="path"/>.
/// </summary>
/// <param name="path">The path for which the <see cref="FileStream"/> is needed.</param>
/// <returns><see cref="FileStream"/> for the specified <paramref name="path"/>.</returns>
protected virtual Stream GetFileStream(string path)
{
if (path == null)
{
throw new ArgumentNullException(nameof(path));
}
return new FileStream(
path,
FileMode.Open,
FileAccess.Read,
FileShare.ReadWrite,
DefaultBufferSize,
FileOptions.Asynchronous | FileOptions.SequentialScan);
} }
} }
} }

View File

@ -106,9 +106,14 @@ namespace Microsoft.AspNet.Mvc
if (fileInfo.Exists) if (fileInfo.Exists)
{ {
var physicalPath = fileInfo.PhysicalPath; var physicalPath = fileInfo.PhysicalPath;
if (!string.IsNullOrEmpty(physicalPath)) var sendFile = response.HttpContext.Features.Get<IHttpSendFileFeature>();
if (sendFile != null && !string.IsNullOrEmpty(physicalPath))
{ {
await response.SendFileAsync(physicalPath); await sendFile.SendFileAsync(
physicalPath,
offset: 0,
length: null,
cancellation: default(CancellationToken));
} }
else else
{ {