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

260 lines
10 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-12-20 11:17:01 +00:00
2020-02-17 08:58:14 +00:00
using ASC.Common;
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-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-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
{
2020-10-22 17:57:18 +00:00
[Singletone(Additional = typeof(StorageSettingsExtension))]
2019-09-13 11:18:27 +00:00
public class BaseStorageSettingsListener
{
2020-10-12 17:40:14 +00:00
private IServiceProvider ServiceProvider { get; }
private volatile bool Subscribed;
private readonly object locker;
2019-11-06 15:03:09 +00:00
public BaseStorageSettingsListener(IServiceProvider serviceProvider)
2019-09-13 11:18:27 +00:00
{
ServiceProvider = serviceProvider;
2020-10-12 17:40:14 +00:00
locker = new object();
}
public void Subscribe()
{
if (Subscribed) return;
lock (locker)
2019-09-13 11:18:27 +00:00
{
2020-10-12 17:40:14 +00:00
if (Subscribed) return;
2019-09-13 11:18:27 +00:00
2020-10-12 17:40:14 +00:00
Subscribed = true;
2019-11-06 15:03:09 +00:00
2020-10-12 17:40:14 +00:00
ServiceProvider.GetService<ICacheNotify<ConsumerCacheItem>>().Subscribe((i) =>
2019-11-06 15:03:09 +00:00
{
2020-10-12 17:40:14 +00:00
using var scope = ServiceProvider.CreateScope();
var scopeClass = scope.ServiceProvider.GetService<BaseStorageSettingsListenerScope>();
var (storageSettingsHelper, settingsManager, cdnStorageSettings) = scopeClass;
var settings = settingsManager.LoadForTenant<StorageSettings>(i.TenantId);
if (i.Name == settings.Module)
{
storageSettingsHelper.Clear(settings);
}
var cdnSettings = settingsManager.LoadForTenant<CdnStorageSettings>(i.TenantId);
if (i.Name == cdnSettings.Module)
{
storageSettingsHelper.Clear(cdnSettings);
}
}, CacheNotifyAction.Remove);
}
2019-09-13 11:18:27 +00:00
}
}
2019-11-06 15:03:09 +00:00
[Serializable]
public abstract class BaseStorageSettings<T> : ISettings where T : class, ISettings, new()
2019-11-06 15:03:09 +00:00
{
public string Module { get; set; }
2019-09-13 11:18:27 +00:00
public Dictionary<string, string> Props { get; set; }
2020-09-30 14:47:42 +00:00
public ISettings GetDefault(IServiceProvider serviceProvider)
{
return new T();
}
public virtual Func<DataStoreConsumer, DataStoreConsumer> Switch { get { return d => d; } }
2020-08-12 09:58:08 +00:00
internal ICacheNotify<DataStoreCacheItem> Cache { get; set; }
public abstract Guid ID { get; }
}
[Serializable]
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
2020-10-19 15:53:15 +00:00
[Scope]
[Serializable]
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; } }
}
2020-10-19 15:53:15 +00:00
[Scope]
public class StorageSettingsHelper
{
2020-08-12 09:58:08 +00:00
private StorageFactoryConfig StorageFactoryConfig { get; }
private PathUtils PathUtils { get; }
private ICacheNotify<DataStoreCacheItem> Cache { get; }
private IOptionsMonitor<ILog> Options { get; }
private TenantManager TenantManager { get; }
private SettingsManager SettingsManager { get; }
private IHttpContextAccessor HttpContextAccessor { get; }
private ConsumerFactory ConsumerFactory { get; }
public StorageSettingsHelper(
2020-10-12 17:40:14 +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-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
ConsumerFactory consumerFactory)
2019-09-13 11:18:27 +00:00
{
2020-10-12 17:40:14 +00:00
baseStorageSettingsListener.Subscribe();
2019-09-13 11:18:27 +00:00
StorageFactoryConfig = storageFactoryConfig;
2019-09-21 16:39:17 +00:00
PathUtils = pathUtils;
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-12-20 11:17:01 +00:00
ConsumerFactory = consumerFactory;
2019-10-18 08:48:27 +00:00
}
2020-04-28 15:11:10 +00:00
public StorageSettingsHelper(
2020-10-12 17:40:14 +00:00
BaseStorageSettingsListener baseStorageSettingsListener,
2020-04-28 15:11:10 +00:00
StorageFactoryConfig storageFactoryConfig,
PathUtils pathUtils,
ICacheNotify<DataStoreCacheItem> cache,
IOptionsMonitor<ILog> options,
TenantManager tenantManager,
SettingsManager settingsManager,
IHttpContextAccessor httpContextAccessor,
ConsumerFactory consumerFactory)
2020-10-12 17:40:14 +00:00
: this(baseStorageSettingsListener, storageFactoryConfig, pathUtils, cache, options, tenantManager, settingsManager, consumerFactory)
2020-04-28 15:11:10 +00:00
{
HttpContextAccessor = httpContextAccessor;
}
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();
2020-01-21 12:44:05 +00:00
var path = TenantPath.CreatePath(tenantId);
2019-09-13 11:18:27 +00:00
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;
2020-10-12 19:39:23 +00:00
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
}
2020-08-24 18:41:06 +00:00
}
2020-08-16 10:11:15 +00:00
2020-10-22 17:57:18 +00:00
[Scope]
2020-08-31 08:18:07 +00:00
public class BaseStorageSettingsListenerScope
2020-08-24 18:41:06 +00:00
{
2020-08-31 08:18:07 +00:00
private StorageSettingsHelper StorageSettingsHelper { get; }
private SettingsManager SettingsManager { get; }
private CdnStorageSettings CdnStorageSettings { get; }
2020-08-16 10:11:15 +00:00
2020-08-31 08:18:07 +00:00
public BaseStorageSettingsListenerScope(StorageSettingsHelper storageSettingsHelper, SettingsManager settingsManager, CdnStorageSettings cdnStorageSettings)
2020-08-24 18:41:06 +00:00
{
StorageSettingsHelper = storageSettingsHelper;
SettingsManager = settingsManager;
CdnStorageSettings = cdnStorageSettings;
2020-08-16 10:11:15 +00:00
}
2020-08-31 08:18:07 +00:00
public void Deconstruct(out StorageSettingsHelper storageSettingsHelper, out SettingsManager settingsManager, out CdnStorageSettings cdnStorageSettings)
{
storageSettingsHelper = StorageSettingsHelper;
settingsManager = SettingsManager;
cdnStorageSettings = CdnStorageSettings;
}
2019-10-31 11:28:30 +00:00
}
2020-10-22 17:57:18 +00:00
public class StorageSettingsExtension
2019-11-06 15:03:09 +00:00
{
2020-10-22 17:57:18 +00:00
public static void Register(DIHelper services)
2019-10-31 11:28:30 +00:00
{
2020-10-22 17:57:18 +00:00
services.TryAdd<BaseStorageSettingsListenerScope>();
2019-11-06 15:03:09 +00:00
}
}
}