Simplify FileResult

Fixes #3249
This commit is contained in:
Pranav K 2015-10-05 11:57:34 -07:00
parent 04c6fceb94
commit 04d72d8894
6 changed files with 20 additions and 34 deletions

View File

@ -2,7 +2,6 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
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.Http.Features;
@ -82,12 +81,12 @@ namespace Microsoft.AspNet.Mvc
} }
/// <inheritdoc /> /// <inheritdoc />
protected override Task WriteFileAsync(HttpResponse response, CancellationToken cancellation) protected override Task WriteFileAsync(HttpResponse response)
{ {
var bufferingFeature = response.HttpContext.Features.Get<IHttpBufferingFeature>(); var bufferingFeature = response.HttpContext.Features.Get<IHttpBufferingFeature>();
bufferingFeature?.DisableResponseBuffering(); bufferingFeature?.DisableResponseBuffering();
return response.Body.WriteAsync(FileContents, 0, FileContents.Length, cancellation); return response.Body.WriteAsync(FileContents, offset: 0, count: FileContents.Length);
} }
} }
} }

View File

@ -2,7 +2,6 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.Net.Http.Headers; using Microsoft.Net.Http.Headers;
@ -83,21 +82,16 @@ namespace Microsoft.AspNet.Mvc
context.HttpContext.Response.Headers[HeaderNames.ContentDisposition] = cd.ToString(); context.HttpContext.Response.Headers[HeaderNames.ContentDisposition] = cd.ToString();
} }
// We aren't flowing the cancellation token appropriately, see return WriteFileAsync(response);
// https://github.com/aspnet/Mvc/issues/743 for details.
return WriteFileAsync(response, CancellationToken.None);
} }
/// <summary> /// <summary>
/// Writes the file to the response. /// Writes the file to the specified <paramref name="response"/>.
/// </summary> /// </summary>
/// <param name="response"> /// <param name="response">The <see cref="HttpResponse"/>.</param>
/// The <see cref="HttpResponse"/> where the file will be written
/// </param>
/// <param name="cancellation">The <see cref="CancellationToken"/>to cancel the operation.</param>
/// <returns> /// <returns>
/// A <see cref="Task"/> that will complete when the file has been written to the response. /// A <see cref="Task"/> that will complete when the file has been written to the response.
/// </returns> /// </returns>
protected abstract Task WriteFileAsync(HttpResponse response, CancellationToken cancellation); protected abstract Task WriteFileAsync(HttpResponse response);
} }
} }

View File

@ -78,7 +78,7 @@ namespace Microsoft.AspNet.Mvc
} }
/// <inheritdoc /> /// <inheritdoc />
protected async override Task WriteFileAsync(HttpResponse response, CancellationToken cancellation) protected async override Task WriteFileAsync(HttpResponse response)
{ {
var outputStream = response.Body; var outputStream = response.Body;
@ -87,7 +87,7 @@ namespace Microsoft.AspNet.Mvc
var bufferingFeature = response.HttpContext.Features.Get<IHttpBufferingFeature>(); var bufferingFeature = response.HttpContext.Features.Get<IHttpBufferingFeature>();
bufferingFeature?.DisableResponseBuffering(); bufferingFeature?.DisableResponseBuffering();
await FileStream.CopyToAsync(outputStream, BufferSize, cancellation); await FileStream.CopyToAsync(outputStream, BufferSize);
} }
} }
} }

View File

@ -83,7 +83,7 @@ namespace Microsoft.AspNet.Mvc
} }
/// <inheritdoc /> /// <inheritdoc />
protected override async Task WriteFileAsync(HttpResponse response, CancellationToken cancellation) protected override async Task WriteFileAsync(HttpResponse response)
{ {
if (!Path.IsPathRooted(FileName)) if (!Path.IsPathRooted(FileName))
{ {
@ -97,9 +97,7 @@ namespace Microsoft.AspNet.Mvc
FileName, FileName,
offset: 0, offset: 0,
length: null, length: null,
cancellation: cancellation); cancellation: default(CancellationToken));
return;
} }
else else
{ {
@ -107,10 +105,8 @@ namespace Microsoft.AspNet.Mvc
using (fileStream) using (fileStream)
{ {
await fileStream.CopyToAsync(response.Body, DefaultBufferSize, cancellation); await fileStream.CopyToAsync(response.Body, DefaultBufferSize);
} }
return;
} }
} }

View File

@ -92,7 +92,7 @@ namespace Microsoft.AspNet.Mvc
public IFileProvider FileProvider { get; set; } public IFileProvider FileProvider { get; set; }
/// <inheritdoc /> /// <inheritdoc />
protected override async Task WriteFileAsync(HttpResponse response, CancellationToken cancellation) protected override async Task WriteFileAsync(HttpResponse response)
{ {
var fileProvider = GetFileProvider(response.HttpContext.RequestServices); var fileProvider = GetFileProvider(response.HttpContext.RequestServices);
@ -113,24 +113,22 @@ namespace Microsoft.AspNet.Mvc
physicalPath, physicalPath,
offset: 0, offset: 0,
length: null, length: null,
cancellation: cancellation); cancellation: default(CancellationToken));
return;
} }
else else
{ {
var fileStream = GetFileStream(fileInfo); var fileStream = GetFileStream(fileInfo);
using (fileStream) using (fileStream)
{ {
await fileStream.CopyToAsync(response.Body, DefaultBufferSize, cancellation); await fileStream.CopyToAsync(response.Body, DefaultBufferSize);
} }
return;
} }
} }
else
throw new FileNotFoundException( {
Resources.FormatFileResult_InvalidPath(FileName), FileName); throw new FileNotFoundException(
Resources.FormatFileResult_InvalidPath(FileName), FileName);
}
} }
/// <summary> /// <summary>

View File

@ -3,7 +3,6 @@
using System.IO; using System.IO;
using System.Net.Mime; using System.Net.Mime;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Internal; 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; WasWriteFileCalled = true;
return Task.FromResult(0); return Task.FromResult(0);