Skip to content

Commit

Permalink
Merge pull request #72 from Lombiq/issue/NEST-536
Browse files Browse the repository at this point in the history
NEST-536: Fix shape resolution
  • Loading branch information
sarahelsaig authored Aug 23, 2024
2 parents 8662637 + 530e774 commit 18ac502
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="OrchardCore.Deployment.Abstractions" Version="2.0.0-preview-18312" />
<PackageReference Include="OrchardCore.DisplayManagement.Abstractions" Version="2.0.0-preview-18312" />
<PackageReference Include="OrchardCore.Recipes.Abstractions" Version="2.0.0-preview-18312" />
<PackageReference Include="OrchardCore.ResourceManagement.Abstractions" Version="2.0.0-preview-18312" />
<PackageReference Include="OrchardCore.Media" Version="2.0.0-preview-18312" />
<PackageReference Include="OrchardCore.Module.Targets" Version="2.0.0-preview-18312" />
<PackageReference Include="OrchardCore.Deployment.Abstractions" Version="2.0.0-preview-18315" />
<PackageReference Include="OrchardCore.DisplayManagement.Abstractions" Version="2.0.0-preview-18315" />
<PackageReference Include="OrchardCore.Recipes.Abstractions" Version="2.0.0-preview-18315" />
<PackageReference Include="OrchardCore.ResourceManagement.Abstractions" Version="2.0.0-preview-18315" />
<PackageReference Include="OrchardCore.Media" Version="2.0.0-preview-18315" />
<PackageReference Include="OrchardCore.Module.Targets" Version="2.0.0-preview-18315" />
<PackageReference Include="Scrutor" Version="4.2.2" />
</ItemGroup>

Expand All @@ -44,4 +44,8 @@
<PackageReference Include="Lombiq.HelpfulLibraries.OrchardCore" Version="10.0.1-alpha.5.occ-280" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\Modules\Lombiq.HelpfulExtensions\Lombiq.HelpfulExtensions\Lombiq.HelpfulExtensions.csproj" />
</ItemGroup>

</Project>
3 changes: 2 additions & 1 deletion Lombiq.Hosting.MediaTheme.Bridge/Manifest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Lombiq.HelpfulExtensions;
using OrchardCore.Modules.Manifest;

[assembly: Module(
Expand All @@ -7,5 +8,5 @@
Version = "0.0.1",
Description = "Provides the processing logic for the Media Theme.",
Category = "Hosting",
Dependencies = ["OrchardCore.Deployment", "OrchardCore.Media"]
Dependencies = ["OrchardCore.Deployment", "OrchardCore.Media", FeatureIds.GoogleTag]
)]
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Http;
using OrchardCore.Admin;
using OrchardCore.DisplayManagement;
using OrchardCore.DisplayManagement.Descriptors;
using OrchardCore.DisplayManagement.Implementation;
using OrchardCore.Liquid;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
Expand All @@ -27,27 +29,20 @@ public MediaTemplatesShapeBindingResolver(
_mediaThemeCachingService = mediaThemeCachingService;
}

public async Task<ShapeBinding> GetShapeBindingAsync(string shapeType)
{
if (AdminAttribute.IsApplied(_hca.HttpContext))
{
return null;
}
public async Task<ShapeBinding> GetShapeBindingAsync(string shapeType) =>
!AdminAttribute.IsApplied(_hca.HttpContext) &&
await _mediaThemeCachingService.GetMemoryCachedMediaTemplateAsync(shapeType) is { } mediaTemplate
? new()
{
BindingName = shapeType,
BindingSource = shapeType,
BindingAsync = displayContext => BindingAsync(displayContext, mediaTemplate.Content),
}
: null;

return await _mediaThemeCachingService.GetMemoryCachedMediaTemplateAsync(shapeType) is not { } mediaTemplate
? null
: BuildShapeBinding(shapeType, mediaTemplate.Content);
private async Task<IHtmlContent> BindingAsync(DisplayContext displayContext, string text)
{
var content = await _liquidTemplateManager.RenderHtmlContentAsync(text, _htmlEncoder, displayContext.Value);
return content;
}

private ShapeBinding BuildShapeBinding(string shapeType, string text) =>
new()
{
BindingName = shapeType,
BindingSource = shapeType,
BindingAsync = async displayContext =>
{
var content = await _liquidTemplateManager.RenderHtmlContentAsync(text, _htmlEncoder, displayContext.Value);
return content;
},
};
}
27 changes: 22 additions & 5 deletions Lombiq.Hosting.MediaTheme.Bridge/Services/MediaThemeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ namespace Lombiq.Hosting.MediaTheme.Bridge.Services;

public class MediaThemeManager : IMediaThemeManager
{
private const string LiquidExtension = ".liquid";

private readonly IMediaThemeStateStore _mediaThemeStateStore;
private readonly IShellFeaturesManager _shellFeaturesManager;
private readonly IMemoryCache _memoryCache;
Expand Down Expand Up @@ -77,11 +79,21 @@ public async Task UpdateBaseThemeAsync(string baseThemeId)

public async Task<MediaTemplate> GetMediaTemplateByShapeTypeAsync(string shapeType)
{
var templatePath = _mediaFileStore.Combine(
Paths.MediaThemeRootFolder,
Paths.MediaThemeTemplatesFolder,
shapeType + ".liquid");
if (!await _mediaFileStore.FileExistsAsync(templatePath)) return null;
var templatePath = GetPath(shapeType + LiquidExtension);

// Check if the file exists with a different casing, before giving up. Re-evaluate if this is still needed after
// https://github.com/OrchardCMS/OrchardCore/issues/16585 is resolved.
if (!await _mediaFileStore.FileExistsAsync(templatePath))
{
var basePath = GetPath();
var file = await _mediaFileStore
.GetDirectoryContentAsync(basePath)
.Where(file => file.Name.EqualsOrdinalIgnoreCase(shapeType + LiquidExtension))
.FirstOrDefaultAsync();

if (file == null) return null;
templatePath = GetPath(file.Name);
}

await using var templateFileStream = await _mediaFileStore.GetFileStreamAsync(templatePath);
using var reader = new StreamReader(templateFileStream);
Expand All @@ -95,6 +107,11 @@ public async Task<MediaTemplate> GetMediaTemplateByShapeTypeAsync(string shapeTy
};
}

private string GetPath(string shapeType = null) =>
shapeType == null
? _mediaFileStore.Combine(Paths.MediaThemeRootFolder, Paths.MediaThemeTemplatesFolder)
: _mediaFileStore.Combine(Paths.MediaThemeRootFolder, Paths.MediaThemeTemplatesFolder, shapeType);

private static void ThrowIfBaseThemeIdIsInvalid(string baseThemeId)
{
if (baseThemeId == FeatureNames.MediaTheme)
Expand Down
5 changes: 1 addition & 4 deletions Lombiq.Hosting.MediaTheme.Bridge/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using Microsoft.Extensions.DependencyInjection;
using OrchardCore.Deployment;
using OrchardCore.DisplayManagement;
using OrchardCore.DisplayManagement.Handlers;
using OrchardCore.Environment.Extensions;
using OrchardCore.Modules;
using OrchardCore.Navigation;
Expand All @@ -35,9 +34,7 @@ public override void ConfigureServices(IServiceCollection services)
services.AddScoped<IShapeBindingResolver, MediaTemplatesShapeBindingResolver>();
services.AddScoped<IMediaThemeManager, MediaThemeManager>();
services.AddRecipeExecutionStep<MediaThemeStep>();
services.AddTransient<IDeploymentSource, MediaThemeDeploymentSource>();
services.AddSingleton<IDeploymentStepFactory>(new DeploymentStepFactory<MediaThemeDeploymentStep>());
services.AddScoped<IDisplayDriver<DeploymentStep>, MediaThemeDeploymentStepDriver>();
services.AddDeployment<MediaThemeDeploymentSource, MediaThemeDeploymentStep, MediaThemeDeploymentStepDriver>();
services.AddScoped<IAuthorizationHandler, ManageMediaThemeFolderAuthorizationHandler>();
services.AddScoped<IMediaThemeCachingService, MediaThemeCachingService>();
services.AddOrchardServices();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="OrchardCore.Theme.Targets" Version="2.0.0-preview-18312" />
<PackageReference Include="OrchardCore.ContentManagement" Version="2.0.0-preview-18312" />
<PackageReference Include="OrchardCore.DisplayManagement" Version="2.0.0-preview-18312" />
<PackageReference Include="OrchardCore.ResourceManagement" Version="2.0.0-preview-18312" />
<PackageReference Include="OrchardCore.Theme.Targets" Version="2.0.0-preview-18315" />
<PackageReference Include="OrchardCore.ContentManagement" Version="2.0.0-preview-18315" />
<PackageReference Include="OrchardCore.DisplayManagement" Version="2.0.0-preview-18315" />
<PackageReference Include="OrchardCore.ResourceManagement" Version="2.0.0-preview-18315" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Lombiq.Hosting.MediaTheme/Lombiq.Hosting.MediaTheme.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="OrchardCore.Theme.Targets" Version="2.0.0-preview-18312" />
<PackageReference Include="OrchardCore.Theme.Targets" Version="2.0.0-preview-18315" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 18ac502

Please sign in to comment.