DocSpace-client/common/ASC.Data.Storage/StorageFactory.cs

340 lines
14 KiB
C#
Raw Normal View History

2019-06-04 14:43:20 +00:00
/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
*
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
2020-01-21 12:44:05 +00:00
2020-02-17 08:58:14 +00:00
using ASC.Common;
2019-06-04 14:43:20 +00:00
using ASC.Common.Caching;
2019-10-17 15:55:35 +00:00
using ASC.Common.Logging;
2019-06-04 14:43:20 +00:00
using ASC.Core;
using ASC.Core.Common.Configuration;
using ASC.Core.Common.Settings;
2019-08-15 12:04:42 +00:00
using ASC.Data.Storage.Configuration;
2019-06-04 14:43:20 +00:00
using ASC.Data.Storage.DiscStorage;
2019-09-23 12:20:08 +00:00
using ASC.Security.Cryptography;
2020-01-21 12:44:05 +00:00
2019-10-18 08:48:27 +00:00
using Microsoft.AspNetCore.Http;
2019-06-04 14:43:20 +00:00
using Microsoft.AspNetCore.Routing;
2019-07-09 10:29:53 +00:00
using Microsoft.Extensions.DependencyInjection;
2019-10-17 15:55:35 +00:00
using Microsoft.Extensions.Options;
2019-06-04 14:43:20 +00:00
namespace ASC.Data.Storage
{
2019-09-13 11:18:27 +00:00
public class StorageFactoryListener
2019-06-04 14:43:20 +00:00
{
2019-10-11 15:03:03 +00:00
public StorageFactoryListener(ICacheNotify<DataStoreCacheItem> cache)
2019-06-04 14:43:20 +00:00
{
2019-10-11 15:03:03 +00:00
cache.Subscribe((r) => DataStoreCache.Remove(r.TenantId, r.Module), CacheNotifyAction.Remove);
2019-06-04 14:43:20 +00:00
}
2019-09-13 11:18:27 +00:00
}
2019-06-04 14:43:20 +00:00
2019-09-13 11:18:27 +00:00
public class StorageFactoryConfig
{
public Configuration.Storage Section { get; }
2019-06-04 14:43:20 +00:00
2019-09-13 11:18:27 +00:00
public StorageFactoryConfig(IServiceProvider serviceProvider)
2019-06-04 14:43:20 +00:00
{
2019-09-13 11:18:27 +00:00
Section = serviceProvider.GetService<Configuration.Storage>();
2019-06-04 14:43:20 +00:00
}
2019-09-13 11:18:27 +00:00
public IEnumerable<string> GetModuleList(string configpath, bool exceptDisabledMigration = false)
2019-06-04 14:43:20 +00:00
{
2019-09-13 11:18:27 +00:00
return Section.Module
2019-06-04 14:43:20 +00:00
.Where(x => x.Visible)
2019-08-15 12:04:42 +00:00
.Where(x => !exceptDisabledMigration || !x.DisableMigrate)
2019-06-04 14:43:20 +00:00
.Select(x => x.Name);
}
2019-09-13 11:18:27 +00:00
public IEnumerable<string> GetDomainList(string configpath, string modulename)
2019-06-04 14:43:20 +00:00
{
2019-09-13 11:18:27 +00:00
if (Section == null)
2019-06-04 14:43:20 +00:00
{
throw new ArgumentException("config section not found");
}
return
2019-09-13 11:18:27 +00:00
Section.Module
2019-06-04 14:43:20 +00:00
.Single(x => x.Name.Equals(modulename, StringComparison.OrdinalIgnoreCase))
.Domain
.Where(x => x.Visible)
.Select(x => x.Name);
}
2019-09-13 11:18:27 +00:00
}
2019-06-04 14:43:20 +00:00
2019-09-13 11:18:27 +00:00
public static class StorageFactoryExtenstion
{
2019-06-04 14:43:20 +00:00
public static void InitializeHttpHandlers(this IEndpointRouteBuilder builder, string config = null)
{
//TODO:
//if (!HostingEnvironment.IsHosted)
//{
// throw new InvalidOperationException("Application not hosted.");
//}
2019-08-15 12:04:42 +00:00
2019-07-09 10:29:53 +00:00
var section = builder.ServiceProvider.GetService<Configuration.Storage>();
2019-09-21 16:39:17 +00:00
var pathUtils = builder.ServiceProvider.GetService<PathUtils>();
2019-06-04 14:43:20 +00:00
if (section != null)
{
//old scheme
var discHandler = section.GetHandler("disc");
if (discHandler != null && section.Module != null)
{
var props = discHandler.Property != null ? discHandler.Property.ToDictionary(r => r.Name, r => r.Value) : new Dictionary<string, string>();
foreach (var m in section.Module.Where(m => m.Type == "disc"))
{
if (m.Path.Contains(Constants.STORAGE_ROOT_PARAM))
builder.RegisterDiscDataHandler(
2019-09-21 16:39:17 +00:00
pathUtils.ResolveVirtualPath(m.VirtualPath),
pathUtils.ResolvePhysicalPath(m.Path, props),
2019-06-04 14:43:20 +00:00
m.Public);
if (m.Domain != null)
{
foreach (var d in m.Domain.Where(d => (d.Type == "disc" || string.IsNullOrEmpty(d.Type)) && d.Path.Contains(Constants.STORAGE_ROOT_PARAM)))
{
builder.RegisterDiscDataHandler(
2019-09-21 16:39:17 +00:00
pathUtils.ResolveVirtualPath(d.VirtualPath),
pathUtils.ResolvePhysicalPath(d.Path, props));
2019-06-04 14:43:20 +00:00
}
}
}
}
//new scheme
if (section.Module != null)
{
foreach (var m in section.Module)
{
//todo: add path criterion
if (m.Type == "disc" || !m.Public || m.Path.Contains(Constants.STORAGE_ROOT_PARAM))
builder.RegisterStorageHandler(
m.Name,
string.Empty,
m.Public);
//todo: add path criterion
if (m.Domain != null)
{
foreach (var d in m.Domain.Where(d => d.Path.Contains(Constants.STORAGE_ROOT_PARAM)))
{
builder.RegisterStorageHandler(
m.Name,
d.Name,
d.Public);
}
}
}
}
}
}
2019-09-13 11:18:27 +00:00
}
public class StorageFactory
{
private const string DefaultTenantName = "default";
2020-08-12 09:58:08 +00:00
private StorageFactoryListener StorageFactoryListener { get; }
private StorageFactoryConfig StorageFactoryConfig { get; }
private SettingsManager SettingsManager { get; }
private StorageSettingsHelper StorageSettingsHelper { get; }
private TenantManager TenantManager { get; }
private CoreBaseSettings CoreBaseSettings { get; }
private PathUtils PathUtils { get; }
private EmailValidationKeyProvider EmailValidationKeyProvider { get; }
private IOptionsMonitor<ILog> Options { get; }
private IHttpContextAccessor HttpContextAccessor { get; }
2019-09-13 11:18:27 +00:00
2019-09-18 08:25:57 +00:00
public StorageFactory(
2019-10-11 15:03:03 +00:00
StorageFactoryListener storageFactoryListener,
StorageFactoryConfig storageFactoryConfig,
SettingsManager settingsManager,
StorageSettingsHelper storageSettingsHelper,
2019-09-19 11:34:54 +00:00
TenantManager tenantManager,
2019-09-23 12:20:08 +00:00
CoreBaseSettings coreBaseSettings,
PathUtils pathUtils,
2019-10-17 15:55:35 +00:00
EmailValidationKeyProvider emailValidationKeyProvider,
2019-11-06 15:03:09 +00:00
IOptionsMonitor<ILog> options) :
this(storageFactoryListener, storageFactoryConfig, settingsManager, storageSettingsHelper, tenantManager, coreBaseSettings, pathUtils, emailValidationKeyProvider, options, null)
2019-10-18 08:48:27 +00:00
{
}
public StorageFactory(
StorageFactoryListener storageFactoryListener,
StorageFactoryConfig storageFactoryConfig,
SettingsManager settingsManager,
StorageSettingsHelper storageSettingsHelper,
2019-10-18 08:48:27 +00:00
TenantManager tenantManager,
CoreBaseSettings coreBaseSettings,
PathUtils pathUtils,
EmailValidationKeyProvider emailValidationKeyProvider,
2019-11-06 15:03:09 +00:00
IOptionsMonitor<ILog> options,
2019-10-18 08:48:27 +00:00
IHttpContextAccessor httpContextAccessor)
2019-09-13 11:18:27 +00:00
{
StorageFactoryListener = storageFactoryListener;
StorageFactoryConfig = storageFactoryConfig;
SettingsManager = settingsManager;
StorageSettingsHelper = storageSettingsHelper;
2019-09-18 08:25:57 +00:00
TenantManager = tenantManager;
2019-09-19 11:34:54 +00:00
CoreBaseSettings = coreBaseSettings;
2019-09-23 12:20:08 +00:00
PathUtils = pathUtils;
EmailValidationKeyProvider = emailValidationKeyProvider;
2019-10-17 15:55:35 +00:00
Options = options;
2019-10-18 08:48:27 +00:00
HttpContextAccessor = httpContextAccessor;
2019-09-13 11:18:27 +00:00
}
public IDataStore GetStorage(string tenant, string module)
{
return GetStorage(string.Empty, tenant, module);
}
public IDataStore GetStorage(string configpath, string tenant, string module)
{
int.TryParse(tenant, out var tenantId);
2020-01-21 12:44:05 +00:00
return GetStorage(configpath, tenant, module, new TenantQuotaController(tenantId, TenantManager));
2019-09-13 11:18:27 +00:00
}
public IDataStore GetStorage(string configpath, string tenant, string module, IQuotaController controller)
{
var tenantId = -2;
if (string.IsNullOrEmpty(tenant))
{
tenant = DefaultTenantName;
}
else
{
tenantId = Convert.ToInt32(tenant);
}
//Make tennant path
2020-01-21 12:44:05 +00:00
tenant = TenantPath.CreatePath(tenant);
2019-09-13 11:18:27 +00:00
2020-02-19 08:37:52 +00:00
//remove cache
2020-06-28 11:08:12 +00:00
//var store = DataStoreCache.Get(tenant, module);//TODO
2020-02-19 08:37:52 +00:00
//if (store == null)
//{
var section = StorageFactoryConfig.Section;
if (section == null)
2019-09-13 11:18:27 +00:00
{
2020-02-19 08:37:52 +00:00
throw new InvalidOperationException("config section not found");
}
2019-09-13 11:18:27 +00:00
2020-02-19 08:37:52 +00:00
var settings = SettingsManager.LoadForTenant<StorageSettings>(tenantId);
2019-09-13 11:18:27 +00:00
2020-02-19 08:37:52 +00:00
//}
return GetDataStore(tenant, module, StorageSettingsHelper.DataStoreConsumer(settings), controller);
2019-09-13 11:18:27 +00:00
}
2019-06-04 14:43:20 +00:00
2019-09-13 11:18:27 +00:00
public IDataStore GetStorageFromConsumer(string configpath, string tenant, string module, DataStoreConsumer consumer)
2019-06-04 14:43:20 +00:00
{
2019-09-13 11:18:27 +00:00
if (tenant == null) tenant = DefaultTenantName;
//Make tennant path
2020-01-21 12:44:05 +00:00
tenant = TenantPath.CreatePath(tenant);
2019-09-13 11:18:27 +00:00
var section = StorageFactoryConfig.Section;
if (section == null)
2019-06-04 14:43:20 +00:00
{
2019-09-13 11:18:27 +00:00
throw new InvalidOperationException("config section not found");
2019-06-04 14:43:20 +00:00
}
2019-09-13 11:18:27 +00:00
int.TryParse(tenant, out var tenantId);
2020-01-21 12:44:05 +00:00
return GetDataStore(tenant, module, consumer, new TenantQuotaController(tenantId, TenantManager));
2019-06-04 14:43:20 +00:00
}
2019-09-13 11:18:27 +00:00
private IDataStore GetStoreAndCache(string tenant, string module, DataStoreConsumer consumer, IQuotaController controller)
2019-06-04 14:43:20 +00:00
{
var store = GetDataStore(tenant, module, consumer, controller);
if (store != null)
{
DataStoreCache.Put(store, tenant, module);
}
return store;
}
2019-09-13 11:18:27 +00:00
private IDataStore GetDataStore(string tenant, string module, DataStoreConsumer consumer, IQuotaController controller)
2019-06-04 14:43:20 +00:00
{
2019-09-13 11:18:27 +00:00
var storage = StorageFactoryConfig.Section;
2019-06-04 14:43:20 +00:00
var moduleElement = storage.GetModuleElement(module);
if (moduleElement == null)
{
throw new ArgumentException("no such module", module);
}
var handler = storage.GetHandler(moduleElement.Type);
Type instanceType;
IDictionary<string, string> props;
2019-09-19 11:34:54 +00:00
if (CoreBaseSettings.Standalone &&
2019-06-04 14:43:20 +00:00
!moduleElement.DisableMigrate &&
consumer.IsSet)
{
instanceType = consumer.HandlerType;
props = consumer;
}
else
{
instanceType = Type.GetType(handler.Type, true);
2019-08-15 12:04:42 +00:00
props = handler.Property.ToDictionary(r => r.Name, r => r.Value);
2019-06-04 14:43:20 +00:00
}
2019-10-18 08:48:27 +00:00
return ((IDataStore)Activator.CreateInstance(instanceType, TenantManager, PathUtils, EmailValidationKeyProvider, HttpContextAccessor, Options))
2019-09-20 14:04:06 +00:00
.Configure(tenant, handler, moduleElement, props)
2019-06-04 14:43:20 +00:00
.SetQuotaController(moduleElement.Count ? controller : null
/*don't count quota if specified on module*/);
}
}
2019-10-31 11:28:30 +00:00
public static class StorageFactoryExtension
{
2020-02-17 08:58:14 +00:00
public static DIHelper AddStorageFactoryConfigService(this DIHelper services)
2019-10-31 11:28:30 +00:00
{
services.TryAddSingleton<StorageFactoryConfig>();
return services;
}
2020-02-17 08:58:14 +00:00
public static DIHelper AddStorageFactoryService(this DIHelper services)
2019-10-31 11:28:30 +00:00
{
2020-07-17 10:52:28 +00:00
if (services.TryAddScoped<StorageFactory>())
{
services.TryAddSingleton<StorageFactoryListener>();
return services
.AddConsumerFactoryService()
.AddTenantManagerService()
.AddCoreBaseSettingsService()
.AddPathUtilsService()
.AddEmailValidationKeyProviderService()
.AddStorageSettingsService()
.AddStorage();
}
return services;
2019-10-31 11:28:30 +00:00
}
}
2019-06-04 14:43:20 +00:00
}