Skip to content

Commit

Permalink
Merge pull request #490 from ONLYOFFICE/feature/refactoring-backup-se…
Browse files Browse the repository at this point in the history
…rvice

Feature/refactoring backup service
  • Loading branch information
alexeybannov authored Jan 17, 2022
2 parents a764862 + ee8cb96 commit bcbabe6
Show file tree
Hide file tree
Showing 10 changed files with 375 additions and 479 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
using ASC.Common;
using ASC.Core;
using ASC.Data.Backup.Contracts;
using ASC.Data.Backup.ModelApi;
using ASC.Data.Backup.Models;
using ASC.Data.Backup.ApiModels;
using ASC.Web.Api.Routing;
using ASC.Web.Studio.Utility;

Expand All @@ -22,18 +21,18 @@ namespace ASC.Data.Backup.Controllers
[ApiController]
public class BackupController
{
private BackupAjaxHandler BackupHandler { get; }
private CoreBaseSettings CoreBaseSettings { get; }
private TenantExtra TenantExtra { get; }
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;
_backupHandler = backupAjaxHandler;
_coreBaseSettings = coreBaseSettings;
_tenantExtra = tenantExtra;
}
/// <summary>
/// Returns the backup schedule of the current portal
Expand All @@ -43,12 +42,12 @@ public BackupController(
[Read("getbackupschedule")]
public BackupAjaxHandler.Schedule GetBackupSchedule()
{
if (CoreBaseSettings.Standalone)
if (_coreBaseSettings.Standalone)
{
TenantExtra.DemandControlPanelPermission();
_tenantExtra.DemandControlPanelPermission();
}

return BackupHandler.GetSchedule();
return _backupHandler.GetSchedule();
}

/// <summary>
Expand All @@ -61,23 +60,23 @@ public BackupAjaxHandler.Schedule GetBackupSchedule()
/// <param name="backupMail">Include mail in the backup</param>
/// <category>Backup</category>
[Create("createbackupschedule")]
public bool CreateBackupScheduleFromBody([FromBody]BackupSchedule backupSchedule)
public bool CreateBackupScheduleFromBody([FromBody]BackupScheduleDto backupSchedule)
{
return CreateBackupSchedule(backupSchedule);
}

[Create("createbackupschedule")]
[Consumes("application/x-www-form-urlencoded")]
public bool CreateBackupScheduleFromForm([FromForm]BackupSchedule backupSchedule)
public bool CreateBackupScheduleFromForm([FromForm]BackupScheduleDto backupSchedule)
{
return CreateBackupSchedule(backupSchedule);
}

private bool CreateBackupSchedule(BackupSchedule backupSchedule)
private bool CreateBackupSchedule(BackupScheduleDto backupSchedule)
{
if (CoreBaseSettings.Standalone)
if (_coreBaseSettings.Standalone)
{
TenantExtra.DemandControlPanelPermission();
_tenantExtra.DemandControlPanelPermission();
}
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());
Expand All @@ -88,7 +87,7 @@ private bool CreateBackupSchedule(BackupSchedule backupSchedule)
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);
_backupHandler.CreateSchedule(storageType, storageParams, backupStored, cron, backupSchedule.BackupMail);
return true;
}

Expand All @@ -99,12 +98,12 @@ private bool CreateBackupSchedule(BackupSchedule backupSchedule)
[Delete("deletebackupschedule")]
public bool DeleteBackupSchedule()
{
if (CoreBaseSettings.Standalone)
if (_coreBaseSettings.Standalone)
{
TenantExtra.DemandControlPanelPermission();
_tenantExtra.DemandControlPanelPermission();
}

BackupHandler.DeleteSchedule();
_backupHandler.DeleteSchedule();

return true;
}
Expand All @@ -118,28 +117,28 @@ public bool DeleteBackupSchedule()
/// <category>Backup</category>
/// <returns>Backup Progress</returns>
[Create("startbackup")]
public BackupProgress StartBackupFromBody([FromBody]Models.Backup backup)
public BackupProgress StartBackupFromBody([FromBody]BackupDto backup)
{
return StartBackup(backup);
}

[Create("startbackup")]
[Consumes("application/x-www-form-urlencoded")]
public BackupProgress StartBackupFromForm([FromForm]Models.Backup backup)
public BackupProgress StartBackupFromForm([FromForm]BackupDto backup)
{
return StartBackup(backup);
}

private BackupProgress StartBackup(Models.Backup backup)
private BackupProgress StartBackup(BackupDto backup)
{
if (CoreBaseSettings.Standalone)
if (_coreBaseSettings.Standalone)
{
TenantExtra.DemandControlPanelPermission();
_tenantExtra.DemandControlPanelPermission();
}
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();
_backupHandler.StartBackup(storageType, storageParams, backup.BackupMail);
return _backupHandler.GetBackupProgress();
}

/// <summary>
Expand All @@ -150,12 +149,12 @@ private BackupProgress StartBackup(Models.Backup backup)
[Read("getbackupprogress")]
public BackupProgress GetBackupProgress()
{
if (CoreBaseSettings.Standalone)
if (_coreBaseSettings.Standalone)
{
TenantExtra.DemandControlPanelPermission();
_tenantExtra.DemandControlPanelPermission();
}

return BackupHandler.GetBackupProgress();
return _backupHandler.GetBackupProgress();
}

/// <summary>
Expand All @@ -166,12 +165,12 @@ public BackupProgress GetBackupProgress()
[Read("getbackuphistory")]
public List<BackupHistoryRecord> GetBackupHistory()
{
if (CoreBaseSettings.Standalone)
if (_coreBaseSettings.Standalone)
{
TenantExtra.DemandControlPanelPermission();
_tenantExtra.DemandControlPanelPermission();
}

return BackupHandler.GetBackupHistory();
return _backupHandler.GetBackupHistory();
}

/// <summary>
Expand All @@ -181,12 +180,12 @@ public List<BackupHistoryRecord> GetBackupHistory()
[Delete("deletebackup/{id}")]
public bool DeleteBackup(Guid id)
{
if (CoreBaseSettings.Standalone)
if (_coreBaseSettings.Standalone)
{
TenantExtra.DemandControlPanelPermission();
_tenantExtra.DemandControlPanelPermission();
}

BackupHandler.DeleteBackup(id);
_backupHandler.DeleteBackup(id);
return true;
}

Expand All @@ -198,12 +197,12 @@ public bool DeleteBackup(Guid id)
[Delete("deletebackuphistory")]
public bool DeleteBackupHistory()
{
if (CoreBaseSettings.Standalone)
if (_coreBaseSettings.Standalone)
{
TenantExtra.DemandControlPanelPermission();
_tenantExtra.DemandControlPanelPermission();
}

BackupHandler.DeleteAllBackups();
_backupHandler.DeleteAllBackups();
return true;
}

Expand All @@ -217,27 +216,27 @@ public bool DeleteBackupHistory()
/// <category>Backup</category>
/// <returns>Restore Progress</returns>
[Create("startrestore")]
public BackupProgress StartBackupRestoreFromBody([FromBody]BackupRestore backupRestore)
public BackupProgress StartBackupRestoreFromBody([FromBody]BackupRestoreDto backupRestore)
{
return StartBackupRestore(backupRestore);
}

[Create("startrestore")]
[Consumes("application/x-www-form-urlencoded")]
public BackupProgress StartBackupRestoreFromForm([FromForm]BackupRestore backupRestore)
public BackupProgress StartBackupRestoreFromForm([FromForm]BackupRestoreDto backupRestore)
{
return StartBackupRestore(backupRestore);
}

private BackupProgress StartBackupRestore(BackupRestore backupRestore)
private BackupProgress StartBackupRestore(BackupRestoreDto backupRestore)
{
if (CoreBaseSettings.Standalone)
if (_coreBaseSettings.Standalone)
{
TenantExtra.DemandControlPanelPermission();
_tenantExtra.DemandControlPanelPermission();
}
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();
_backupHandler.StartRestore(backupRestore.BackupId, (BackupStorageType)Int32.Parse(backupRestore.StorageType.ToString()), storageParams, backupRestore.Notify);
return _backupHandler.GetBackupProgress();
}

/// <summary>
Expand All @@ -248,24 +247,24 @@ private BackupProgress StartBackupRestore(BackupRestore backupRestore)
[Read("getrestoreprogress", true)] //NOTE: this method doesn't check payment!!!
public BackupProgress GetRestoreProgress()
{
if (CoreBaseSettings.Standalone)
if (_coreBaseSettings.Standalone)
{
TenantExtra.DemandControlPanelPermission();
_tenantExtra.DemandControlPanelPermission();
}

return BackupHandler.GetRestoreProgress();
return _backupHandler.GetRestoreProgress();
}

///<visible>false</visible>
[Read("backuptmp")]
public object GetTempPath()
{
if (CoreBaseSettings.Standalone)
if (_coreBaseSettings.Standalone)
{
TenantExtra.DemandControlPanelPermission();
_tenantExtra.DemandControlPanelPermission();
}

return BackupHandler.GetTmpFolder();
return _backupHandler.GetTmpFolder();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@

using ASC.Api.Collections;

namespace ASC.Data.Backup.Models
namespace ASC.Data.Backup.ApiModels
{
public class Backup
public class BackupDto
{

public string StorageType { get; set; }
public bool BackupMail { get; set; }
public IEnumerable<ItemKeyValuePair<object, object>> StorageParams { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

using ASC.Api.Collections;

namespace ASC.Data.Backup.Models
namespace ASC.Data.Backup.ApiModels
{
public class BackupRestore
public class BackupRestoreDto
{
public string BackupId { get; set; }
public object StorageType { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

using ASC.Api.Collections;

namespace ASC.Data.Backup.ModelApi
namespace ASC.Data.Backup.ApiModels
{
public class BackupSchedule
public class BackupScheduleDto
{
public string StorageType { get; set; }
public IEnumerable<ItemKeyValuePair<object, object>> StorageParams { get; set; }
Expand Down
Loading

0 comments on commit bcbabe6

Please sign in to comment.