// (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.Data.Backup.Controllers; [Scope] [DefaultRoute] [ApiController] public class BackupController : ControllerBase { private readonly BackupAjaxHandler _backupHandler; private readonly IEventBus _eventBus; private readonly Guid _currentUserId; private readonly int _tenantId; public BackupController( BackupAjaxHandler backupAjaxHandler, TenantManager tenantManager, SecurityContext securityContext, IEventBus eventBus) { _currentUserId = securityContext.CurrentAccount.ID; _tenantId = tenantManager.GetCurrentTenant().Id; _backupHandler = backupAjaxHandler; _eventBus = eventBus; } /// /// Returns the backup schedule of the current portal /// /// Backup /// Backup Schedule [HttpGet("getbackupschedule")] public async Task GetBackupSchedule() { return await _backupHandler.GetScheduleAsync(); } /// /// Create the backup schedule of the current portal /// /// Storage type /// Storage parameters /// Max of the backup's stored copies /// Cron parameters /// Backup [HttpPost("createbackupschedule")] public async Task CreateBackupScheduleAsync(BackupScheduleDto backupSchedule) { var storageType = backupSchedule.StorageType == null ? BackupStorageType.Documents : (BackupStorageType)Int32.Parse(backupSchedule.StorageType); var storageParams = backupSchedule.StorageParams == null ? new Dictionary() : backupSchedule.StorageParams.ToDictionary(r => r.Key.ToString(), r => r.Value.ToString()); var backupStored = backupSchedule.BackupsStored == null ? 0 : Int32.Parse(backupSchedule.BackupsStored); var cron = new CronParams() { Period = backupSchedule.CronParams.Period == null ? BackupPeriod.EveryDay : (BackupPeriod)Int32.Parse(backupSchedule.CronParams.Period), Hour = backupSchedule.CronParams.Hour == null ? 0 : Int32.Parse(backupSchedule.CronParams.Hour), Day = backupSchedule.CronParams.Day == null ? 0 : Int32.Parse(backupSchedule.CronParams.Day), }; await _backupHandler.CreateScheduleAsync(storageType, storageParams, backupStored, cron); return true; } /// /// Delete the backup schedule of the current portal /// /// Backup [HttpDelete("deletebackupschedule")] public async Task DeleteBackupSchedule() { await _backupHandler.DeleteScheduleAsync(); return true; } /// /// Start a backup of the current portal /// /// Storage Type /// Storage Params /// Backup /// Backup Progress [AllowNotPayment] [HttpPost("startbackup")] public async Task StartBackupAsync(BackupDto backup) { var storageType = backup.StorageType == null ? BackupStorageType.Documents : (BackupStorageType)Int32.Parse(backup.StorageType); var storageParams = backup.StorageParams == null ? new Dictionary() : backup.StorageParams.ToDictionary(r => r.Key.ToString(), r => r.Value.ToString()); _eventBus.Publish(new BackupRequestIntegrationEvent( tenantId: _tenantId, storageParams: storageParams, storageType: storageType, createBy: _currentUserId )); return await _backupHandler.GetBackupProgressAsync(); } /// /// Returns the progress of the started backup /// /// Backup /// Backup Progress [AllowNotPayment] [HttpGet("getbackupprogress")] public async Task GetBackupProgressAsync() { return await _backupHandler.GetBackupProgressAsync(); } /// /// Returns the backup history of the started backup /// /// Backup /// Backup History [HttpGet("getbackuphistory")] public async Task> GetBackupHistory() { return await _backupHandler.GetBackupHistory(); } /// /// Delete the backup with the specified id /// /// Backup [HttpDelete("deletebackup/{id}")] public async Task DeleteBackup(Guid id) { await _backupHandler.DeleteBackupAsync(id); return true; } /// /// Delete all backups of the current portal /// /// Backup /// Backup History [HttpDelete("deletebackuphistory")] public async Task DeleteBackupHistory() { await _backupHandler.DeleteAllBackupsAsync(); return true; } /// /// Start a data restore of the current portal /// /// Backup Id /// Storage Type /// Storage Params /// Notify about backup to users /// Backup /// Restore Progress [HttpPost("startrestore")] public async Task StartBackupRestoreAsync(BackupRestoreDto backupRestore) { var storageParams = backupRestore.StorageParams == null ? new Dictionary() : backupRestore.StorageParams.ToDictionary(r => r.Key.ToString(), r => r.Value.ToString()); _eventBus.Publish(new BackupRestoreRequestIntegrationEvent( tenantId: _tenantId, createBy: _currentUserId, storageParams: storageParams, storageType: (BackupStorageType)Int32.Parse(backupRestore.StorageType.ToString()), notify: backupRestore.Notify, backupId: backupRestore.BackupId )); return await _backupHandler.GetBackupProgressAsync(); } /// /// Returns the progress of the started restore /// /// Backup /// Restore Progress [HttpGet("getrestoreprogress")] //NOTE: this method doesn't check payment!!! [AllowAnonymous] [AllowNotPayment] public async Task GetRestoreProgressAsync() { return await _backupHandler.GetRestoreProgressAsync(); } ///false [HttpGet("backuptmp")] public object GetTempPath() { return _backupHandler.GetTmpFolder(); } }