From 5aa2e0630597abe28b86665b26f67f63d7150245 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Thu, 31 Dec 2015 15:28:50 -0800 Subject: [PATCH] Change PhysicalFileResult and VirtualFileResult to use SendFileAsync - SendFileAsync does the proper fallback to stream copy when the feature isn't available. Take advantage of it in MVC. There are plans to use the buffer pool as part of the stream copy so MVC will get that benefit for free. - Left CopyToAsync in SendFileAsync when the file doesn't have a physical path --- .../PhysicalFileResult.cs | 45 +------------------ .../VirtualFileResult.cs | 9 +--- 2 files changed, 4 insertions(+), 50 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.Core/PhysicalFileResult.cs b/src/Microsoft.AspNet.Mvc.Core/PhysicalFileResult.cs index 6c343d3fef..0752dd61c6 100644 --- a/src/Microsoft.AspNet.Mvc.Core/PhysicalFileResult.cs +++ b/src/Microsoft.AspNet.Mvc.Core/PhysicalFileResult.cs @@ -3,10 +3,8 @@ 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; @@ -18,7 +16,6 @@ namespace Microsoft.AspNet.Mvc /// public class PhysicalFileResult : FileResult { - private const int DefaultBufferSize = 0x1000; private string _fileName; /// @@ -83,52 +80,14 @@ namespace Microsoft.AspNet.Mvc } /// - protected override async Task WriteFileAsync(HttpResponse response) + protected override Task WriteFileAsync(HttpResponse response) { if (!Path.IsPathRooted(FileName)) { throw new NotSupportedException(Resources.FormatFileResult_PathNotRooted(FileName)); } - var sendFile = response.HttpContext.Features.Get(); - 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); - } - } - } - - /// - /// Returns for the specified . - /// - /// The path for which the is needed. - /// for the specified . - 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); + return response.SendFileAsync(FileName); } } } diff --git a/src/Microsoft.AspNet.Mvc.Core/VirtualFileResult.cs b/src/Microsoft.AspNet.Mvc.Core/VirtualFileResult.cs index 5dd5c302a5..96466e7342 100644 --- a/src/Microsoft.AspNet.Mvc.Core/VirtualFileResult.cs +++ b/src/Microsoft.AspNet.Mvc.Core/VirtualFileResult.cs @@ -106,14 +106,9 @@ namespace Microsoft.AspNet.Mvc if (fileInfo.Exists) { var physicalPath = fileInfo.PhysicalPath; - var sendFile = response.HttpContext.Features.Get(); - if (sendFile != null && !string.IsNullOrEmpty(physicalPath)) + if (!string.IsNullOrEmpty(physicalPath)) { - await sendFile.SendFileAsync( - physicalPath, - offset: 0, - length: null, - cancellation: default(CancellationToken)); + await response.SendFileAsync(physicalPath); } else {