-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Startup.cs
155 lines (129 loc) · 7.71 KB
/
Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OrchardCore.Admin;
using OrchardCore.Environment.Shell;
using OrchardCore.Environment.Shell.Configuration;
using OrchardCore.FileStorage;
using OrchardCore.FileStorage.AzureBlob;
using OrchardCore.Media.Core;
using OrchardCore.Media.Core.Events;
using OrchardCore.Media.Events;
using OrchardCore.Modules;
using OrchardCore.Mvc.Core.Utilities;
using OrchardCore.Navigation;
using OrchardCore.Security.Permissions;
namespace OrchardCore.Media.Azure
{
[Feature("OrchardCore.Media.Azure.Storage")]
public class Startup : Modules.StartupBase
{
private readonly AdminOptions _adminOptions;
private readonly ILogger _logger;
private readonly IShellConfiguration _configuration;
public Startup(IOptions<AdminOptions> adminOptions, ILogger<Startup> logger, IShellConfiguration configuration)
{
_adminOptions = adminOptions.Value;
_logger = logger;
_configuration = configuration;
}
public override void Configure(IApplicationBuilder app, IEndpointRouteBuilder routes, IServiceProvider serviceProvider)
{
routes.MapAreaControllerRoute(
name: "AzureBlob.Options",
areaName: "OrchardCore.Media.Azure",
pattern: _adminOptions.AdminUrlPrefix + "/MediaAzureBlob/Options",
defaults: new { controller = typeof(AdminController).ControllerName(), action = nameof(AdminController.Options) }
);
}
public override int Order => 10;
public override void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IPermissionProvider, Permissions>();
services.AddScoped<INavigationProvider, AdminMenu>();
services.AddTransient<IConfigureOptions<MediaBlobStorageOptions>, MediaBlobStorageOptionsConfiguration>();
// Only replace default implementation if options are valid.
var connectionString = _configuration[$"OrchardCore_Media_Azure:{nameof(MediaBlobStorageOptions.ConnectionString)}"];
var containerName = _configuration[$"OrchardCore_Media_Azure:{nameof(MediaBlobStorageOptions.ContainerName)}"];
if (CheckOptions(connectionString, containerName, _logger))
{
// Register a media cache file provider.
services.AddSingleton<IMediaFileStoreCacheFileProvider>(serviceProvider =>
{
var hostingEnvironment = serviceProvider.GetRequiredService<IWebHostEnvironment>();
if (string.IsNullOrWhiteSpace(hostingEnvironment.WebRootPath))
{
throw new MediaConfigurationException("The wwwroot folder for serving cache media files is missing.");
}
var mediaOptions = serviceProvider.GetRequiredService<IOptions<MediaOptions>>().Value;
var shellOptions = serviceProvider.GetRequiredService<IOptions<ShellOptions>>();
var shellSettings = serviceProvider.GetRequiredService<ShellSettings>();
var logger = serviceProvider.GetRequiredService<ILogger<DefaultMediaFileStoreCacheFileProvider>>();
var mediaCachePath = GetMediaCachePath(
hostingEnvironment, shellSettings, DefaultMediaFileStoreCacheFileProvider.AssetsCachePath);
if (!Directory.Exists(mediaCachePath))
{
Directory.CreateDirectory(mediaCachePath);
}
return new DefaultMediaFileStoreCacheFileProvider(logger, mediaOptions.AssetsRequestPath, mediaCachePath);
});
// Replace the default media file provider with the media cache file provider.
services.Replace(ServiceDescriptor.Singleton<IMediaFileProvider>(serviceProvider =>
serviceProvider.GetRequiredService<IMediaFileStoreCacheFileProvider>()));
// Register the media cache file provider as a file store cache provider.
services.AddSingleton<IMediaFileStoreCache>(serviceProvider =>
serviceProvider.GetRequiredService<IMediaFileStoreCacheFileProvider>());
// Replace the default media file store with a blob file store.
services.Replace(ServiceDescriptor.Singleton<IMediaFileStore>(serviceProvider =>
{
var blobStorageOptions = serviceProvider.GetRequiredService<IOptions<MediaBlobStorageOptions>>().Value;
var shellOptions = serviceProvider.GetRequiredService<IOptions<ShellOptions>>();
var shellSettings = serviceProvider.GetRequiredService<ShellSettings>();
var mediaOptions = serviceProvider.GetRequiredService<IOptions<MediaOptions>>().Value;
var clock = serviceProvider.GetRequiredService<IClock>();
var contentTypeProvider = serviceProvider.GetRequiredService<IContentTypeProvider>();
var mediaEventHandlers = serviceProvider.GetServices<IMediaEventHandler>();
var mediaCreatingEventHandlers = serviceProvider.GetServices<IMediaCreatingEventHandler>();
var logger = serviceProvider.GetRequiredService<ILogger<DefaultMediaFileStore>>();
var fileStore = new BlobFileStore(blobStorageOptions, clock, contentTypeProvider);
var mediaUrlBase = "/" + fileStore.Combine(shellSettings.RequestUrlPrefix, mediaOptions.AssetsRequestPath);
var originalPathBase = serviceProvider.GetRequiredService<IHttpContextAccessor>().HttpContext
?.Features.Get<ShellContextFeature>()
?.OriginalPathBase ?? PathString.Empty;
if (originalPathBase.HasValue)
{
mediaUrlBase = fileStore.Combine(originalPathBase.Value, mediaUrlBase);
}
return new DefaultMediaFileStore(fileStore, mediaUrlBase, mediaOptions.CdnBaseUrl, mediaEventHandlers, mediaCreatingEventHandlers, logger);
}));
services.AddSingleton<IMediaEventHandler, DefaultMediaFileStoreCacheEventHandler>();
services.AddScoped<IModularTenantEvents, MediaBlobContainerTenantEvents>();
}
}
private static string GetMediaCachePath(IWebHostEnvironment hostingEnvironment, ShellSettings shellSettings, string assetsPath)
=> PathExtensions.Combine(hostingEnvironment.WebRootPath, shellSettings.Name, assetsPath);
private static bool CheckOptions(string connectionString, string containerName, ILogger logger)
{
var optionsAreValid = true;
if (string.IsNullOrWhiteSpace(connectionString))
{
logger.LogError("Azure Media Storage is enabled but not active because the 'ConnectionString' is missing or empty in application configuration.");
optionsAreValid = false;
}
if (string.IsNullOrWhiteSpace(containerName))
{
logger.LogError("Azure Media Storage is enabled but not active because the 'ContainerName' is missing or empty in application configuration.");
optionsAreValid = false;
}
return optionsAreValid;
}
}
}