using System;
using System.Collections.Generic;
using ASC.Api.Core.Middleware;
using ASC.Common;
using ASC.Core;
using ASC.Data.Backup.Contracts;
using ASC.Data.Backup.Models;
using ASC.Web.Api.Routing;
using ASC.Web.Studio.Utility;
using Microsoft.AspNetCore.Mvc;
namespace ASC.Data.Backup.Controllers
{
[DefaultRoute]
[ApiController]
public class BackupController
{
private BackupAjaxHandler BackupHandler { get; }
private CoreBaseSettings CoreBaseSettings { get; }
private TenantExtra TenantExtra { get; }
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 void CreateBackupSchedule(BackupStorageType storageType, [FromQuery] Dictionary storageParams, int backupsStored, [FromBody] BackupAjaxHandler.CronParams cronParams, bool backupMail)
{
if (CoreBaseSettings.Standalone)
{
TenantExtra.DemandControlPanelPermission();
}
BackupHandler.CreateSchedule(storageType, storageParams, backupsStored, cronParams, backupMail);
}
///
/// Delete the backup schedule of the current portal
///
/// Backup
[Delete("deletebackupschedule")]
public void DeleteBackupSchedule()
{
if (CoreBaseSettings.Standalone)
{
TenantExtra.DemandControlPanelPermission();
}
BackupHandler.DeleteSchedule();
}
///
/// Start a backup of the current portal
///
/// Storage Type
/// Storage Params
/// Include mail in the backup
/// Backup
/// Backup Progress
[Create("startbackup")]
public BackupProgress StartBackup(Models.Backup backup)
{
if (CoreBaseSettings.Standalone)
{
TenantExtra.DemandControlPanelPermission();
}
BackupHandler.StartBackup(backup.StorageType, backup.StorageParams ?? new Dictionary(), 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 void DeleteBackup(Guid id)
{
if (CoreBaseSettings.Standalone)
{
TenantExtra.DemandControlPanelPermission();
}
BackupHandler.DeleteBackup(id);
}
///
/// Delete all backups of the current portal
///
/// Backup
/// Backup History
[Delete("deletebackuphistory")]
public void DeleteBackupHistory()
{
if (CoreBaseSettings.Standalone)
{
TenantExtra.DemandControlPanelPermission();
}
BackupHandler.DeleteAllBackups();
}
///
/// 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 StartBackupRestore(BackupRestore backupRestore)
{
if (CoreBaseSettings.Standalone)
{
TenantExtra.DemandControlPanelPermission();
}
BackupHandler.StartRestore(backupRestore.BackupId, backupRestore.StorageType, backupRestore.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 string GetTempPath()
{
if (CoreBaseSettings.Standalone)
{
TenantExtra.DemandControlPanelPermission();
}
return BackupHandler.GetTmpFolder();
}
}
public static class BackupControllerExtension
{
public static DIHelper AddBackupController(this DIHelper services)
{
return services
.AddBackupAjaxHandler()
.AddIpSecurityFilter()
.AddCoreBaseSettingsService()
.AddTenantExtraService();
}
}
}