DocSpace-buildtools/common/ASC.Data.Storage/Configuration/StorageSettings.cs

242 lines
9.8 KiB
C#
Raw Normal View History

2019-11-06 15:03:09 +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;
2019-08-15 12:04:42 +00:00
using System.Runtime.Serialization;
2019-12-20 11:17:01 +00:00
2019-11-06 15:03:09 +00:00
using ASC.Common.Caching;
2019-10-17 15:55:35 +00:00
using ASC.Common.Logging;
2019-11-06 15:03:09 +00:00
using ASC.Core;
using ASC.Core.Common.Configuration;
using ASC.Core.Common.Settings;
2019-09-23 12:20:08 +00:00
using ASC.Security.Cryptography;
2019-12-20 11:17:01 +00:00
2019-10-18 08:48:27 +00:00
using Microsoft.AspNetCore.Http;
2019-09-13 11:18:27 +00:00
using Microsoft.Extensions.DependencyInjection;
2019-10-31 11:28:30 +00:00
using Microsoft.Extensions.DependencyInjection.Extensions;
2019-10-17 15:55:35 +00:00
using Microsoft.Extensions.Options;
2019-09-13 11:18:27 +00:00
2019-11-06 15:03:09 +00:00
namespace ASC.Data.Storage.Configuration
2019-09-13 11:18:27 +00:00
{
public class BaseStorageSettingsListener
{
2019-11-06 15:03:09 +00:00
public BaseStorageSettingsListener(IServiceProvider serviceProvider)
2019-09-13 11:18:27 +00:00
{
ServiceProvider = serviceProvider;
2019-11-06 15:03:09 +00:00
serviceProvider.GetService<ICacheNotify<ConsumerCacheItem>>().Subscribe((i) =>
2019-09-13 11:18:27 +00:00
{
2019-11-15 10:11:40 +00:00
using var scope = ServiceProvider.CreateScope();
2019-09-13 11:18:27 +00:00
var storageSettingsHelper = scope.ServiceProvider.GetService<StorageSettingsHelper>();
var storageSettings = scope.ServiceProvider.GetService<SettingsManager>();
var settings = storageSettings.LoadForTenant<StorageSettings>(i.TenantId);
2019-11-06 15:03:09 +00:00
if (i.Name == settings.Module)
{
storageSettingsHelper.Clear(settings);
2019-11-06 15:03:09 +00:00
}
var cdnStorageSettings = scope.ServiceProvider.GetService<CdnStorageSettings>();
var cdnSettings = storageSettings.LoadForTenant<CdnStorageSettings>(i.TenantId);
2019-11-06 15:03:09 +00:00
if (i.Name == cdnSettings.Module)
{
storageSettingsHelper.Clear(cdnSettings);
2019-09-13 11:18:27 +00:00
}
}, CacheNotifyAction.Remove);
}
public IServiceProvider ServiceProvider { get; }
}
2019-11-06 15:03:09 +00:00
[Serializable]
[DataContract]
public abstract class BaseStorageSettings<T> : ISettings where T : class, ISettings, new()
2019-11-06 15:03:09 +00:00
{
[DataMember(Name = "Module")]
public string Module { get; set; }
[DataMember(Name = "Props")]
2019-09-13 11:18:27 +00:00
public Dictionary<string, string> Props { get; set; }
2019-11-08 15:11:30 +00:00
public ISettings GetDefault(IServiceProvider serviceProvider) => new T();
public virtual Func<DataStoreConsumer, DataStoreConsumer> Switch { get { return d => d; } }
public ICacheNotify<DataStoreCacheItem> Cache { get; internal set; }
public abstract Guid ID { get; }
}
[Serializable]
[DataContract]
public class StorageSettings : BaseStorageSettings<StorageSettings>
{
public override Guid ID
2019-09-13 11:18:27 +00:00
{
get { return new Guid("F13EAF2D-FA53-44F1-A6D6-A5AEDA46FA2B"); }
}
}
2019-09-13 11:18:27 +00:00
[Serializable]
[DataContract]
public class CdnStorageSettings : BaseStorageSettings<CdnStorageSettings>
{
public override Guid ID
{
get { return new Guid("0E9AE034-F398-42FE-B5EE-F86D954E9FB2"); }
2019-09-13 11:18:27 +00:00
}
public override Func<DataStoreConsumer, DataStoreConsumer> Switch { get { return d => d.Cdn; } }
}
public class StorageSettingsHelper
{
public BaseStorageSettingsListener BaseStorageSettingsListener { get; }
public StorageFactoryConfig StorageFactoryConfig { get; }
public PathUtils PathUtils { get; }
public EmailValidationKeyProvider EmailValidationKeyProvider { get; }
public ICacheNotify<DataStoreCacheItem> Cache { get; }
public IOptionsMonitor<ILog> Options { get; }
public TenantManager TenantManager { get; }
public SettingsManager SettingsManager { get; }
public IHttpContextAccessor HttpContextAccessor { get; }
2019-12-20 11:17:01 +00:00
public ConsumerFactory ConsumerFactory { get; }
public StorageSettingsHelper(
2019-09-13 11:18:27 +00:00
BaseStorageSettingsListener baseStorageSettingsListener,
2019-09-21 16:39:17 +00:00
StorageFactoryConfig storageFactoryConfig,
2019-09-23 12:20:08 +00:00
PathUtils pathUtils,
2019-10-11 15:03:03 +00:00
EmailValidationKeyProvider emailValidationKeyProvider,
2019-10-17 15:55:35 +00:00
ICacheNotify<DataStoreCacheItem> cache,
2019-11-06 15:03:09 +00:00
IOptionsMonitor<ILog> options,
TenantManager tenantManager,
SettingsManager settingsManager,
2019-12-20 11:17:01 +00:00
IHttpContextAccessor httpContextAccessor,
ConsumerFactory consumerFactory)
2019-09-13 11:18:27 +00:00
{
BaseStorageSettingsListener = baseStorageSettingsListener;
StorageFactoryConfig = storageFactoryConfig;
2019-09-21 16:39:17 +00:00
PathUtils = pathUtils;
2019-09-23 12:20:08 +00:00
EmailValidationKeyProvider = emailValidationKeyProvider;
2019-10-11 15:03:03 +00:00
Cache = cache;
2019-10-17 15:55:35 +00:00
Options = options;
TenantManager = tenantManager;
SettingsManager = settingsManager;
2019-10-18 08:48:27 +00:00
HttpContextAccessor = httpContextAccessor;
2019-12-20 11:17:01 +00:00
ConsumerFactory = consumerFactory;
2019-10-18 08:48:27 +00:00
}
2019-11-06 15:03:09 +00:00
public bool Save<T>(BaseStorageSettings<T> baseStorageSettings) where T : class, ISettings, new()
2019-11-06 15:03:09 +00:00
{
ClearDataStoreCache();
dataStoreConsumer = null;
return SettingsManager.Save(baseStorageSettings);
2019-09-13 11:18:27 +00:00
}
internal void ClearDataStoreCache()
{
2019-09-17 08:07:46 +00:00
var tenantId = TenantManager.GetCurrentTenant().TenantId.ToString();
2019-09-13 11:18:27 +00:00
var path = TennantPath.CreatePath(tenantId);
foreach (var module in StorageFactoryConfig.GetModuleList("", true))
{
Cache.Publish(new DataStoreCacheItem() { TenantId = path, Module = module }, CacheNotifyAction.Remove);
}
}
2019-11-06 15:03:09 +00:00
public void Clear<T>(BaseStorageSettings<T> baseStorageSettings) where T : class, ISettings, new()
2019-11-06 15:03:09 +00:00
{
baseStorageSettings.Module = null;
baseStorageSettings.Props = null;
Save(baseStorageSettings);
2019-11-06 15:03:09 +00:00
}
private DataStoreConsumer dataStoreConsumer;
public DataStoreConsumer DataStoreConsumer<T>(BaseStorageSettings<T> baseStorageSettings) where T : class, ISettings, new()
2019-11-06 15:03:09 +00:00
{
if (string.IsNullOrEmpty(baseStorageSettings.Module) || baseStorageSettings.Props == null) return dataStoreConsumer = new DataStoreConsumer();
2019-11-06 15:03:09 +00:00
2019-12-20 11:17:01 +00:00
var consumer = ConsumerFactory.GetByKey<DataStoreConsumer>(baseStorageSettings.Module);
2019-11-06 15:03:09 +00:00
if (!consumer.IsSet) return dataStoreConsumer = new DataStoreConsumer();
2019-11-06 15:03:09 +00:00
dataStoreConsumer = (DataStoreConsumer)consumer.Clone();
2019-11-06 15:03:09 +00:00
foreach (var prop in baseStorageSettings.Props)
2019-11-06 15:03:09 +00:00
{
dataStoreConsumer[prop.Key] = prop.Value;
2019-11-06 15:03:09 +00:00
}
2019-09-13 11:18:27 +00:00
return dataStoreConsumer;
2019-10-18 08:48:27 +00:00
}
private IDataStore dataStore;
public IDataStore DataStore<T>(BaseStorageSettings<T> baseStorageSettings) where T : class, ISettings, new()
2019-11-06 15:03:09 +00:00
{
if (dataStore != null) return dataStore;
2019-11-06 15:03:09 +00:00
if (DataStoreConsumer(baseStorageSettings).HandlerType == null) return null;
2019-11-06 15:03:09 +00:00
return dataStore = ((IDataStore)
Activator.CreateInstance(DataStoreConsumer(baseStorageSettings).HandlerType, TenantManager, PathUtils, HttpContextAccessor, Options))
.Configure(TenantManager.GetCurrentTenant().TenantId.ToString(), null, null, DataStoreConsumer(baseStorageSettings));
2019-11-06 15:03:09 +00:00
}
2019-10-31 11:28:30 +00:00
}
public static class StorageSettingsExtension
2019-11-06 15:03:09 +00:00
{
public static IServiceCollection AddBaseStorageSettingsService(this IServiceCollection services)
2019-10-31 11:28:30 +00:00
{
services.TryAddSingleton(typeof(ICacheNotify<>), typeof(KafkaCache<>));
services.TryAddSingleton<BaseStorageSettingsListener>();
2019-11-06 15:03:09 +00:00
2019-10-31 11:28:30 +00:00
return services
.AddStorageFactoryConfigService()
.AddPathUtilsService()
.AddEmailValidationKeyProviderService()
2019-11-06 15:03:09 +00:00
.AddHttpContextAccessor();
2019-10-31 11:28:30 +00:00
}
2019-11-06 15:03:09 +00:00
public static IServiceCollection AddCdnStorageSettingsService(this IServiceCollection services)
2019-10-31 11:28:30 +00:00
{
services.TryAddScoped<StorageSettingsHelper>();
2019-11-06 15:03:09 +00:00
return services
.AddSettingsManagerService()
2019-12-20 11:17:01 +00:00
.AddBaseStorageSettingsService()
.AddConsumerFactoryService();
2019-10-31 11:28:30 +00:00
}
2019-11-06 15:03:09 +00:00
public static IServiceCollection AddStorageSettingsService(this IServiceCollection services)
2019-10-31 11:28:30 +00:00
{
services.TryAddScoped<StorageSettingsHelper>();
2019-11-06 15:03:09 +00:00
return services
.AddSettingsManagerService()
2019-12-20 11:17:01 +00:00
.AddBaseStorageSettingsService()
.AddConsumerFactoryService();
2019-11-06 15:03:09 +00:00
}
}
}