21 lines
5.3 KiB
C#
21 lines
5.3 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using PlotLine.Services;
|
|
using PlotLine.Data;
|
|
using PlotLine.ViewModels;
|
|
namespace PlotLine.Controllers;
|
|
|
|
[Authorize]
|
|
public sealed class BookRecoveryController(BookRecoveryService recovery, BookRecoveryRepository repository, BookRecoveryUploads uploads, ICurrentUserService current) : Controller
|
|
{
|
|
private int UserID => current.UserId ?? throw new UnauthorizedAccessException();
|
|
[HttpGet] public async Task<IActionResult> Backup(int projectId = 0, int? bookId = null) { try { return View(new BookRecoveryPage { Projects = await repository.Projects(UserID), ProjectID = projectId, BookID = bookId, Source = projectId > 0 ? await recovery.Source(projectId) : null }); } catch (UnauthorizedAccessException) { return NotFound(); } }
|
|
[HttpPost, ValidateAntiForgeryToken] public async Task<IActionResult> Download(int projectId, int[] books, Guid? staged = null) { try { var package = staged.HasValue ? uploads.Get(UserID, staged.Value) : await recovery.Export(projectId, books); await repository.Mappings(package.Snapshot.Project!.ProjectID, UserID); var title = string.Concat(package.Snapshot.Project!.ProjectName.Select(c => char.IsLetterOrDigit(c) || c is '-' or '_' ? c : '_')); return File(System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(package), "application/json", $"{title}_books_{DateTime.UtcNow:yyyy-MM-dd_HHmmss}.plotdirector"); } catch (UnauthorizedAccessException) { return NotFound(); } catch (Exception e) when (e is ArgumentException or InvalidOperationException or System.IO.IOException) { var model = new BookRecoveryPage { Projects = await repository.Projects(UserID), ProjectID = projectId, Source = await recovery.Source(projectId), Error = e.Message }; return View("Backup", model); } }
|
|
[HttpPost, ValidateAntiForgeryToken] public async Task<IActionResult> ReviewBackup(int projectId, int[] books) { try { var generation=uploads.Begin(UserID);var package = await recovery.Export(projectId, books); var id = uploads.Add(UserID, System.Text.Json.JsonSerializer.Serialize(package),generation:generation); return View("BackupPreview", new BookRecoveryPage { ProjectID = projectId, Package = package, Upload = id }); } catch (UnauthorizedAccessException) { return NotFound(); } catch (Exception e) when (e is ArgumentException or InvalidOperationException or IOException) { return View("Backup", new BookRecoveryPage { ProjectID = projectId, Source = await recovery.Source(projectId), Projects = await repository.Projects(UserID), Error = e.Message }); } }
|
|
[HttpGet] public IActionResult Restore() => View(new BookRecoveryPage());
|
|
[HttpPost, ValidateAntiForgeryToken, RequestSizeLimit(80 * 1024 * 1024), RequestFormLimits(MultipartBodyLengthLimit = 80 * 1024 * 1024)]
|
|
public async Task<IActionResult> Upload(IFormFile? backup) { try { if (backup == null || backup.Length is < 1 or > 64 * 1024 * 1024) throw new ArgumentException("Choose a nonempty .plotdirector or compatible JSON backup up to 64 MiB."); var generation=uploads.Begin(UserID);using var reader = new StreamReader(backup.OpenReadStream(), System.Text.Encoding.UTF8, true); var text = await reader.ReadToEndAsync(HttpContext.RequestAborted); var package = BookRecoveryService.Read(text); var id = uploads.Add(UserID, text, backup.FileName, generation); return View("Restore", new BookRecoveryPage { Upload = id, UploadName=uploads.Name(UserID,id), Package = package, Projects = await repository.Projects(UserID) }); } catch(UnauthorizedAccessException){return View("Restore",new BookRecoveryPage{Error="A newer upload replaced this request. Choose your latest file again."});} catch (Exception e) when (e is ArgumentException or System.Text.Json.JsonException or System.IO.InvalidDataException) { return View("Restore", new BookRecoveryPage { Error = "Backup validation failed. " + e.Message }); } }
|
|
[HttpPost, ValidateAntiForgeryToken] public async Task<IActionResult> Preview(Guid upload, int[] books, int destination, string name) { try { var package = uploads.Get(UserID, upload); return View(new BookRecoveryPage { Upload = upload, UploadName=uploads.Name(UserID,upload), Package = package, Preview = await recovery.Preview(upload, package, books, destination, name) }); } catch (UnauthorizedAccessException) { return NotFound(); } catch (Exception e) when (e is ArgumentException or InvalidOperationException) { return View("Restore", new BookRecoveryPage { Error = e.Message + " Choose the backup again to start a new preview." }); } }
|
|
[HttpPost, ValidateAntiForgeryToken] public async Task<IActionResult> Confirm(string confirmation, bool confirm = false) { if (!confirm) return BadRequest("Confirm the preview before restoring."); try { var receipt = await recovery.Restore(confirmation, id => uploads.Get(UserID, id)); return View("Complete", receipt); } catch (UnauthorizedAccessException) { return NotFound(); } catch (Exception e) when (e is ArgumentException or InvalidOperationException or Microsoft.Data.SqlClient.SqlException or System.Security.Cryptography.CryptographicException or System.Transactions.TransactionException or IOException) { return View("Restore", new BookRecoveryPage { RetryConfirmation=confirmation,Error = "The restore result could not be confirmed. Existing books were not overwritten. " + (e is ArgumentException ? e.Message : "Retry the original confirmation below before starting another restore; contact support if it continues.") }); } }
|
|
}
|