-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor ProjectController to use FileCallbackResult instead of my ha…
…cked version
- Loading branch information
Showing
4 changed files
with
58 additions
and
14 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
backend/LexBoxApi/Controllers/ActionResults/FileCallbackResult.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Infrastructure; | ||
|
||
namespace LexBoxApi.Controllers.ActionResults; | ||
|
||
/// <summary> | ||
/// Represents an <see cref="ActionResult"/> that when executed will | ||
/// execute a callback to write the file content out as a stream. | ||
/// copied from https://github.com/StephenClearyExamples/AsyncDynamicZip/tree/net6-ziparchive | ||
/// MIT License | ||
/// </summary> | ||
public sealed class FileCallbackResult : FileResult | ||
{ | ||
private readonly Func<Stream, ActionContext, Task> _callback; | ||
|
||
/// <summary> | ||
/// Creates a new <see cref="FileCallbackResult"/> instance. | ||
/// </summary> | ||
/// <param name="contentType">The Content-Type header of the response.</param> | ||
/// <param name="callback">The stream with the file.</param> | ||
public FileCallbackResult(string contentType, Func<Stream, ActionContext, Task> callback) | ||
: base(contentType) | ||
{ | ||
_ = callback ?? throw new ArgumentNullException(nameof(callback)); | ||
_callback = callback; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override Task ExecuteResultAsync(ActionContext context) | ||
{ | ||
_ = context ?? throw new ArgumentNullException(nameof(context)); | ||
var executor = | ||
new FileCallbackResultExecutor(context.HttpContext.RequestServices.GetRequiredService<ILoggerFactory>()); | ||
return executor.ExecuteAsync(context, this); | ||
} | ||
|
||
private sealed class FileCallbackResultExecutor : FileResultExecutorBase | ||
{ | ||
public FileCallbackResultExecutor(ILoggerFactory loggerFactory) | ||
: base(CreateLogger<FileCallbackResultExecutor>(loggerFactory)) | ||
{ | ||
} | ||
|
||
public Task ExecuteAsync(ActionContext context, FileCallbackResult result) | ||
{ | ||
SetHeadersAndLog(context, result, fileLength: null, enableRangeProcessing: false); | ||
return result._callback(context.HttpContext.Response.BodyWriter.AsStream(), context); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters