// (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 namespace ASC.Migration.PersonalToDocspace.Runner; [Scope] public class MigrationRunner { private readonly DbFactory _dbFactory; private readonly StorageFactory _storageFactory; private readonly StorageFactoryConfig _storageFactoryConfig; private readonly ModuleProvider _moduleProvider; private readonly ILogger _logger; private string _backupFile; private string _region; private List _modules; private readonly List _namesModules = new List() { ModuleName.Core, ModuleName.Files, ModuleName.Files2, ModuleName.Tenants, ModuleName.WebStudio }; public MigrationRunner( DbFactory dbFactory, StorageFactory storageFactory, StorageFactoryConfig storageFactoryConfig, ModuleProvider moduleProvider, ILogger logger) { _dbFactory = dbFactory; _storageFactory = storageFactory; _storageFactoryConfig = storageFactoryConfig; _moduleProvider = moduleProvider; _logger = logger; } public async Task Run(string backupFile, string region) { _region = region; _modules = _moduleProvider.AllModules.Where(m => _namesModules.Contains(m.ModuleName)).ToList(); _backupFile = backupFile; var columnMapper = new ColumnMapper(); using (var dataReader = new ZipReadOperator(_backupFile)) { foreach (var module in _modules) { Console.WriteLine($"start restore module: {module}"); var restoreTask = new RestoreDbModuleTask(_logger, module, dataReader, columnMapper, _dbFactory, false, false, _region, _storageFactory, _storageFactoryConfig, _moduleProvider); await restoreTask.RunJob(); Console.WriteLine($"end restore module: {module}"); } await DoRestoreStorage(dataReader, columnMapper); SetAdmin(columnMapper.GetTenantMapping()); SetTenantActive(columnMapper.GetTenantMapping()); } } private async Task DoRestoreStorage(IDataReadOperator dataReader, ColumnMapper columnMapper) { var fileGroups = GetFilesToProcess(dataReader).GroupBy(file => file.Module).ToList(); foreach (var group in fileGroups) { Console.WriteLine($"start restore fileGroup: {group.Key}"); foreach (var file in group) { var storage = _storageFactory.GetStorage(columnMapper.GetTenantMapping(), group.Key, _region); var quotaController = storage.QuotaController; storage.SetQuotaController(null); try { var adjustedPath = file.Path; var module = _moduleProvider.GetByStorageModule(file.Module, file.Domain); if (module == null || module.TryAdjustFilePath(false, columnMapper, ref adjustedPath)) { var key = file.GetZipKey(); using var stream = dataReader.GetEntry(key); await storage.SaveAsync(file.Domain, adjustedPath, module != null ? module.PrepareData(key, stream, columnMapper) : stream); } } finally { if (quotaController != null) { storage.SetQuotaController(quotaController); } } } Console.WriteLine($"end restore fileGroup: {group.Key}"); } } private IEnumerable GetFilesToProcess(IDataReadOperator dataReader) { using var stream = dataReader.GetEntry(KeyHelper.GetStorageRestoreInfoZipKey()); if (stream == null) { return Enumerable.Empty(); } var restoreInfo = XElement.Load(new StreamReader(stream)); return restoreInfo.Elements("file").Select(BackupFileInfo.FromXElement).ToList(); } private void SetTenantActive(int tenantId) { using var dbContext = _dbFactory.CreateDbContext(_region); var tenant = dbContext.Tenants.Single(t=> t.Id == tenantId); tenant.Status = TenantStatus.Active; tenant.LastModified = DateTime.UtcNow; tenant.StatusChanged = DateTime.UtcNow; dbContext.Tenants.Update(tenant); dbContext.SaveChanges(); } private void SetAdmin(int tenantId) { using var dbContextTenant = _dbFactory.CreateDbContext(_region); var tenant = dbContextTenant.Tenants.Single(t => t.Id == tenantId); var userGroup = new UserGroup() { Tenant = tenantId, LastModified = DateTime.UtcNow, RefType = Core.UserGroupRefType.Contains, Removed = false, UserGroupId = ASC.Common.Security.Authorizing.Constants.DocSpaceAdmin.ID, Userid = tenant.OwnerId.Value }; using var dbContextUser = _dbFactory.CreateDbContext(_region); dbContextUser.UserGroups.Update(userGroup); dbContextUser.SaveChanges(); } }