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

224 lines
8.8 KiB
C#
Raw Normal View History

2022-03-15 18:00:53 +00:00
// (c) Copyright Ascensio System SIA 2010-2022
//
// This program is a free software product.
// You can redistribute it and/or modify it under the terms
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
// Foundation. In accordance with Section 7(a) of the GNU AGPL 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 details, see
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
//
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
//
// The interactive user interfaces in modified source and object code versions of the Program must
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
//
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
// trademark law for use of our trademarks.
//
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
namespace ASC.Data.Storage;
[Singletone(Additional = typeof(StorageConfigExtension))]
public class StorageFactoryConfig
2019-06-04 14:43:20 +00:00
{
2022-09-05 11:27:38 +00:00
private readonly Configuration.Storage _section;
private readonly ConfigurationExtension _configurationExtension;
2022-02-10 11:24:16 +00:00
2022-09-05 11:27:38 +00:00
public StorageFactoryConfig(Configuration.Storage storage, ConfigurationExtension configurationExtension)
2019-09-13 11:18:27 +00:00
{
2022-09-05 11:27:38 +00:00
_section = storage;
_configurationExtension = configurationExtension;
2022-02-10 11:24:16 +00:00
}
2019-06-04 14:43:20 +00:00
2022-09-05 11:27:38 +00:00
public IEnumerable<string> GetModuleList(string region = "current", bool exceptDisabledMigration = false)
2022-02-10 11:24:16 +00:00
{
2022-09-05 11:27:38 +00:00
return GetStorage(region).Module
2022-01-18 14:00:28 +00:00
.Where(x => x.Visible && (!exceptDisabledMigration || !x.DisableMigrate))
2022-02-10 11:24:16 +00:00
.Select(x => x.Name);
}
2019-06-04 14:43:20 +00:00
2022-09-05 11:27:38 +00:00
public IEnumerable<string> GetDomainList(string modulename, string region = "current")
2022-02-10 11:24:16 +00:00
{
2022-09-05 11:27:38 +00:00
var section = GetStorage(region);
if (section == null)
2019-06-04 14:43:20 +00:00
{
2022-02-10 11:24:16 +00:00
throw new ArgumentException("config section not found");
2019-06-04 14:43:20 +00:00
}
2022-09-05 11:27:38 +00:00
return section.Module
2022-02-10 11:24:16 +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
}
2022-09-05 11:27:38 +00:00
public Configuration.Storage GetStorage(string region)
{
return region == "current" ? _section : _configurationExtension.GetSetting<Configuration.Storage>($"regions:{region}:storage");
}
2022-02-10 11:24:16 +00:00
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public static class StorageFactoryExtenstion
{
public static void InitializeHttpHandlers(this IEndpointRouteBuilder builder, string config = null)
2019-09-13 11:18:27 +00:00
{
2022-02-10 11:24:16 +00:00
//TODO:
//if (!HostingEnvironment.IsHosted)
//{
// throw new InvalidOperationException("Application not hosted.");
//}
2019-08-15 12:04:42 +00:00
2022-02-10 11:24:16 +00:00
var section = builder.ServiceProvider.GetService<Configuration.Storage>();
var pathUtils = builder.ServiceProvider.GetService<PathUtils>();
if (section != null)
{
if (section.Module != null)
{
foreach (var m in section.Module)
2019-06-04 14:43:20 +00:00
{
2022-02-10 11:24:16 +00:00
//todo: add path criterion
if (m.Type == "disc" || !m.Public || m.Path.Contains(Constants.StorageRootParam))
2022-03-17 15:01:39 +00:00
{
2022-02-10 11:24:16 +00:00
builder.RegisterStorageHandler(
m.Name,
string.Empty,
m.Public);
2022-03-17 15:01:39 +00:00
}
2022-02-10 11:24:16 +00:00
//todo: add path criterion
if (m.Domain != null)
2019-06-04 14:43:20 +00:00
{
2022-02-10 11:24:16 +00:00
foreach (var d in m.Domain.Where(d => d.Path.Contains(Constants.StorageRootParam)))
{
2020-10-12 19:39:23 +00:00
builder.RegisterStorageHandler(
2019-06-04 14:43:20 +00:00
m.Name,
2022-02-10 11:24:16 +00:00
d.Name,
d.Public);
2019-06-04 14:43:20 +00:00
}
}
}
}
}
2019-09-13 11:18:27 +00:00
}
2022-02-10 11:24:16 +00:00
}
[Scope(Additional = typeof(StorageFactoryExtension))]
public class StorageFactory
{
2022-03-25 16:26:06 +00:00
private const string DefaultTenantName = "default";
2022-02-10 11:24:16 +00:00
private readonly StorageFactoryConfig _storageFactoryConfig;
private readonly SettingsManager _settingsManager;
private readonly StorageSettingsHelper _storageSettingsHelper;
private readonly CoreBaseSettings _coreBaseSettings;
private readonly IServiceProvider _serviceProvider;
public StorageFactory(
IServiceProvider serviceProvider,
StorageFactoryConfig storageFactoryConfig,
SettingsManager settingsManager,
StorageSettingsHelper storageSettingsHelper,
2022-10-31 10:03:02 +00:00
CoreBaseSettings coreBaseSettings)
2022-02-10 11:24:16 +00:00
{
_serviceProvider = serviceProvider;
_storageFactoryConfig = storageFactoryConfig;
_settingsManager = settingsManager;
_storageSettingsHelper = storageSettingsHelper;
_coreBaseSettings = coreBaseSettings;
}
2019-09-13 11:18:27 +00:00
2023-06-27 09:18:04 +00:00
public async Task<IDataStore> GetStorageAsync(int tenant, string module, string region = "current")
2022-02-10 11:24:16 +00:00
{
2022-10-20 13:19:20 +00:00
var tenantQuotaController = _serviceProvider.GetService<TenantQuotaController>();
2023-06-27 09:18:04 +00:00
tenantQuotaController.Init(tenant);
2019-09-13 11:18:27 +00:00
2023-03-24 16:24:59 +00:00
return await GetStorageAsync(tenant, module, tenantQuotaController, region);
2022-02-10 11:24:16 +00:00
}
2019-09-13 11:18:27 +00:00
2023-03-24 16:24:59 +00:00
public async Task<IDataStore> GetStorageAsync(int? tenant, string module, IQuotaController controller, string region = "current")
2022-02-10 11:24:16 +00:00
{
2022-10-28 12:23:51 +00:00
var tenantPath = tenant != null ? TenantPath.CreatePath(tenant.Value) : TenantPath.CreatePath(DefaultTenantName);
2019-09-13 11:18:27 +00:00
2022-10-28 12:23:51 +00:00
tenant = tenant ?? -2;
2022-09-05 11:27:38 +00:00
var section = _storageFactoryConfig.GetStorage(region);
2022-02-10 11:24:16 +00:00
if (section == null)
{
throw new InvalidOperationException("config section not found");
2019-09-13 11:18:27 +00:00
}
2023-03-24 16:24:59 +00:00
var settings = await _settingsManager.LoadAsync<StorageSettings>(tenant.Value);
2022-02-10 11:24:16 +00:00
//TODO:GetStoreAndCache
return GetDataStore(tenantPath, module, _storageSettingsHelper.DataStoreConsumer(settings), controller, region);
2022-02-10 11:24:16 +00:00
}
2019-09-13 11:18:27 +00:00
public IDataStore GetStorageFromConsumer(int? tenant, string module, DataStoreConsumer consumer, string region = "current")
2022-02-10 11:24:16 +00:00
{
2022-10-28 12:23:51 +00:00
var tenantPath = tenant != null ? TenantPath.CreatePath(tenant.Value) : TenantPath.CreatePath(DefaultTenantName);
2019-09-13 11:18:27 +00:00
2022-09-05 11:27:38 +00:00
var section = _storageFactoryConfig.GetStorage(region);
2022-02-10 11:24:16 +00:00
if (section == null)
{
throw new InvalidOperationException("config section not found");
2019-09-13 11:18:27 +00:00
}
2019-06-04 14:43:20 +00:00
2022-10-20 13:19:20 +00:00
var tenantQuotaController = _serviceProvider.GetService<TenantQuotaController>();
2022-10-28 12:23:51 +00:00
tenantQuotaController.Init(tenant.GetValueOrDefault());
2019-09-13 11:18:27 +00:00
2022-10-28 12:23:51 +00:00
return GetDataStore(tenantPath, module, consumer, tenantQuotaController);
2022-02-10 11:24:16 +00:00
}
2019-09-13 11:18:27 +00:00
private IDataStore GetDataStore(string tenantPath, string module, DataStoreConsumer consumer, IQuotaController controller, string region = "current")
2022-02-10 11:24:16 +00:00
{
2022-09-05 11:27:38 +00:00
var storage = _storageFactoryConfig.GetStorage(region);
2022-02-10 11:24:16 +00:00
var moduleElement = storage.GetModuleElement(module);
if (moduleElement == null)
{
throw new ArgumentException("no such module", module);
}
2019-09-13 11:18:27 +00:00
2022-02-10 11:24:16 +00:00
var handler = storage.GetHandler(moduleElement.Type);
Type instanceType;
IDictionary<string, string> props;
2022-02-10 11:24:16 +00:00
if (_coreBaseSettings.Standalone &&
!moduleElement.DisableMigrate &&
consumer.IsSet)
{
instanceType = consumer.HandlerType;
props = consumer;
2019-06-04 14:43:20 +00:00
}
2022-02-10 11:24:16 +00:00
else
2019-06-04 14:43:20 +00:00
{
2022-02-10 11:24:16 +00:00
instanceType = Type.GetType(handler.Type, true);
props = handler.Property.ToDictionary(r => r.Name, r => r.Value);
2019-06-04 14:43:20 +00:00
}
2022-02-10 11:24:16 +00:00
2022-03-25 09:23:28 +00:00
2022-02-10 11:24:16 +00:00
return ((IDataStore)ActivatorUtilities.CreateInstance(_serviceProvider, instanceType))
2022-10-28 12:23:51 +00:00
.Configure(tenantPath, handler, moduleElement, props)
2022-02-10 11:24:16 +00:00
.SetQuotaController(moduleElement.Count ? controller : null
/*don't count quota if specified on module*/);
2019-06-04 14:43:20 +00:00
}
2022-02-10 11:24:16 +00:00
}
2020-12-04 10:52:07 +00:00
public static class StorageFactoryExtension
2022-02-10 11:24:16 +00:00
{
public static void Register(DIHelper services)
2020-12-04 10:52:07 +00:00
{
2022-02-10 11:24:16 +00:00
services.TryAdd<DiscDataStore>();
services.TryAdd<GoogleCloudStorage>();
services.TryAdd<RackspaceCloudStorage>();
services.TryAdd<S3Storage>();
2022-10-20 13:19:20 +00:00
services.TryAdd<TenantQuotaController>();
2020-12-04 10:52:07 +00:00
}
2022-02-10 11:24:16 +00:00
}