forked from OrchardCMS/OrchardCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace ScriptsMiddleware(OrchardCMS#15629)
- Loading branch information
1 parent
b4ff27a
commit 34b0355
Showing
8 changed files
with
199 additions
and
135 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
56 changes: 56 additions & 0 deletions
56
src/OrchardCore.Modules/OrchardCore.Facebook/Endpoints/Api/InitSdkEndpoint.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,56 @@ | ||
using System.Globalization; | ||
using Microsoft.AspNetCore.Routing; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using OrchardCore.ContentManagement; | ||
using OrchardCore.Settings; | ||
using OrchardCore.Facebook.Settings; | ||
|
||
namespace OrchardCore.Facebook; | ||
internal static class InitSdkEndpoint | ||
{ | ||
public static IEndpointRouteBuilder AddInitSdkEndpoint(this IEndpointRouteBuilder builder) | ||
{ | ||
builder.MapGet("orchardcore/facebook/sdk/fbsdk.js", HandleAsync) | ||
.AllowAnonymous() | ||
.DisableAntiforgery(); | ||
|
||
return builder; | ||
} | ||
|
||
[Authorize(AuthenticationSchemes = "Api")] | ||
private static async Task<IResult> HandleAsync( | ||
ContentItem model, | ||
ISiteService siteService, | ||
HttpContext httpContext, | ||
bool draft = false) | ||
{ | ||
ArgumentNullException.ThrowIfNull(httpContext); | ||
|
||
var script = await getInitSdkScript(siteService); | ||
|
||
if (script == null) | ||
{ | ||
return Results.NotFound(); | ||
} | ||
|
||
return Results.Ok(script); | ||
} | ||
|
||
private static async Task<string> getInitSdkScript(ISiteService siteService) | ||
{ | ||
var settings = await siteService.GetSettingsAsync<FacebookSettings>(); | ||
var locale = CultureInfo.CurrentUICulture.Name.Replace('-', '_'); | ||
|
||
var script = $@"(function(d){{ | ||
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {{ return; }} | ||
js = d.createElement('script'); js.id = id; js.async = true; | ||
js.src = ""https://connect.facebook.net/{locale}/{settings.SdkJs}""; | ||
d.getElementsByTagName('head')[0].appendChild(js); | ||
}} (document));"; | ||
|
||
return script; | ||
} | ||
} | ||
|
61 changes: 61 additions & 0 deletions
61
src/OrchardCore.Modules/OrchardCore.Facebook/Endpoints/Api/LoadSdkEndPoint.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,61 @@ | ||
using System.Globalization; | ||
using Microsoft.AspNetCore.Routing; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using OrchardCore.ContentManagement; | ||
using OrchardCore.Settings; | ||
using OrchardCore.Facebook.Settings; | ||
|
||
namespace OrchardCore.Facebook; | ||
internal static class LoadSdkEndpoint | ||
{ | ||
public static IEndpointRouteBuilder AddLoadSdkEndpoint(this IEndpointRouteBuilder builder) | ||
{ | ||
builder.MapGet("orchardcore/facebook/sdk/fb.js", HandleAsync) | ||
.AllowAnonymous() | ||
.DisableAntiforgery(); | ||
|
||
return builder; | ||
} | ||
|
||
[Authorize(AuthenticationSchemes = "Api")] | ||
private static async Task<IResult> HandleAsync( | ||
ContentItem model, | ||
ISiteService siteService, | ||
HttpContext httpContext, | ||
bool draft = false) | ||
{ | ||
ArgumentNullException.ThrowIfNull(httpContext); | ||
|
||
var script = await getLoadSdkScript(siteService); | ||
|
||
if (script == null) | ||
{ | ||
return Results.NotFound(); | ||
} | ||
|
||
return Results.Ok(script); | ||
} | ||
|
||
private static async Task<string> getLoadSdkScript(ISiteService siteService) | ||
{ | ||
var settings = await siteService.GetSettingsAsync<FacebookSettings>(); | ||
var locale = CultureInfo.CurrentUICulture.Name.Replace('-', '_'); | ||
|
||
var script = default(string); | ||
|
||
if (!string.IsNullOrWhiteSpace(settings?.AppId)) | ||
{ | ||
var options = $"{{ appId:'{settings.AppId}',version:'{settings.Version}'"; | ||
options = string.IsNullOrWhiteSpace(settings.FBInitParams) | ||
? string.Concat(options, "}") | ||
: string.Concat(options, ",", settings.FBInitParams, "}"); | ||
|
||
script = $"window.fbAsyncInit = function(){{ FB.init({options});}};"; | ||
} | ||
|
||
return script; | ||
} | ||
} | ||
|
63 changes: 0 additions & 63 deletions
63
src/OrchardCore.Modules/OrchardCore.Facebook/ScriptsMiddleware.cs
This file was deleted.
Oops, something went wrong.
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
74 changes: 74 additions & 0 deletions
74
src/OrchardCore.Modules/OrchardCore.Liquid/Endpoints/Api/GetIntellisenseEndpoint.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,74 @@ | ||
using Fluid; | ||
using System.Text; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Routing; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.Net.Http.Headers; | ||
using OrchardCore.ContentManagement; | ||
using OrchardCore.DisplayManagement.Liquid; | ||
using OrchardCore.Environment.Shell.Configuration; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Caching.Memory; | ||
|
||
namespace OrchardCore.Liquid.Endpoints.Api; | ||
|
||
public static class GetIntellisenseEndpoint | ||
{ | ||
|
||
public static IEndpointRouteBuilder AddGetIntellisenseEndpoint(this IEndpointRouteBuilder builder) | ||
{ | ||
builder.MapGet("/OrchardCore.Liquid/Scripts/liquid-intellisense.js", HandleAsync) | ||
.AllowAnonymous() | ||
.DisableAntiforgery(); | ||
|
||
return builder; | ||
} | ||
|
||
[Authorize(AuthenticationSchemes = "Api")] | ||
private static Task<IResult> HandleAsync(HttpContext httpContext, IMemoryCache cache) | ||
{ | ||
const string cacheKey = "LiquidIntellisenseScript"; | ||
|
||
var cacheControl = $"public, max-age={TimeSpan.FromDays(30).TotalSeconds}, s-max-age={TimeSpan.FromDays(365.25).TotalSeconds}"; | ||
if (cache.TryGetValue(cacheKey, out (byte[] Bytes, string ETag) cachedData)) | ||
{ | ||
if (httpContext.Request.Headers.TryGetValue(HeaderNames.IfNoneMatch, out var etagHeader) && | ||
etagHeader.Contains(cachedData.ETag)) | ||
{ | ||
return Task.FromResult(Results.StatusCode(StatusCodes.Status304NotModified)); | ||
} | ||
|
||
httpContext.Response.Headers[HeaderNames.CacheControl] = cacheControl; | ||
httpContext.Response.Headers[HeaderNames.ContentType] = "application/javascript"; | ||
httpContext.Response.Headers[HeaderNames.ETag] = cachedData.ETag; | ||
|
||
return Task.FromResult(Results.Bytes(cachedData.Bytes, "application/javascript")); | ||
} | ||
|
||
var shellConfiguration = httpContext.RequestServices.GetRequiredService<IShellConfiguration>(); | ||
cacheControl = shellConfiguration.GetValue("StaticFileOptions:CacheControl", cacheControl); | ||
|
||
var templateOptions = httpContext.RequestServices.GetRequiredService<IOptions<TemplateOptions>>(); | ||
var liquidViewParser = httpContext.RequestServices.GetRequiredService<LiquidViewParser>(); | ||
|
||
var filters = string.Join(',', templateOptions.Value.Filters.Select(x => $"'{x.Key}'")); | ||
var tags = string.Join(',', liquidViewParser.RegisteredTags.Select(x => $"'{x.Key}'")); | ||
|
||
var script = $@"[{filters}].forEach(value=>{{if(!liquidFilters.includes(value)){{ liquidFilters.push(value);}}}}); | ||
[{tags}].forEach(value=>{{if(!liquidTags.includes(value)){{ liquidTags.push(value);}}}});"; | ||
|
||
var etag = Guid.NewGuid().ToString("n"); | ||
var bytes = Encoding.UTF8.GetBytes(script); | ||
cache.Set(cacheKey,(bytes,etag)); | ||
|
||
httpContext.Response.Headers[HeaderNames.CacheControl] = cacheControl; | ||
httpContext.Response.Headers[HeaderNames.ContentType] = "application/javascript"; | ||
httpContext.Response.Headers[HeaderNames.ETag] = etag; | ||
|
||
return Task.FromResult(Results.Bytes(bytes, "application/javascript")); | ||
} | ||
|
||
} |
70 changes: 0 additions & 70 deletions
70
src/OrchardCore.Modules/OrchardCore.Liquid/ScriptsMiddleware.cs
This file was deleted.
Oops, something went wrong.
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