Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
Revert "Change PhysicalFileResult and VirtualFileResult to use SendFi…
Browse files Browse the repository at this point in the history
…leAsync"

This reverts commit 5aa2e06.
  • Loading branch information
davidfowl committed Jan 1, 2016
1 parent 5aa2e06 commit ba5fe60
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
45 changes: 43 additions & 2 deletions src/Microsoft.AspNet.Mvc.Core/PhysicalFileResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Features;
using Microsoft.AspNet.Mvc.Core;
using Microsoft.Net.Http.Headers;

Expand All @@ -16,6 +18,7 @@ namespace Microsoft.AspNet.Mvc
/// </summary>
public class PhysicalFileResult : FileResult
{
private const int DefaultBufferSize = 0x1000;
private string _fileName;

/// <summary>
Expand Down Expand Up @@ -80,14 +83,52 @@ public string FileName
}

/// <inheritdoc />
protected override Task WriteFileAsync(HttpResponse response)
protected override async Task WriteFileAsync(HttpResponse response)
{
if (!Path.IsPathRooted(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);
}
}
}
9 changes: 7 additions & 2 deletions src/Microsoft.AspNet.Mvc.Core/VirtualFileResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,14 @@ protected override async Task WriteFileAsync(HttpResponse response)
if (fileInfo.Exists)
{
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
{
Expand Down

0 comments on commit ba5fe60

Please sign in to comment.