From 04d72d88947f72b292beac0f1359b548fb2e60ba Mon Sep 17 00:00:00 2001 From: Pranav K Date: Mon, 5 Oct 2015 11:57:34 -0700 Subject: [PATCH] Simplify FileResult Fixes #3249 --- .../FileContentResult.cs | 5 ++--- src/Microsoft.AspNet.Mvc.Core/FileResult.cs | 14 ++++---------- .../FileStreamResult.cs | 4 ++-- .../PhysicalFileResult.cs | 10 +++------- .../VirtualFileResult.cs | 18 ++++++++---------- .../FileResultTest.cs | 3 +-- 6 files changed, 20 insertions(+), 34 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.Core/FileContentResult.cs b/src/Microsoft.AspNet.Mvc.Core/FileContentResult.cs index 558d76eaaa..292928217e 100644 --- a/src/Microsoft.AspNet.Mvc.Core/FileContentResult.cs +++ b/src/Microsoft.AspNet.Mvc.Core/FileContentResult.cs @@ -2,7 +2,6 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.Threading; using System.Threading.Tasks; using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Features; @@ -82,12 +81,12 @@ namespace Microsoft.AspNet.Mvc } /// - protected override Task WriteFileAsync(HttpResponse response, CancellationToken cancellation) + protected override Task WriteFileAsync(HttpResponse response) { var bufferingFeature = response.HttpContext.Features.Get(); bufferingFeature?.DisableResponseBuffering(); - return response.Body.WriteAsync(FileContents, 0, FileContents.Length, cancellation); + return response.Body.WriteAsync(FileContents, offset: 0, count: FileContents.Length); } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/FileResult.cs b/src/Microsoft.AspNet.Mvc.Core/FileResult.cs index 84db886e74..504cfcdd4f 100644 --- a/src/Microsoft.AspNet.Mvc.Core/FileResult.cs +++ b/src/Microsoft.AspNet.Mvc.Core/FileResult.cs @@ -2,7 +2,6 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.Threading; using System.Threading.Tasks; using Microsoft.AspNet.Http; using Microsoft.Net.Http.Headers; @@ -83,21 +82,16 @@ namespace Microsoft.AspNet.Mvc context.HttpContext.Response.Headers[HeaderNames.ContentDisposition] = cd.ToString(); } - // We aren't flowing the cancellation token appropriately, see - // https://github.com/aspnet/Mvc/issues/743 for details. - return WriteFileAsync(response, CancellationToken.None); + return WriteFileAsync(response); } /// - /// Writes the file to the response. + /// Writes the file to the specified . /// - /// - /// The where the file will be written - /// - /// The to cancel the operation. + /// The . /// /// A that will complete when the file has been written to the response. /// - protected abstract Task WriteFileAsync(HttpResponse response, CancellationToken cancellation); + protected abstract Task WriteFileAsync(HttpResponse response); } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/FileStreamResult.cs b/src/Microsoft.AspNet.Mvc.Core/FileStreamResult.cs index 6533c49826..91ca79fe6b 100644 --- a/src/Microsoft.AspNet.Mvc.Core/FileStreamResult.cs +++ b/src/Microsoft.AspNet.Mvc.Core/FileStreamResult.cs @@ -78,7 +78,7 @@ namespace Microsoft.AspNet.Mvc } /// - protected async override Task WriteFileAsync(HttpResponse response, CancellationToken cancellation) + protected async override Task WriteFileAsync(HttpResponse response) { var outputStream = response.Body; @@ -87,7 +87,7 @@ namespace Microsoft.AspNet.Mvc var bufferingFeature = response.HttpContext.Features.Get(); bufferingFeature?.DisableResponseBuffering(); - await FileStream.CopyToAsync(outputStream, BufferSize, cancellation); + await FileStream.CopyToAsync(outputStream, BufferSize); } } } diff --git a/src/Microsoft.AspNet.Mvc.Core/PhysicalFileResult.cs b/src/Microsoft.AspNet.Mvc.Core/PhysicalFileResult.cs index 06a8a9307f..c82aeacf94 100644 --- a/src/Microsoft.AspNet.Mvc.Core/PhysicalFileResult.cs +++ b/src/Microsoft.AspNet.Mvc.Core/PhysicalFileResult.cs @@ -83,7 +83,7 @@ namespace Microsoft.AspNet.Mvc } /// - protected override async Task WriteFileAsync(HttpResponse response, CancellationToken cancellation) + protected override async Task WriteFileAsync(HttpResponse response) { if (!Path.IsPathRooted(FileName)) { @@ -97,9 +97,7 @@ namespace Microsoft.AspNet.Mvc FileName, offset: 0, length: null, - cancellation: cancellation); - - return; + cancellation: default(CancellationToken)); } else { @@ -107,10 +105,8 @@ namespace Microsoft.AspNet.Mvc using (fileStream) { - await fileStream.CopyToAsync(response.Body, DefaultBufferSize, cancellation); + await fileStream.CopyToAsync(response.Body, DefaultBufferSize); } - - return; } } diff --git a/src/Microsoft.AspNet.Mvc.Core/VirtualFileResult.cs b/src/Microsoft.AspNet.Mvc.Core/VirtualFileResult.cs index f4334218f7..3fb664130b 100644 --- a/src/Microsoft.AspNet.Mvc.Core/VirtualFileResult.cs +++ b/src/Microsoft.AspNet.Mvc.Core/VirtualFileResult.cs @@ -92,7 +92,7 @@ namespace Microsoft.AspNet.Mvc public IFileProvider FileProvider { get; set; } /// - protected override async Task WriteFileAsync(HttpResponse response, CancellationToken cancellation) + protected override async Task WriteFileAsync(HttpResponse response) { var fileProvider = GetFileProvider(response.HttpContext.RequestServices); @@ -113,24 +113,22 @@ namespace Microsoft.AspNet.Mvc physicalPath, offset: 0, length: null, - cancellation: cancellation); - - return; + cancellation: default(CancellationToken)); } else { var fileStream = GetFileStream(fileInfo); using (fileStream) { - await fileStream.CopyToAsync(response.Body, DefaultBufferSize, cancellation); + await fileStream.CopyToAsync(response.Body, DefaultBufferSize); } - - return; } } - - throw new FileNotFoundException( - Resources.FormatFileResult_InvalidPath(FileName), FileName); + else + { + throw new FileNotFoundException( + Resources.FormatFileResult_InvalidPath(FileName), FileName); + } } /// diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/FileResultTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/FileResultTest.cs index c479c53b24..63057caa1e 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/FileResultTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/FileResultTest.cs @@ -3,7 +3,6 @@ using System.IO; using System.Net.Mime; -using System.Threading; using System.Threading.Tasks; using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Internal; @@ -227,7 +226,7 @@ namespace Microsoft.AspNet.Mvc { } - protected override Task WriteFileAsync(HttpResponse response, CancellationToken cancellation) + protected override Task WriteFileAsync(HttpResponse response) { WasWriteFileCalled = true; return Task.FromResult(0);