DocSpace-buildtools/common/services/ASC.Data.Backup/Api/BackupController.cs

274 lines
9.0 KiB
C#
Raw Normal View History

2022-01-19 16:25:10 +00:00
namespace ASC.Data.Backup.Controllers;
2021-08-31 09:40:28 +00:00
2022-01-19 16:25:10 +00:00
[Scope]
[DefaultRoute]
[ApiController]
public class BackupController
2021-08-31 09:40:28 +00:00
{
2022-01-19 16:25:10 +00:00
private readonly BackupAjaxHandler _backupHandler;
private readonly CoreBaseSettings _coreBaseSettings;
private readonly TenantExtra _tenantExtra;
public BackupController(
BackupAjaxHandler backupAjaxHandler,
CoreBaseSettings coreBaseSettings,
TenantExtra tenantExtra)
2021-08-31 09:40:28 +00:00
{
2022-01-19 16:25:10 +00:00
_backupHandler = backupAjaxHandler;
_coreBaseSettings = coreBaseSettings;
_tenantExtra = tenantExtra;
}
/// <summary>
/// Returns the backup schedule of the current portal
/// </summary>
/// <category>Backup</category>
/// <returns>Backup Schedule</returns>
[Read("getbackupschedule")]
public BackupAjaxHandler.Schedule GetBackupSchedule()
{
if (_coreBaseSettings.Standalone)
2021-08-31 09:40:28 +00:00
{
2022-01-19 16:25:10 +00:00
_tenantExtra.DemandControlPanelPermission();
2021-08-31 09:40:28 +00:00
}
2022-01-19 16:25:10 +00:00
return _backupHandler.GetSchedule();
}
2021-08-31 09:40:28 +00:00
2022-01-19 16:25:10 +00:00
/// <summary>
/// Create the backup schedule of the current portal
/// </summary>
/// <param name="storageType">Storage type</param>
/// <param name="storageParams">Storage parameters</param>
/// <param name="backupsStored">Max of the backup's stored copies</param>
/// <param name="cronParams">Cron parameters</param>
/// <param name="backupMail">Include mail in the backup</param>
/// <category>Backup</category>
[Create("createbackupschedule")]
public bool CreateBackupScheduleFromBody([FromBody] BackupScheduleDto backupSchedule)
{
return CreateBackupSchedule(backupSchedule);
}
[Create("createbackupschedule")]
[Consumes("application/x-www-form-urlencoded")]
public bool CreateBackupScheduleFromForm([FromForm] BackupScheduleDto backupSchedule)
{
return CreateBackupSchedule(backupSchedule);
}
2021-08-31 09:40:28 +00:00
2022-01-19 16:25:10 +00:00
private bool CreateBackupSchedule(BackupScheduleDto backupSchedule)
{
if (_coreBaseSettings.Standalone)
2021-08-31 09:40:28 +00:00
{
2022-01-19 16:25:10 +00:00
_tenantExtra.DemandControlPanelPermission();
2021-08-31 09:40:28 +00:00
}
2022-01-19 16:25:10 +00:00
var storageType = backupSchedule.StorageType == null ? BackupStorageType.Documents : (BackupStorageType)Int32.Parse(backupSchedule.StorageType);
var storageParams = backupSchedule.StorageParams == null ? new Dictionary<string, string>() : 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),
};
_backupHandler.CreateSchedule(storageType, storageParams, backupStored, cron, backupSchedule.BackupMail);
return true;
}
2021-08-31 09:40:28 +00:00
2022-01-19 16:25:10 +00:00
/// <summary>
/// Delete the backup schedule of the current portal
/// </summary>
/// <category>Backup</category>
[Delete("deletebackupschedule")]
public bool DeleteBackupSchedule()
{
if (_coreBaseSettings.Standalone)
2021-08-31 09:40:28 +00:00
{
2022-01-19 16:25:10 +00:00
_tenantExtra.DemandControlPanelPermission();
2021-08-31 09:40:28 +00:00
}
2022-01-19 16:25:10 +00:00
_backupHandler.DeleteSchedule();
2021-08-31 09:40:28 +00:00
2022-01-19 16:25:10 +00:00
return true;
}
2021-08-31 09:40:28 +00:00
2022-01-19 16:25:10 +00:00
/// <summary>
/// Start a backup of the current portal
/// </summary>
/// <param name="storageType">Storage Type</param>
/// <param name="storageParams">Storage Params</param>
/// <param name="backupMail">Include mail in the backup</param>
/// <category>Backup</category>
/// <returns>Backup Progress</returns>
[Create("startbackup")]
public BackupProgress StartBackupFromBody([FromBody] BackupDto backup)
{
return StartBackup(backup);
}
2021-08-31 09:40:28 +00:00
2022-01-19 16:25:10 +00:00
[Create("startbackup")]
[Consumes("application/x-www-form-urlencoded")]
public BackupProgress StartBackupFromForm([FromForm] BackupDto backup)
{
return StartBackup(backup);
}
2021-08-31 09:40:28 +00:00
2022-01-19 16:25:10 +00:00
private BackupProgress StartBackup(BackupDto backup)
{
if (_coreBaseSettings.Standalone)
2021-08-31 09:40:28 +00:00
{
2022-01-19 16:25:10 +00:00
_tenantExtra.DemandControlPanelPermission();
2021-08-31 09:40:28 +00:00
}
2022-01-19 16:25:10 +00:00
var storageType = backup.StorageType == null ? BackupStorageType.Documents : (BackupStorageType)Int32.Parse(backup.StorageType);
var storageParams = backup.StorageParams == null ? new Dictionary<string, string>() : backup.StorageParams.ToDictionary(r => r.Key.ToString(), r => r.Value.ToString());
_backupHandler.StartBackup(storageType, storageParams, backup.BackupMail);
return _backupHandler.GetBackupProgress();
}
2021-08-31 09:40:28 +00:00
2022-01-19 16:25:10 +00:00
/// <summary>
/// Returns the progress of the started backup
/// </summary>
/// <category>Backup</category>
/// <returns>Backup Progress</returns>
[Read("getbackupprogress")]
public BackupProgress GetBackupProgress()
{
if (_coreBaseSettings.Standalone)
2021-08-31 09:40:28 +00:00
{
2022-01-19 16:25:10 +00:00
_tenantExtra.DemandControlPanelPermission();
2021-08-31 09:40:28 +00:00
}
2022-01-19 16:25:10 +00:00
return _backupHandler.GetBackupProgress();
}
2021-08-31 09:40:28 +00:00
2022-01-19 16:25:10 +00:00
/// <summary>
/// Returns the backup history of the started backup
/// </summary>
/// <category>Backup</category>
/// <returns>Backup History</returns>
[Read("getbackuphistory")]
public List<BackupHistoryRecord> GetBackupHistory()
{
if (_coreBaseSettings.Standalone)
{
_tenantExtra.DemandControlPanelPermission();
2021-08-31 09:40:28 +00:00
}
2022-01-19 16:25:10 +00:00
return _backupHandler.GetBackupHistory();
}
2021-08-31 09:40:28 +00:00
2022-01-19 16:25:10 +00:00
/// <summary>
/// Delete the backup with the specified id
/// </summary>
/// <category>Backup</category>
[Delete("deletebackup/{id}")]
public bool DeleteBackup(Guid id)
{
if (_coreBaseSettings.Standalone)
{
_tenantExtra.DemandControlPanelPermission();
2021-08-31 09:40:28 +00:00
}
2022-01-19 16:25:10 +00:00
_backupHandler.DeleteBackup(id);
return true;
}
2021-08-31 09:40:28 +00:00
2022-01-19 16:25:10 +00:00
/// <summary>
/// Delete all backups of the current portal
/// </summary>
/// <category>Backup</category>
/// <returns>Backup History</returns>
[Delete("deletebackuphistory")]
public bool DeleteBackupHistory()
{
if (_coreBaseSettings.Standalone)
{
_tenantExtra.DemandControlPanelPermission();
2021-08-31 09:40:28 +00:00
}
2022-01-19 16:25:10 +00:00
_backupHandler.DeleteAllBackups();
return true;
}
2021-08-31 09:40:28 +00:00
2022-01-19 16:25:10 +00:00
/// <summary>
/// Start a data restore of the current portal
/// </summary>
/// <param name="backupId">Backup Id</param>
/// <param name="storageType">Storage Type</param>
/// <param name="storageParams">Storage Params</param>
/// <param name="notify">Notify about backup to users</param>
/// <category>Backup</category>
/// <returns>Restore Progress</returns>
[Create("startrestore")]
public BackupProgress StartBackupRestoreFromBody([FromBody] BackupRestoreDto backupRestore)
{
return StartBackupRestore(backupRestore);
}
2021-08-31 09:40:28 +00:00
2022-01-19 16:25:10 +00:00
[Create("startrestore")]
[Consumes("application/x-www-form-urlencoded")]
public BackupProgress StartBackupRestoreFromForm([FromForm] BackupRestoreDto backupRestore)
{
return StartBackupRestore(backupRestore);
}
2021-08-31 09:40:28 +00:00
2022-01-19 16:25:10 +00:00
private BackupProgress StartBackupRestore(BackupRestoreDto backupRestore)
{
if (_coreBaseSettings.Standalone)
2021-08-31 09:40:28 +00:00
{
2022-01-19 16:25:10 +00:00
_tenantExtra.DemandControlPanelPermission();
2021-08-31 09:40:28 +00:00
}
2022-01-19 16:25:10 +00:00
var storageParams = backupRestore.StorageParams == null ? new Dictionary<string, string>() : backupRestore.StorageParams.ToDictionary(r => r.Key.ToString(), r => r.Value.ToString());
_backupHandler.StartRestore(backupRestore.BackupId, (BackupStorageType)Int32.Parse(backupRestore.StorageType.ToString()), storageParams, backupRestore.Notify);
return _backupHandler.GetBackupProgress();
}
2021-08-31 09:40:28 +00:00
2022-01-19 16:25:10 +00:00
/// <summary>
/// Returns the progress of the started restore
/// </summary>
/// <category>Backup</category>
/// <returns>Restore Progress</returns>
[Read("getrestoreprogress", true)] //NOTE: this method doesn't check payment!!!
public BackupProgress GetRestoreProgress()
{
if (_coreBaseSettings.Standalone)
2021-08-31 09:40:28 +00:00
{
2022-01-19 16:25:10 +00:00
_tenantExtra.DemandControlPanelPermission();
2021-08-31 09:40:28 +00:00
}
2022-01-19 16:25:10 +00:00
return _backupHandler.GetRestoreProgress();
}
2021-08-31 09:40:28 +00:00
2022-01-19 16:25:10 +00:00
///<visible>false</visible>
[Read("backuptmp")]
public object GetTempPath()
{
if (_coreBaseSettings.Standalone)
2021-08-31 09:40:28 +00:00
{
2022-01-19 16:25:10 +00:00
_tenantExtra.DemandControlPanelPermission();
2021-08-31 09:40:28 +00:00
}
2022-01-19 16:25:10 +00:00
return _backupHandler.GetTmpFolder();
2021-08-31 09:40:28 +00:00
}
///<visible>false</visible>
[Read("enablerestore")]
public bool EnableRestore()
{
try
{
if (CoreBaseSettings.Standalone)
{
TenantExtra.DemandControlPanelPermission();
}
BackupHandler.DemandPermissionsRestore();
return true;
}
catch
{
return false;
}
}
2021-08-31 09:40:28 +00:00
}