DocSpace-client/common/services/ASC.Data.Backup/Storage/BackupStorageFactory.cs

119 lines
5.1 KiB
C#
Raw Normal View History

2020-05-20 15:14:44 +00:00
/*
*
* (c) Copyright Ascensio System Limited 2010-2020
*
* 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;
2020-05-29 14:50:39 +00:00
using System.Collections.Generic;
using ASC.Common;
using ASC.Common.Utils;
2020-05-20 15:14:44 +00:00
using ASC.Core;
2020-06-23 10:48:36 +00:00
using ASC.Data.Backup.Contracts;
2020-05-20 15:14:44 +00:00
using ASC.Data.Backup.EF.Model;
using ASC.Data.Backup.Service;
using ASC.Data.Backup.Utils;
2020-06-10 12:35:10 +00:00
using ASC.Web.Files.Utils;
2020-05-29 14:50:39 +00:00
using Microsoft.Extensions.Configuration;
2020-05-29 14:50:39 +00:00
using Newtonsoft.Json;
2020-05-20 15:14:44 +00:00
namespace ASC.Data.Backup.Storage
{
public class BackupStorageFactory
2020-05-29 14:50:39 +00:00
{
2020-05-20 15:14:44 +00:00
public IServiceProvider ServiceProvider { get; }
public IConfiguration Configuration { get; }
2020-06-10 12:35:10 +00:00
public DocumentsBackupStorage DocumentsBackupStorage { get; }
public DataStoreBackupStorage DataStoreBackupStorage { get; }
public LocalBackupStorage LocalBackupStorage { get; }
public ConsumerBackupStorage ConsumerBackupStorage { get; }
public TenantManager TenantManager { get; }
public BackupStorageFactory(ConsumerBackupStorage consumerBackupStorage ,LocalBackupStorage localBackupStorage, IServiceProvider serviceProvider, IConfiguration configuration, DocumentsBackupStorage documentsBackupStorage, TenantManager tenantManager, DataStoreBackupStorage dataStoreBackupStorage)
2020-05-20 15:14:44 +00:00
{
ServiceProvider = serviceProvider;
Configuration = configuration;
2020-06-10 12:35:10 +00:00
DocumentsBackupStorage = documentsBackupStorage;
DataStoreBackupStorage = dataStoreBackupStorage;
LocalBackupStorage = localBackupStorage;
ConsumerBackupStorage = consumerBackupStorage;
TenantManager = tenantManager;
2020-05-29 14:50:39 +00:00
}
public IBackupStorage GetBackupStorage(BackupRecord record)
{
return GetBackupStorage(record.StorageType, record.TenantId, JsonConvert.DeserializeObject<Dictionary<string, string>>(record.StorageParams));
}
2020-05-20 15:14:44 +00:00
public IBackupStorage GetBackupStorage(BackupStorageType type, int tenantId, Dictionary<string, string> storageParams)
{
var settings = Configuration.GetSetting<BackupSettings>("backup");
var webConfigPath = PathHelper.ToRootedConfigPath(settings.WebConfigs.CurrentPath);
2020-06-10 12:35:10 +00:00
2020-05-29 14:50:39 +00:00
2020-05-20 15:14:44 +00:00
switch (type)
{
case BackupStorageType.Documents:
case BackupStorageType.ThridpartyDocuments:
2020-06-10 12:35:10 +00:00
{
DocumentsBackupStorage.Init(tenantId, webConfigPath);
return DocumentsBackupStorage;
}
2020-05-20 15:14:44 +00:00
case BackupStorageType.DataStore:
2020-06-10 12:35:10 +00:00
{
DataStoreBackupStorage.Init(tenantId, webConfigPath);
return DataStoreBackupStorage;
}
2020-05-20 15:14:44 +00:00
case BackupStorageType.Local:
2020-06-10 12:35:10 +00:00
return LocalBackupStorage;
2020-05-20 15:14:44 +00:00
case BackupStorageType.ThirdPartyConsumer:
2020-06-10 12:35:10 +00:00
{
if (storageParams == null) return null;
TenantManager.SetCurrentTenant(tenantId);
ConsumerBackupStorage.Init(storageParams);
return ConsumerBackupStorage;
}
2020-05-20 15:14:44 +00:00
default:
throw new InvalidOperationException("Unknown storage type.");
}
}
}
public static class BackupStorageFactoryExtension
{
public static DIHelper AddBackupStorageFactory(this DIHelper services)
{
services.TryAddScoped<BackupStorageFactory>();
return services
.AddTenantManagerService()
2020-06-10 12:35:10 +00:00
.AddDocumentsBackupStorage()
.AddDataStoreBackupStorage()
.AddLocalBackupStorage()
.AddConsumerBackupStorage()
.AddFileConverterService();
2020-05-20 15:14:44 +00:00
}
}
}