-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #113 from hydrostack/90-handle-iresult-return-type
Handle IComponentResult
- Loading branch information
Showing
9 changed files
with
344 additions
and
17 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Hydro; | ||
|
||
/// <summary> | ||
/// Component result | ||
/// </summary> | ||
public interface IComponentResult | ||
{ | ||
/// <summary> | ||
/// Execute the result | ||
/// </summary> | ||
Task ExecuteAsync(HttpContext httpContext, HydroComponent component); | ||
} | ||
|
||
internal class ComponentResult : IComponentResult | ||
{ | ||
private readonly IResult _result; | ||
private readonly ComponentResultType _type; | ||
|
||
internal ComponentResult(IResult result, ComponentResultType type) | ||
{ | ||
_result = result; | ||
_type = type; | ||
} | ||
|
||
public async Task ExecuteAsync(HttpContext httpContext, HydroComponent component) | ||
{ | ||
var response = httpContext.Response; | ||
|
||
response.Headers.TryAdd(HydroConsts.ResponseHeaders.SkipOutput, "True"); | ||
|
||
if (_type == ComponentResultType.File) | ||
{ | ||
response.Headers.Append("Content-Disposition", "inline"); | ||
} | ||
|
||
await _result.ExecuteAsync(httpContext); | ||
|
||
if (response.Headers.Remove("Location", out var location)) | ||
{ | ||
response.StatusCode = StatusCodes.Status200OK; | ||
component.Redirect(location); | ||
} | ||
} | ||
} | ||
|
||
internal enum ComponentResultType | ||
{ | ||
Empty, | ||
File, | ||
Challenge, | ||
SignIn, | ||
SignOut | ||
} |
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,72 @@ | ||
using System.Security.Claims; | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Net.Http.Headers; | ||
|
||
namespace Hydro; | ||
|
||
/// <summary> | ||
/// Results for Hydro actions | ||
/// </summary> | ||
public static class ComponentResults | ||
{ | ||
/// <summary> | ||
/// Create a ChallengeHttpResult | ||
/// </summary> | ||
public static IComponentResult Challenge( | ||
AuthenticationProperties properties = null, | ||
IList<string> authenticationSchemes = null) | ||
=> new ComponentResult(Results.Challenge(properties, authenticationSchemes), ComponentResultType.Challenge); | ||
|
||
/// <summary> | ||
/// Creates a SignInHttpResult | ||
/// </summary> | ||
public static IComponentResult SignIn( | ||
ClaimsPrincipal principal, | ||
AuthenticationProperties properties = null, | ||
string authenticationScheme = null) | ||
=> new ComponentResult(Results.SignIn(principal, properties, authenticationScheme), ComponentResultType.SignIn); | ||
|
||
/// <summary> | ||
/// Creates a SignOutHttpResult | ||
/// </summary> | ||
public static IComponentResult SignOut(AuthenticationProperties properties = null, IList<string> authenticationSchemes = null) | ||
=> new ComponentResult(Results.SignOut(properties, authenticationSchemes), ComponentResultType.SignOut); | ||
|
||
/// <summary> | ||
/// Creates a FileContentHttpResult | ||
/// </summary> | ||
public static IComponentResult File( | ||
byte[] fileContents, | ||
string contentType = null, | ||
string fileDownloadName = null, | ||
bool enableRangeProcessing = false, | ||
DateTimeOffset? lastModified = null, | ||
EntityTagHeaderValue entityTag = null) | ||
=> new ComponentResult(Results.File(fileContents, contentType, fileDownloadName, enableRangeProcessing, lastModified, entityTag), ComponentResultType.File); | ||
|
||
|
||
/// <summary> | ||
/// Creates a FileStreamHttpResult | ||
/// </summary> | ||
public static IComponentResult File( | ||
Stream fileStream, | ||
string contentType = null, | ||
string fileDownloadName = null, | ||
DateTimeOffset? lastModified = null, | ||
EntityTagHeaderValue entityTag = null, | ||
bool enableRangeProcessing = false) | ||
=> new ComponentResult(Results.File(fileStream, contentType, fileDownloadName, lastModified, entityTag, enableRangeProcessing), ComponentResultType.File); | ||
|
||
/// <summary> | ||
/// Returns either PhysicalFileHttpResult or VirtualFileHttpResult | ||
/// </summary> | ||
public static IComponentResult File( | ||
string path, | ||
string contentType = null, | ||
string fileDownloadName = null, | ||
DateTimeOffset? lastModified = null, | ||
EntityTagHeaderValue entityTag = null, | ||
bool enableRangeProcessing = false) | ||
=> new ComponentResult(Results.File(path, contentType, fileDownloadName, lastModified, entityTag, enableRangeProcessing), ComponentResultType.File); | ||
} |
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
Oops, something went wrong.