DI: PathUtils

This commit is contained in:
pavelbannov 2019-09-21 19:39:17 +03:00
parent 1e946b19b6
commit 3e0f946372
11 changed files with 55 additions and 38 deletions

View File

@ -38,9 +38,10 @@ namespace ASC.Data.Storage
{ {
public abstract class BaseStorage : IDataStore public abstract class BaseStorage : IDataStore
{ {
public BaseStorage(TenantManager tenantManager) public BaseStorage(TenantManager tenantManager, PathUtils pathUtils)
{ {
TenantManager = tenantManager; TenantManager = tenantManager;
PathUtils = pathUtils;
} }
#region IDataStore Members #region IDataStore Members
@ -200,6 +201,7 @@ namespace ASC.Data.Storage
public virtual bool IsSupportChunking { get { return false; } } public virtual bool IsSupportChunking { get { return false; } }
public TenantManager TenantManager { get; } public TenantManager TenantManager { get; }
public PathUtils PathUtils { get; }
#endregion #endregion

View File

@ -90,11 +90,13 @@ namespace ASC.Data.Storage.Configuration
SettingsManager settingsManager, SettingsManager settingsManager,
TenantManager tenantManager, TenantManager tenantManager,
BaseStorageSettingsListener baseStorageSettingsListener, BaseStorageSettingsListener baseStorageSettingsListener,
StorageFactoryConfig storageFactoryConfig) : StorageFactoryConfig storageFactoryConfig,
PathUtils pathUtils) :
base(authContext, settingsManager, tenantManager) base(authContext, settingsManager, tenantManager)
{ {
BaseStorageSettingsListener = baseStorageSettingsListener; BaseStorageSettingsListener = baseStorageSettingsListener;
StorageFactoryConfig = storageFactoryConfig; StorageFactoryConfig = storageFactoryConfig;
PathUtils = pathUtils;
} }
public override ISettings GetDefault() public override ISettings GetDefault()
@ -158,7 +160,7 @@ namespace ASC.Data.Storage.Configuration
if (DataStoreConsumer.HandlerType == null) return null; if (DataStoreConsumer.HandlerType == null) return null;
return dataStore = ((IDataStore) return dataStore = ((IDataStore)
Activator.CreateInstance(DataStoreConsumer.HandlerType, TenantManager)) Activator.CreateInstance(DataStoreConsumer.HandlerType, TenantManager, PathUtils))
.Configure(TenantManager.GetCurrentTenant().TenantId.ToString(), null, null, DataStoreConsumer); .Configure(TenantManager.GetCurrentTenant().TenantId.ToString(), null, null, DataStoreConsumer);
} }
} }
@ -167,6 +169,7 @@ namespace ASC.Data.Storage.Configuration
public BaseStorageSettingsListener BaseStorageSettingsListener { get; } public BaseStorageSettingsListener BaseStorageSettingsListener { get; }
public StorageFactoryConfig StorageFactoryConfig { get; } public StorageFactoryConfig StorageFactoryConfig { get; }
public PathUtils PathUtils { get; }
private static readonly ICacheNotify<DataStoreCacheItem> Cache; private static readonly ICacheNotify<DataStoreCacheItem> Cache;
} }
@ -185,8 +188,9 @@ namespace ASC.Data.Storage.Configuration
SettingsManager settingsManager, SettingsManager settingsManager,
TenantManager tenantManager, TenantManager tenantManager,
BaseStorageSettingsListener baseStorageSettingsListener, BaseStorageSettingsListener baseStorageSettingsListener,
StorageFactoryConfig storageFactoryConfig) : StorageFactoryConfig storageFactoryConfig,
base(authContext, settingsManager, tenantManager, baseStorageSettingsListener, storageFactoryConfig) PathUtils pathUtils) :
base(authContext, settingsManager, tenantManager, baseStorageSettingsListener, storageFactoryConfig, pathUtils)
{ {
} }
@ -210,8 +214,9 @@ namespace ASC.Data.Storage.Configuration
SettingsManager settingsManager, SettingsManager settingsManager,
TenantManager tenantManager, TenantManager tenantManager,
BaseStorageSettingsListener baseStorageSettingsListener, BaseStorageSettingsListener baseStorageSettingsListener,
StorageFactoryConfig storageFactoryConfig) : StorageFactoryConfig storageFactoryConfig,
base(authContext, settingsManager, tenantManager, baseStorageSettingsListener, storageFactoryConfig) PathUtils pathUtils) :
base(authContext, settingsManager, tenantManager, baseStorageSettingsListener, storageFactoryConfig, pathUtils)
{ {
} }

View File

@ -49,10 +49,10 @@ namespace ASC.Data.Storage.DiscStorage
_dataList = new DataList(moduleConfig); _dataList = new DataList(moduleConfig);
foreach (var domain in moduleConfig.Domain) foreach (var domain in moduleConfig.Domain)
{ {
_mappedPaths.Add(domain.Name, new MappedPath(tenant, moduleConfig.AppendTenantId, domain.Path, handlerConfig.GetProperties())); _mappedPaths.Add(domain.Name, new MappedPath(PathUtils, tenant, moduleConfig.AppendTenantId, domain.Path, handlerConfig.GetProperties()));
} }
//Add default //Add default
_mappedPaths.Add(string.Empty, new MappedPath(tenant, moduleConfig.AppendTenantId, PathUtils.Normalize(moduleConfig.Path), handlerConfig.GetProperties())); _mappedPaths.Add(string.Empty, new MappedPath(PathUtils, tenant, moduleConfig.AppendTenantId, PathUtils.Normalize(moduleConfig.Path), handlerConfig.GetProperties()));
//Make expires //Make expires
_domainsExpires = _domainsExpires =
@ -63,7 +63,7 @@ namespace ASC.Data.Storage.DiscStorage
return this; return this;
} }
public DiscDataStore(TenantManager tenantManager) : base(tenantManager) public DiscDataStore(TenantManager tenantManager, PathUtils pathUtils) : base(tenantManager, pathUtils)
{ {
} }

View File

@ -31,14 +31,15 @@ namespace ASC.Data.Storage.DiscStorage
{ {
internal class MappedPath internal class MappedPath
{ {
public string PhysicalPath { get; set; } public string PhysicalPath { get; set; }
public PathUtils PathUtils { get; }
private MappedPath(PathUtils pathUtils)
private MappedPath() {
{ PathUtils = pathUtils;
} }
public MappedPath(string tenant, bool appendTenant, string ppath, IDictionary<string, string> storageConfig) public MappedPath(PathUtils pathUtils, string tenant, bool appendTenant, string ppath, IDictionary<string, string> storageConfig) : this(pathUtils)
{ {
tenant = tenant.Trim('/'); tenant = tenant.Trim('/');
@ -49,7 +50,7 @@ namespace ASC.Data.Storage.DiscStorage
public MappedPath AppendDomain(string domain) public MappedPath AppendDomain(string domain)
{ {
domain = domain.Replace('.', '_'); //Domain prep. Remove dots domain = domain.Replace('.', '_'); //Domain prep. Remove dots
return new MappedPath return new MappedPath(PathUtils)
{ {
PhysicalPath = Path.Combine(PhysicalPath, PathUtils.Normalize(domain, true)), PhysicalPath = Path.Combine(PhysicalPath, PathUtils.Normalize(domain, true)),
}; };

View File

@ -58,7 +58,7 @@ namespace ASC.Data.Storage.GoogleCloud
private bool _lowerCasing = true; private bool _lowerCasing = true;
public GoogleCloudStorage(TenantManager tenantManager) : base(tenantManager) public GoogleCloudStorage(TenantManager tenantManager, PathUtils pathUtils) : base(tenantManager, pathUtils)
{ {
} }

View File

@ -30,15 +30,21 @@ using System.IO;
using ASC.Common.DependencyInjection; using ASC.Common.DependencyInjection;
using ASC.Common.Utils; using ASC.Common.Utils;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
namespace ASC.Data.Storage namespace ASC.Data.Storage
{ {
class PathUtils public class PathUtils
{ {
private static string StorageRoot { get; } private string StorageRoot { get; }
static PathUtils() public IConfiguration Configuration { get; }
public IWebHostEnvironment WebHostEnvironment { get; }
public PathUtils(IConfiguration configuration, IWebHostEnvironment webHostEnvironment)
{ {
StorageRoot = ConfigurationManager.AppSettings[Constants.STORAGE_ROOT_PARAM]; Configuration = configuration;
WebHostEnvironment = webHostEnvironment;
StorageRoot = Configuration[Constants.STORAGE_ROOT_PARAM];
} }
public static string Normalize(string path, bool addTailingSeparator = false) public static string Normalize(string path, bool addTailingSeparator = false)
@ -52,7 +58,7 @@ namespace ASC.Data.Storage
return addTailingSeparator && 0 < path.Length ? path + Path.DirectorySeparatorChar : path; return addTailingSeparator && 0 < path.Length ? path + Path.DirectorySeparatorChar : path;
} }
public static string ResolveVirtualPath(string module, string domain) public string ResolveVirtualPath(string module, string domain)
{ {
var url = string.Format("~/storage/{0}/{1}/", var url = string.Format("~/storage/{0}/{1}/",
module, module,
@ -60,21 +66,19 @@ namespace ASC.Data.Storage
return ResolveVirtualPath(url); return ResolveVirtualPath(url);
} }
//TODO public string ResolveVirtualPath(string virtPath, bool addTrailingSlash = true)
public static string ResolveVirtualPath(string virtPath, bool addTrailingSlash = true)
{ {
if (virtPath == null) if (virtPath == null)
{ {
virtPath = ""; virtPath = "";
} }
var webHostEnvironment = CommonServiceProvider.GetService<IWebHostEnvironment>();
if (virtPath.StartsWith("~") && !Uri.IsWellFormedUriString(virtPath, UriKind.Absolute)) if (virtPath.StartsWith("~") && !Uri.IsWellFormedUriString(virtPath, UriKind.Absolute))
{ {
var rootPath = "/"; var rootPath = "/";
if (!string.IsNullOrEmpty(webHostEnvironment.WebRootPath) && webHostEnvironment.WebRootPath.Length > 1) if (!string.IsNullOrEmpty(WebHostEnvironment.WebRootPath) && WebHostEnvironment.WebRootPath.Length > 1)
{ {
rootPath = webHostEnvironment.WebRootPath.Trim('/'); rootPath = WebHostEnvironment.WebRootPath.Trim('/');
} }
virtPath = virtPath.Replace("~", rootPath); virtPath = virtPath.Replace("~", rootPath);
} }
@ -89,12 +93,10 @@ namespace ASC.Data.Storage
return virtPath.Replace("//", "/"); return virtPath.Replace("//", "/");
} }
public static string ResolvePhysicalPath(string physPath, IDictionary<string, string> storageConfig) public string ResolvePhysicalPath(string physPath, IDictionary<string, string> storageConfig)
{ {
physPath = Normalize(physPath, false).TrimStart('~'); physPath = Normalize(physPath, false).TrimStart('~');
var webHostEnvironment = CommonServiceProvider.GetService<IWebHostEnvironment>();
if (physPath.Contains(Constants.STORAGE_ROOT_PARAM)) if (physPath.Contains(Constants.STORAGE_ROOT_PARAM))
{ {
physPath = physPath.Replace(Constants.STORAGE_ROOT_PARAM, StorageRoot ?? storageConfig[Constants.STORAGE_ROOT_PARAM]); physPath = physPath.Replace(Constants.STORAGE_ROOT_PARAM, StorageRoot ?? storageConfig[Constants.STORAGE_ROOT_PARAM]);
@ -102,7 +104,7 @@ namespace ASC.Data.Storage
if (!Path.IsPathRooted(physPath)) if (!Path.IsPathRooted(physPath))
{ {
physPath = Path.GetFullPath(Path.Combine(webHostEnvironment.ContentRootPath, physPath.Trim(Path.DirectorySeparatorChar))); physPath = Path.GetFullPath(Path.Combine(WebHostEnvironment.ContentRootPath, physPath.Trim(Path.DirectorySeparatorChar)));
} }
return physPath; return physPath;
} }

View File

@ -55,7 +55,7 @@ namespace ASC.Data.Storage.RackspaceCloud
private static readonly ILog _logger = LogManager.GetLogger("ASC.Data.Storage.Rackspace.RackspaceCloudStorage"); private static readonly ILog _logger = LogManager.GetLogger("ASC.Data.Storage.Rackspace.RackspaceCloudStorage");
public RackspaceCloudStorage(TenantManager tenantManager) : base(tenantManager) public RackspaceCloudStorage(TenantManager tenantManager, PathUtils pathUtils) : base(tenantManager, pathUtils)
{ {
} }

View File

@ -63,7 +63,7 @@ namespace ASC.Data.Storage.S3
private string _distributionId = string.Empty; private string _distributionId = string.Empty;
private string _subDir = string.Empty; private string _subDir = string.Empty;
public S3Storage(TenantManager tenantManager) : base(tenantManager) public S3Storage(TenantManager tenantManager, PathUtils pathUtils) : base(tenantManager, pathUtils)
{ {
} }

View File

@ -32,7 +32,9 @@ using ASC.Core;
using ASC.Core.Common.Configuration; using ASC.Core.Common.Configuration;
using ASC.Data.Storage.Configuration; using ASC.Data.Storage.Configuration;
using ASC.Data.Storage.DiscStorage; using ASC.Data.Storage.DiscStorage;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
namespace ASC.Data.Storage namespace ASC.Data.Storage
@ -90,6 +92,7 @@ namespace ASC.Data.Storage
//} //}
var section = builder.ServiceProvider.GetService<Configuration.Storage>(); var section = builder.ServiceProvider.GetService<Configuration.Storage>();
var pathUtils = builder.ServiceProvider.GetService<PathUtils>();
if (section != null) if (section != null)
{ {
//old scheme //old scheme
@ -101,8 +104,8 @@ namespace ASC.Data.Storage
{ {
if (m.Path.Contains(Constants.STORAGE_ROOT_PARAM)) if (m.Path.Contains(Constants.STORAGE_ROOT_PARAM))
builder.RegisterDiscDataHandler( builder.RegisterDiscDataHandler(
PathUtils.ResolveVirtualPath(m.VirtualPath), pathUtils.ResolveVirtualPath(m.VirtualPath),
PathUtils.ResolvePhysicalPath(m.Path, props), pathUtils.ResolvePhysicalPath(m.Path, props),
m.Public); m.Public);
if (m.Domain != null) if (m.Domain != null)
@ -110,8 +113,8 @@ namespace ASC.Data.Storage
foreach (var d in m.Domain.Where(d => (d.Type == "disc" || string.IsNullOrEmpty(d.Type)) && d.Path.Contains(Constants.STORAGE_ROOT_PARAM))) foreach (var d in m.Domain.Where(d => (d.Type == "disc" || string.IsNullOrEmpty(d.Type)) && d.Path.Contains(Constants.STORAGE_ROOT_PARAM)))
{ {
builder.RegisterDiscDataHandler( builder.RegisterDiscDataHandler(
PathUtils.ResolveVirtualPath(d.VirtualPath), pathUtils.ResolveVirtualPath(d.VirtualPath),
PathUtils.ResolvePhysicalPath(d.Path, props)); pathUtils.ResolvePhysicalPath(d.Path, props));
} }
} }
} }

View File

@ -35,8 +35,10 @@ using ASC.Common.Web;
using ASC.Core; using ASC.Core;
using ASC.Security.Cryptography; using ASC.Security.Cryptography;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
namespace ASC.Data.Storage.DiscStorage namespace ASC.Data.Storage.DiscStorage
@ -150,7 +152,8 @@ namespace ASC.Data.Storage.DiscStorage
{ {
public static IEndpointRouteBuilder RegisterStorageHandler(this IEndpointRouteBuilder builder, string module, string domain, bool publicRoute = false) public static IEndpointRouteBuilder RegisterStorageHandler(this IEndpointRouteBuilder builder, string module, string domain, bool publicRoute = false)
{ {
var virtPath = PathUtils.ResolveVirtualPath(module, domain); var pathUtils = new PathUtils(builder.ServiceProvider.GetService<IConfiguration>(), builder.ServiceProvider.GetService<IWebHostEnvironment>());
var virtPath = pathUtils.ResolveVirtualPath(module, domain);
virtPath = virtPath.TrimStart('/'); virtPath = virtPath.TrimStart('/');
var handler = new StorageHandler(builder.ServiceProvider, string.Empty, module, domain, !publicRoute); var handler = new StorageHandler(builder.ServiceProvider, string.Empty, module, domain, !publicRoute);

View File

@ -220,6 +220,7 @@ namespace ASC.People
.AddSingleton<CoreBaseSettings>() .AddSingleton<CoreBaseSettings>()
.AddScoped<SubscriptionManager>() .AddScoped<SubscriptionManager>()
.AddScoped<IPSecurity.IPSecurity>() .AddScoped<IPSecurity.IPSecurity>()
.AddSingleton<PathUtils>()
.AddScoped(typeof(IRecipientProvider), typeof(RecipientProviderImpl)) .AddScoped(typeof(IRecipientProvider), typeof(RecipientProviderImpl))
.AddSingleton(typeof(IRoleProvider), typeof(RoleProvider)) .AddSingleton(typeof(IRoleProvider), typeof(RoleProvider))
.AddScoped(typeof(IPermissionResolver), typeof(PermissionResolver)) .AddScoped(typeof(IPermissionResolver), typeof(PermissionResolver))