namespace ASC.Data.Backup.Controllers;
[Scope]
[DefaultRoute]
[ApiController]
public class BackupController
{
private readonly BackupAjaxHandler _backupHandler;
private readonly CoreBaseSettings _coreBaseSettings;
private readonly TenantExtra _tenantExtra;
public BackupController(
BackupAjaxHandler backupAjaxHandler,
CoreBaseSettings coreBaseSettings,
TenantExtra tenantExtra)
{
_backupHandler = backupAjaxHandler;
_coreBaseSettings = coreBaseSettings;
_tenantExtra = tenantExtra;
}
///
/// Returns the backup schedule of the current portal
///
/// Backup
/// Backup Schedule
[Read("getbackupschedule")]
public BackupAjaxHandler.Schedule GetBackupSchedule()
{
if (_coreBaseSettings.Standalone)
{
_tenantExtra.DemandControlPanelPermission();
}
return _backupHandler.GetSchedule();
}
///
/// Create the backup schedule of the current portal
///
/// Storage type
/// Storage parameters
/// Max of the backup's stored copies
/// Cron parameters
/// Include mail in the backup
/// Backup
[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);
}
private bool CreateBackupSchedule(BackupScheduleDto backupSchedule)
{
if (_coreBaseSettings.Standalone)
{
_tenantExtra.DemandControlPanelPermission();
}
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),
};
_backupHandler.CreateSchedule(storageType, storageParams, backupStored, cron, backupSchedule.BackupMail);
return true;
}
///
/// Delete the backup schedule of the current portal
///
/// Backup
[Delete("deletebackupschedule")]
public bool DeleteBackupSchedule()
{
if (_coreBaseSettings.Standalone)
{
_tenantExtra.DemandControlPanelPermission();
}
_backupHandler.DeleteSchedule();
return true;
}
///
/// Start a backup of the current portal
///
/// Storage Type
/// Storage Params
/// Include mail in the backup
/// Backup
/// Backup Progress
[Create("startbackup")]
public BackupProgress StartBackupFromBody([FromBody] BackupDto backup)
{
return StartBackup(backup);
}
[Create("startbackup")]
[Consumes("application/x-www-form-urlencoded")]
public BackupProgress StartBackupFromForm([FromForm] BackupDto backup)
{
return StartBackup(backup);
}
private BackupProgress StartBackup(BackupDto backup)
{
if (_coreBaseSettings.Standalone)
{
_tenantExtra.DemandControlPanelPermission();
}
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());
_backupHandler.StartBackup(storageType, storageParams, backup.BackupMail);
return _backupHandler.GetBackupProgress();
}
///
/// Returns the progress of the started backup
///
/// Backup
/// Backup Progress
[Read("getbackupprogress")]
public BackupProgress GetBackupProgress()
{
if (_coreBaseSettings.Standalone)
{
_tenantExtra.DemandControlPanelPermission();
}
return _backupHandler.GetBackupProgress();
}
///
/// Returns the backup history of the started backup
///
/// Backup
/// Backup History
[Read("getbackuphistory")]
public List GetBackupHistory()
{
if (_coreBaseSettings.Standalone)
{
_tenantExtra.DemandControlPanelPermission();
}
return _backupHandler.GetBackupHistory();
}
///
/// Delete the backup with the specified id
///
/// Backup
[Delete("deletebackup/{id}")]
public bool DeleteBackup(Guid id)
{
if (_coreBaseSettings.Standalone)
{
_tenantExtra.DemandControlPanelPermission();
}
_backupHandler.DeleteBackup(id);
return true;
}
///
/// Delete all backups of the current portal
///
/// Backup
/// Backup History
[Delete("deletebackuphistory")]
public bool DeleteBackupHistory()
{
if (_coreBaseSettings.Standalone)
{
_tenantExtra.DemandControlPanelPermission();
}
_backupHandler.DeleteAllBackups();
return true;
}
///
/// Start a data restore of the current portal
///
/// Backup Id
/// Storage Type
/// Storage Params
/// Notify about backup to users
/// Backup
/// Restore Progress
[Create("startrestore")]
public BackupProgress StartBackupRestoreFromBody([FromBody] BackupRestoreDto backupRestore)
{
return StartBackupRestore(backupRestore);
}
[Create("startrestore")]
[Consumes("application/x-www-form-urlencoded")]
public BackupProgress StartBackupRestoreFromForm([FromForm] BackupRestoreDto backupRestore)
{
return StartBackupRestore(backupRestore);
}
private BackupProgress StartBackupRestore(BackupRestoreDto backupRestore)
{
if (_coreBaseSettings.Standalone)
{
_tenantExtra.DemandControlPanelPermission();
}
var storageParams = backupRestore.StorageParams == null ? new Dictionary() : 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();
}
///
/// Returns the progress of the started restore
///
/// Backup
/// Restore Progress
[Read("getrestoreprogress", true)] //NOTE: this method doesn't check payment!!!
public BackupProgress GetRestoreProgress()
{
if (_coreBaseSettings.Standalone)
{
_tenantExtra.DemandControlPanelPermission();
}
return _backupHandler.GetRestoreProgress();
}
///false
[Read("backuptmp")]
public object GetTempPath()
{
if (_coreBaseSettings.Standalone)
{
_tenantExtra.DemandControlPanelPermission();
}
return _backupHandler.GetTmpFolder();
}
}