Skip to content

Add provider-independent middleware and CDN media URL provider #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Umbraco.StorageProviders.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
README.md = README.md
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Umbraco.StorageProviders", "src\Umbraco.StorageProviders\Umbraco.StorageProviders.csproj", "{5EC38982-2C9A-4D8D-AAE2-743A690FCD71}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -37,6 +39,18 @@ Global
{99A3FCBE-FDC6-4580-BDB8-7D219C6D98C3}.Release|x64.Build.0 = Release|Any CPU
{99A3FCBE-FDC6-4580-BDB8-7D219C6D98C3}.Release|x86.ActiveCfg = Release|Any CPU
{99A3FCBE-FDC6-4580-BDB8-7D219C6D98C3}.Release|x86.Build.0 = Release|Any CPU
{5EC38982-2C9A-4D8D-AAE2-743A690FCD71}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5EC38982-2C9A-4D8D-AAE2-743A690FCD71}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5EC38982-2C9A-4D8D-AAE2-743A690FCD71}.Debug|x64.ActiveCfg = Debug|Any CPU
{5EC38982-2C9A-4D8D-AAE2-743A690FCD71}.Debug|x64.Build.0 = Debug|Any CPU
{5EC38982-2C9A-4D8D-AAE2-743A690FCD71}.Debug|x86.ActiveCfg = Debug|Any CPU
{5EC38982-2C9A-4D8D-AAE2-743A690FCD71}.Debug|x86.Build.0 = Debug|Any CPU
{5EC38982-2C9A-4D8D-AAE2-743A690FCD71}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5EC38982-2C9A-4D8D-AAE2-743A690FCD71}.Release|Any CPU.Build.0 = Release|Any CPU
{5EC38982-2C9A-4D8D-AAE2-743A690FCD71}.Release|x64.ActiveCfg = Release|Any CPU
{5EC38982-2C9A-4D8D-AAE2-743A690FCD71}.Release|x64.Build.0 = Release|Any CPU
{5EC38982-2C9A-4D8D-AAE2-743A690FCD71}.Release|x86.ActiveCfg = Release|Any CPU
{5EC38982-2C9A-4D8D-AAE2-743A690FCD71}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Umbraco.StorageProviders;
using Umbraco.StorageProviders.AzureBlob.IO;

// ReSharper disable once CheckNamespace
// uses same namespace as Umbraco Core for easier discoverability
namespace Umbraco.Cms.Core.DependencyInjection
{
/// <summary>
/// Extension methods to help registering a CDN media URL provider.
/// </summary>
public static class AzureBlobCdnMediaUrlProviderExtensions
{
/// <summary>
/// Registers and configures the <see cref="CdnMediaUrlProvider" />.
/// </summary>
/// <param name="builder">The <see cref="IUmbracoBuilder" />.</param>
/// <returns>
/// The <see cref="IUmbracoBuilder" />.
/// </returns>
/// <exception cref="System.ArgumentNullException">builder</exception>
public static IUmbracoBuilder AddAzureBlobCdnMediaUrlProvider(this IUmbracoBuilder builder)
{
if (builder == null) throw new ArgumentNullException(nameof(builder));

builder.AddCdnMediaUrlProvider();

builder.Services.AddOptions<CdnMediaUrlProviderOptions>()
.BindConfiguration("Umbraco:Storage:AzureBlob:Media:Cdn")
.Configure<IOptionsFactory<AzureBlobFileSystemOptions>>(
(options, factory) =>
{
var mediaOptions = factory.Create(AzureBlobFileSystemOptions.MediaFileSystemName);
if (!string.IsNullOrEmpty(mediaOptions.ContainerName))
{
options.Url = new Uri(options.Url, mediaOptions.ContainerName);
}
}
)
.ValidateDataAnnotations();

return builder;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.StaticFiles.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using Microsoft.Net.Http.Headers;
using SixLabors.ImageSharp.Web.Caching;
using SixLabors.ImageSharp.Web.Providers;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Hosting;
using Umbraco.Cms.Infrastructure.DependencyInjection;
using Umbraco.Cms.Web.Common.ApplicationBuilder;
using Umbraco.Extensions;
using Umbraco.StorageProviders.AzureBlob;
using Umbraco.StorageProviders.AzureBlob.Imaging;
using Umbraco.StorageProviders.AzureBlob.IO;
using Umbraco.StorageProviders.IO;

// ReSharper disable once CheckNamespace
// uses same namespace as Umbraco Core for easier discoverability
Expand Down Expand Up @@ -41,8 +44,6 @@ public static IUmbracoBuilder AddAzureBlobMediaFileSystem(this IUmbracoBuilder b
options.VirtualPath = globalSettingsOptions.Value.UmbracoMediaPath;
});

builder.Services.TryAddSingleton<AzureBlobFileSystemMiddleware>();

// ImageSharp image provider/cache
builder.Services.Insert(0, ServiceDescriptor.Singleton<IImageProvider, AzureBlobFileSystemImageProvider>());
builder.Services.AddUnique<IImageCache, AzureBlobFileSystemImageCache>();
Expand Down Expand Up @@ -104,7 +105,7 @@ public static IUmbracoBuilder AddAzureBlobMediaFileSystem(this IUmbracoBuilder b
}

/// <summary>
/// Adds the <see cref="AzureBlobFileSystemMiddleware" />.
/// Adds the <see cref="Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware" />.
/// </summary>
/// <param name="builder">The <see cref="IUmbracoApplicationBuilderContext" />.</param>
/// <returns>
Expand All @@ -121,7 +122,7 @@ public static IUmbracoApplicationBuilderContext UseAzureBlobMediaFileSystem(this
}

/// <summary>
/// Adds the <see cref="AzureBlobFileSystemMiddleware" />.
/// Adds the <see cref="Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware" />.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder" />.</param>
/// <returns>
Expand All @@ -132,7 +133,32 @@ public static IApplicationBuilder UseAzureBlobMediaFileSystem(this IApplicationB
{
if (app == null) throw new ArgumentNullException(nameof(app));

app.UseMiddleware<AzureBlobFileSystemMiddleware>();
var fileSystem = app.ApplicationServices.GetRequiredService<IAzureBlobFileSystemProvider>().GetFileSystem(AzureBlobFileSystemOptions.MediaFileSystemName);
var options = app.ApplicationServices.GetRequiredService<IOptionsFactory<AzureBlobFileSystemOptions>>().Create(AzureBlobFileSystemOptions.MediaFileSystemName);
var hostingEnvironment = app.ApplicationServices.GetRequiredService<IHostingEnvironment>();

var requestPath = hostingEnvironment.ToAbsolute(options.VirtualPath);

var sharedOptions = new SharedOptions()
{
FileProvider = new FileSystemFileProvider(fileSystem, requestPath),
RequestPath = requestPath
};

app.UseStaticFiles(new StaticFileOptions(sharedOptions)
{
OnPrepareResponse = ctx =>
{
// TODO Make this configurable
var headers = ctx.Context.Response.GetTypedHeaders();
headers.CacheControl = new CacheControlHeaderValue
{
Public = true,
MustRevalidate = true,
MaxAge = TimeSpan.FromDays(7)
};
}
});

return app;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
<Nullable>enable</Nullable>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<description>Azure blob storage plugin for Umbraco CMS</description>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Storage.Blobs" Version="12.10.0" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="5.0.3" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.0.0" PrivateAssets="all" />
<PackageReference Include="SixLabors.ImageSharp.Web.Providers.Azure" Version="1.0.3" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.27.0.35380" PrivateAssets="all" />
<PackageReference Include="Umbraco.Cms.Web.Common" Version="9.0.0" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.33.0.40503" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Umbraco.StorageProviders\Umbraco.StorageProviders.csproj" />
</ItemGroup>
</Project>
7 changes: 0 additions & 7 deletions src/Umbraco.StorageProviders.AzureBlob/version.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Cms.Core.Routing;

namespace Umbraco.StorageProviders.AzureBlob
namespace Umbraco.StorageProviders
{
/// <summary>
/// A <see cref="IMediaUrlProvider" /> that returns a CDN URL for a media item.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.ComponentModel.DataAnnotations;

namespace Umbraco.StorageProviders.AzureBlob
namespace Umbraco.StorageProviders
{
/// <summary>
/// The CDN media URL provider options.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Umbraco.StorageProviders.AzureBlob;
using Umbraco.StorageProviders.AzureBlob.IO;
using Umbraco.StorageProviders;

// ReSharper disable once CheckNamespace
// uses same namespace as Umbraco Core for easier discoverability
Expand All @@ -25,22 +23,12 @@ public static IUmbracoBuilder AddCdnMediaUrlProvider(this IUmbracoBuilder builde
{
if (builder == null) throw new ArgumentNullException(nameof(builder));

builder.MediaUrlProviders().Insert<CdnMediaUrlProvider>();

builder.Services.AddOptions<CdnMediaUrlProviderOptions>()
.BindConfiguration("Umbraco:Storage:AzureBlob:Media:Cdn")
.Configure<IOptionsFactory<AzureBlobFileSystemOptions>>(
(options, factory) =>
{
var mediaOptions = factory.Create(AzureBlobFileSystemOptions.MediaFileSystemName);
if (!string.IsNullOrEmpty(mediaOptions.ContainerName))
{
options.Url = new Uri(options.Url, mediaOptions.ContainerName);
}
}
)
.BindConfiguration("Umbraco:Storage:Media:Cdn")
.ValidateDataAnnotations();

builder.MediaUrlProviders().Insert<CdnMediaUrlProvider>();

return builder;
}

Expand Down
60 changes: 60 additions & 0 deletions src/Umbraco.StorageProviders/IO/FileSystemDirectoryContents.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.FileProviders;
using Umbraco.Cms.Core.IO;

namespace Umbraco.StorageProviders.IO
{
/// <summary>
/// Represents the directory contents in an <see cref="IFileSystem" />.
/// </summary>
/// <seealso cref="Microsoft.Extensions.FileProviders.IDirectoryContents" />
public class FileSystemDirectoryContents : IDirectoryContents
{
private readonly IFileSystem _fileSystem;
private readonly string _subpath;
private IEnumerable<IFileInfo> _entries = null!;

/// <inheritdoc />
public bool Exists => true;

/// <summary>
/// Initializes a new instance of the <see cref="FileSystemDirectoryContents"/> class.
/// </summary>
/// <param name="fileSystem">The file system.</param>
/// <param name="subpath">The subpath.</param>
/// <exception cref="System.ArgumentNullException">
/// fileSystem
/// or
/// subpath
/// </exception>
public FileSystemDirectoryContents(IFileSystem fileSystem, string subpath)
{
_fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
_subpath = subpath ?? throw new ArgumentNullException(nameof(subpath));
}

/// <inheritdoc />
public IEnumerator<IFileInfo> GetEnumerator()
{
EnsureInitialized();
return _entries.GetEnumerator();
}

/// <inheritdoc />
IEnumerator IEnumerable.GetEnumerator()
{
EnsureInitialized();
return _entries.GetEnumerator();
}

private void EnsureInitialized()
{
_entries = _fileSystem.GetDirectories(_subpath).Select<string, IFileInfo>(d => new FileSystemDirectoryInfo(_fileSystem, d))
.Union(_fileSystem.GetFiles(_subpath).Select<string, IFileInfo>(f => new FileSystemFileInfo(_fileSystem, f)))
.ToList();
}
}
}
49 changes: 49 additions & 0 deletions src/Umbraco.StorageProviders/IO/FileSystemDirectoryInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.IO;
using Microsoft.Extensions.FileProviders;
using Umbraco.Cms.Core.IO;

namespace Umbraco.StorageProviders.IO
{
/// <summary>
/// Represents a directory in an <see cref="IFileSystem" />.
/// </summary>
/// <seealso cref="Microsoft.Extensions.FileProviders.IFileInfo" />
public class FileSystemDirectoryInfo : IFileInfo
{
private readonly IFileSystem _fileSystem;
private readonly string _subpath;

/// <inheritdoc />
public bool Exists => true;

/// <inheritdoc />
public bool IsDirectory => true;

/// <inheritdoc />
public DateTimeOffset LastModified => _fileSystem.GetLastModified(_subpath);

/// <inheritdoc />
public long Length => -1;

/// <inheritdoc />
public string Name => _fileSystem.GetRelativePath(_subpath);

/// <inheritdoc />
public string PhysicalPath => null!;

/// <summary>
/// Initializes a new instance of the <see cref="FileSystemDirectoryInfo" /> class.
/// </summary>
/// <param name="fileSystem">The file system.</param>
/// <param name="subpath">The subpath.</param>
public FileSystemDirectoryInfo(IFileSystem fileSystem, string subpath)
{
_fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
_subpath = subpath ?? throw new ArgumentNullException(nameof(subpath));
}

/// <inheritdoc />
public Stream CreateReadStream() => throw new InvalidOperationException("Cannot create a stream for a directory.");
}
}
Loading