115 lines
4.1 KiB
C#
115 lines
4.1 KiB
C#
using System.IO;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.FileProviders;
|
|
using xCommons.Extensions;
|
|
using xExceptions.Constants;
|
|
using xStorageService.Constants;
|
|
using xStorageService.Helpers;
|
|
using xStorageService.Interfaces;
|
|
using xStorageService.Models;
|
|
using xStorageService.Providers;
|
|
|
|
namespace xStorageService.DI {
|
|
public static partial class XDIHelperExtension {
|
|
/// <summary>
|
|
/// Retrieve XStorage Configuration from AppSettings
|
|
/// </summary>
|
|
/// <param name="configuration"></param>
|
|
/// <returns></returns>
|
|
public static XStorageConfiguration GetXStorageConfiguration (this IConfiguration configuration) {
|
|
//
|
|
var storageConfigurationSection = configuration.GetSection (ConfigurationNodeNames.STORAGE_SERVICE_NODE_NAME);
|
|
return storageConfigurationSection.Get<XStorageConfiguration> ();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register XStorage Configuration
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="configuration"></param>
|
|
public static void AddXStorageConfiguration (
|
|
this IServiceCollection services,
|
|
IConfiguration configuration
|
|
) {
|
|
//
|
|
// Check wwwroot Folder ...
|
|
if (!Directory.Exists ("wwwroot")) {
|
|
Directory.CreateDirectory ("wwwroot");
|
|
}
|
|
|
|
//
|
|
// Retrieve and Register XStorage Configurations ...
|
|
var xStorageConfiguration = configuration.GetXStorageConfiguration ();
|
|
if (xStorageConfiguration.IsNull ()) {
|
|
XException.InvalidConfiguration.Throw ();
|
|
}
|
|
|
|
//
|
|
xStorageConfiguration.BaseFolder = Path.Combine (Directory.GetCurrentDirectory (), "wwwroot");
|
|
xStorageConfiguration.RootFolder = Directory.GetDirectoryRoot (xStorageConfiguration.BaseFolder);
|
|
|
|
//
|
|
services.AddSingleton<XStorageConfiguration> (xStorageConfiguration);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register XStorage Service
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="config"></param>
|
|
public static void AddXStorageService (
|
|
this IServiceCollection services,
|
|
IConfiguration configuration
|
|
) {
|
|
//
|
|
// Register and Retrieve XStorage Configuration
|
|
services.AddXStorageConfiguration (configuration);
|
|
var xStorageConfiguration = services.GetRegisteredService<XStorageConfiguration> ();
|
|
|
|
//
|
|
// Add File Provider ...
|
|
services.AddSingleton<IFileProvider> (
|
|
new PhysicalFileProvider (xStorageConfiguration.RootFolder)
|
|
);
|
|
|
|
//
|
|
// Register XStorageHelper Service ...
|
|
services.AddSingleton<XStorageHelper> ();
|
|
|
|
//
|
|
// Register ThumbnailHelper ...
|
|
services.AddSingleton<IXThumbnailProvider, XThumbnailProvider> ();
|
|
|
|
//
|
|
// Register Storage Provider ...
|
|
services.AddScoped<IXStorageProvider, XStorageProvider> ();
|
|
|
|
//
|
|
// StorageService Preperation...
|
|
var xStorageService = services.GetRegisteredService<IXStorageProvider> ();
|
|
xStorageService.Prepare ();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Use XStorage Service
|
|
/// </summary>
|
|
/// <param name="app"></param>
|
|
public static void UseXStorageService (this IApplicationBuilder app) {
|
|
//
|
|
// Get Instance of Scope and Service and Prepare Folder Structure ...
|
|
using (var scope = app.ApplicationServices.CreateScope ()) {
|
|
//
|
|
var storageProvider = scope.ServiceProvider.GetService<IXStorageProvider> ();
|
|
|
|
//
|
|
storageProvider.Prepare ();
|
|
}
|
|
|
|
//
|
|
// available Static Files ...
|
|
app.UseStaticFiles ();
|
|
}
|
|
}
|
|
} |