143 lines
5.6 KiB
C#
143 lines
5.6 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Routing;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using AyaNova.Models;
|
|
using AyaNova.Api.ControllerHelpers;
|
|
using AyaNova.Biz;
|
|
using System.Threading.Tasks;
|
|
using AyaNova.Util;
|
|
using System;
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
|
|
|
|
namespace AyaNova.Api.Controllers
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
/// Backup
|
|
///
|
|
/// </summary>
|
|
[ApiController]
|
|
[ApiVersion("8.0")]
|
|
[Route("api/v{version:apiVersion}/backup")]
|
|
[Produces("application/json")]
|
|
[Authorize]
|
|
public class BackupController : ControllerBase
|
|
{
|
|
private readonly AyContext ct;
|
|
private readonly ILogger<BackupController> log;
|
|
private readonly ApiServerState serverState;
|
|
|
|
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="dbcontext"></param>
|
|
/// <param name="logger"></param>
|
|
/// <param name="apiServerState"></param>
|
|
public BackupController(AyContext dbcontext, ILogger<BackupController> logger, ApiServerState apiServerState)
|
|
{
|
|
ct = dbcontext;
|
|
log = logger;
|
|
serverState = apiServerState;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Trigger immediate system backup
|
|
///
|
|
/// </summary>
|
|
/// <returns>Job Id</returns>
|
|
[HttpPost("backup-now")]
|
|
[Authorize]
|
|
public async Task<IActionResult> PostServerState()
|
|
{
|
|
if (serverState.IsClosed)
|
|
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
|
if (!Authorized.HasAnyRole(HttpContext.Items, AuthorizationRoles.OpsAdminFull | AuthorizationRoles.OpsAdminLimited))
|
|
return StatusCode(403, new ApiNotAuthorizedResponse());
|
|
var JobName = $"Backup (on demand)";
|
|
OpsJob j = new OpsJob();
|
|
j.Name = JobName;
|
|
j.ObjectType = AyaType.NoType;
|
|
j.JobType = JobType.Backup;
|
|
j.SubType = JobSubType.NotSet;
|
|
j.Exclusive = true;
|
|
await JobsBiz.AddJobAsync(j, ct);
|
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserIdFromContext.Id(HttpContext.Items), 0, AyaType.ServerJob, AyaEvent.Created, JobName), ct);
|
|
return Accepted(new { JobId = j.GId });
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Get list of backup files
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet("list")]
|
|
public ActionResult ListBackupFiles()
|
|
{
|
|
if (serverState.IsClosed)
|
|
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
|
if (!Authorized.HasAnyRole(HttpContext.Items, AuthorizationRoles.OpsAdminFull | AuthorizationRoles.OpsAdminLimited))
|
|
return StatusCode(403, new ApiNotAuthorizedResponse());
|
|
return Ok(ApiOkResponse.Response(FileUtil.UtilityFileList()));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Download a backup file
|
|
/// </summary>
|
|
/// <param name="fileName"></param>
|
|
/// <param name="t">download token</param>
|
|
/// <returns></returns>
|
|
[HttpGet("download/{fileName}")]
|
|
public async Task<IActionResult> DownloadAsync([FromRoute] string fileName, [FromQuery] string t)
|
|
{
|
|
int nFailedAuthDelay = 3000;
|
|
if (!serverState.IsOpen)
|
|
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
|
if (string.IsNullOrWhiteSpace(t))
|
|
{
|
|
await Task.Delay(nFailedAuthDelay);//DOS protection
|
|
return StatusCode(401, new ApiErrorResponse(ApiErrorCode.AUTHENTICATION_FAILED));
|
|
}
|
|
var DownloadUser = await ct.User.AsNoTracking().SingleOrDefaultAsync(z => z.DlKey == t && z.Active == true);
|
|
if (DownloadUser == null)
|
|
{
|
|
await Task.Delay(nFailedAuthDelay);//DOS protection
|
|
return StatusCode(401, new ApiErrorResponse(ApiErrorCode.AUTHENTICATION_FAILED));
|
|
}
|
|
var utcNow = new DateTimeOffset(DateTime.Now.ToUniversalTime(), TimeSpan.Zero);
|
|
if (DownloadUser.DlKeyExpire < utcNow.DateTime)
|
|
{
|
|
await Task.Delay(nFailedAuthDelay);//DOS protection
|
|
return StatusCode(401, new ApiErrorResponse(ApiErrorCode.AUTHENTICATION_FAILED));
|
|
}
|
|
if (!Authorized.HasAnyRole(HttpContext.Items, AuthorizationRoles.OpsAdminFull | AuthorizationRoles.OpsAdminLimited))
|
|
{
|
|
await Task.Delay(nFailedAuthDelay);//DOS protection
|
|
return StatusCode(403, new ApiNotAuthorizedResponse());
|
|
}
|
|
|
|
var AvailableFiles = FileUtil.UtilityFileList();
|
|
if (!AvailableFiles.Contains(fileName))
|
|
{
|
|
await Task.Delay(nFailedAuthDelay);//fishing protection
|
|
return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
|
|
}
|
|
string mimetype = fileName.EndsWith("zip") ? "application/zip" : "application/octet-stream";
|
|
var utilityFilePath = FileUtil.GetFullPathForUtilityFile(fileName);
|
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(DownloadUser.Id, 0, AyaType.NoType, AyaEvent.UtilityFileDownload, fileName), ct);
|
|
return PhysicalFile(utilityFilePath, mimetype);
|
|
|
|
}
|
|
|
|
|
|
}//eoc
|
|
}//eons
|