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

Commit

Permalink
Range support for FileResult(#6150)
Browse files Browse the repository at this point in the history
Addresses #3702
  • Loading branch information
jbagga authored May 19, 2017
1 parent ece5e74 commit 9aff0a6
Show file tree
Hide file tree
Showing 21 changed files with 2,655 additions and 84 deletions.
203 changes: 197 additions & 6 deletions src/Microsoft.AspNetCore.Mvc.Core/ControllerBase.cs

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions src/Microsoft.AspNetCore.Mvc.Core/FileContentResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Core;
using Microsoft.AspNetCore.Mvc.Internal;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Net.Http.Headers;
Expand Down
13 changes: 11 additions & 2 deletions src/Microsoft.AspNetCore.Mvc.Core/FileResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Net.Http.Headers;

namespace Microsoft.AspNetCore.Mvc
{
Expand Down Expand Up @@ -43,5 +42,15 @@ public string FileDownloadName
get { return _fileDownloadName ?? string.Empty; }
set { _fileDownloadName = value; }
}

/// <summary>
/// Gets or sets the last modified information associated with the <see cref="FileResult"/>.
/// </summary>
public DateTimeOffset? LastModified { get; set; }

/// <summary>
/// Gets or sets the etag associated with the <see cref="FileResult"/>.
/// </summary>
public EntityTagHeaderValue EntityTag { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers;

namespace Microsoft.AspNetCore.Mvc.Internal
{
Expand All @@ -26,11 +28,22 @@ public virtual Task ExecuteAsync(ActionContext context, FileContentResult result
throw new ArgumentNullException(nameof(result));
}

SetHeadersAndLog(context, result);
return WriteFileAsync(context, result);
var (range, rangeLength, serveBody) = SetHeadersAndLog(
context,
result,
result.FileContents.Length,
result.LastModified,
result.EntityTag);

if (!serveBody)
{
return Task.CompletedTask;
}

return WriteFileAsync(context, result, range, rangeLength);
}

protected virtual Task WriteFileAsync(ActionContext context, FileContentResult result)
protected virtual Task WriteFileAsync(ActionContext context, FileContentResult result, RangeItemHeaderValue range, long rangeLength)
{
if (context == null)
{
Expand All @@ -42,9 +55,15 @@ protected virtual Task WriteFileAsync(ActionContext context, FileContentResult r
throw new ArgumentNullException(nameof(result));
}

var response = context.HttpContext.Response;
if (range != null && rangeLength == 0)
{
return Task.CompletedTask;
}

return response.Body.WriteAsync(result.FileContents, offset: 0, count: result.FileContents.Length);
var response = context.HttpContext.Response;
var outputStream = response.Body;
var fileContentsStream = new MemoryStream(result.FileContents);
return WriteFileAsync(context.HttpContext, fileContentsStream, range, rangeLength);
}
}
}
Loading

0 comments on commit 9aff0a6

Please sign in to comment.