save annotation data as json and sent to server

This commit is contained in:
Jonathan Jenne 2023-10-09 13:05:31 +02:00
parent 8c629691d9
commit dfadcff710
5 changed files with 75 additions and 22 deletions

View File

@ -5,6 +5,7 @@ using EnvelopeGenerator.Web.Services;
using Microsoft.Extensions.Primitives;
using System.IO.Pipelines;
using System.Reflection.Metadata.Ecma335;
using System.Text;
namespace EnvelopeGenerator.Web.Handler
{
@ -128,6 +129,13 @@ namespace EnvelopeGenerator.Web.Handler
int documentId = EnsureValidDocumentIndex(logger, ctx.Request);
var document = GetDocument(r.Envelope, documentId);
var annotationData = EnsureValidAnnotationData(logger, ctx.Request);
if (annotationData == null)
{
throw new ArgumentNullException("AnnotationData");
}
// TODO: Save annotations to database
return Results.Ok();
@ -182,6 +190,26 @@ namespace EnvelopeGenerator.Web.Handler
return envelopeKey;
}
private static async Task<string> EnsureValidAnnotationData(Logger logger, HttpRequest request)
{
logger.Debug("Parsing AnnotationData..");
try
{
using MemoryStream ms = new();
await request.BodyReader.CopyToAsync(ms);
var bytes = ms.ToArray();
return Encoding.UTF8.GetString(bytes);
}
catch (Exception e)
{
logger.Error(e);
return null;
}
}
private static EnvelopeDocument GetDocument(Common.Envelope envelope, int documentId)
{
var document = envelope.Documents.

View File

@ -36,9 +36,9 @@ app.UseRouting();
// Add file download endpoint
app.MapGet("/api/document/{envelopeKey}", FileHandler.HandleGetDocument);
app.MapPost("/api/document/{envelopeKey}/{documentId}", FileHandler.HandlePostDocument);
app.MapPost("/api/document/{envelopeKey}", FileHandler.HandlePostDocument);
app.MapGet("/api/envelope/{envelopeKey}", FileHandler.HandleGetEnvelope);
app.MapPost("/api/envelope/{envelopeKey}/{documentId}", FileHandler.HandlePostEnvelope);
app.MapPost("/api/envelope/{envelopeKey}", FileHandler.HandlePostEnvelope);
// Blazor plumbing
app.MapBlazorHub();

View File

@ -71,15 +71,21 @@ export class App {
}
public static async handleFinish(event: any) {
await App.Instance.save();
// Export annotation data and save to database
const json = await App.Instance.exportInstantJSON()
console.log(json);
console.log(JSON.stringify(json));
const result = await App.Network.postEnvelope(App.envelopeKey, App.currentDocument.id, JSON.stringify(json))
// Flatten the annotations and save the document to disk
/*
const buffer = await App.Instance.exportPDF({ flatten: true });
const result = await App.Network.postDocument(App.envelopeKey, App.currentDocument.id, buffer);
console.log(result)
*/
}
public static async handleReset(event: any) {
@ -196,11 +202,18 @@ class Network {
.then(res => res.json());
}
public postEnvelope(envelopeKey: string, documentId: number, jsonString: string): Promise<any> {
const options: RequestInit = {
credentials: "include",
method: "POST",
body: jsonString
}
public postEnvelope(envelopeKey: string, documentId: number, buffer: ArrayBuffer): Promise<any> {
return fetch(`/api/envelope/${envelopeKey}/${documentId}`, { credentials: "include", method: "POST", body: buffer })
.then(res => res.json());
return fetch(`/api/envelope/${envelopeKey}?index=${documentId}`, options)
.then(res => {
console.log(res)
res.json()
});
}
}

View File

@ -122,7 +122,7 @@ var App = /** @class */ (function () {
};
App.handleFinish = function (event) {
return __awaiter(this, void 0, void 0, function () {
var json, buffer, result;
var json, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, App.Instance.save()];
@ -132,13 +132,17 @@ var App = /** @class */ (function () {
case 2:
json = _a.sent();
console.log(json);
return [4 /*yield*/, App.Instance.exportPDF({ flatten: true })];
console.log(JSON.stringify(json));
return [4 /*yield*/, App.Network.postEnvelope(App.envelopeKey, App.currentDocument.id, JSON.stringify(json))
// Flatten the annotations and save the document to disk
/*
const buffer = await App.Instance.exportPDF({ flatten: true });
const result = await App.Network.postDocument(App.envelopeKey, App.currentDocument.id, buffer);
console.log(result)
*/
];
case 3:
buffer = _a.sent();
return [4 /*yield*/, App.Network.postDocument(App.envelopeKey, App.currentDocument.id, buffer)];
case 4:
result = _a.sent();
console.log(result);
return [2 /*return*/];
}
});
@ -148,7 +152,7 @@ var App = /** @class */ (function () {
return __awaiter(this, void 0, void 0, function () {
var result;
return __generator(this, function (_a) {
if (confirm("Wollen Sie das Dokument und alle erstellten Signaturen zurücksetzen?")) {
if (confirm("Wollen Sie das Dokument und alle erstellten Signaturen zur<EFBFBD>cksetzen?")) {
result = App.Annotation.deleteAnnotations(App.Instance);
}
return [2 /*return*/];
@ -275,9 +279,17 @@ var Network = /** @class */ (function () {
return fetch("/api/document/".concat(envelopeKey, "/").concat(documentId), { credentials: "include", method: "POST", body: buffer })
.then(function (res) { return res.json(); });
};
Network.prototype.postEnvelope = function (envelopeKey, documentId, buffer) {
return fetch("/api/envelope/".concat(envelopeKey, "/").concat(documentId), { credentials: "include", method: "POST", body: buffer })
.then(function (res) { return res.json(); });
Network.prototype.postEnvelope = function (envelopeKey, documentId, jsonString) {
var options = {
credentials: "include",
method: "POST",
body: jsonString
};
return fetch("/api/envelope/".concat(envelopeKey, "?index=").concat(documentId), options)
.then(function (res) {
console.log(res);
res.json();
});
};
return Network;
}());
@ -300,7 +312,7 @@ var UI = /** @class */ (function () {
{
type: "custom",
id: "button-reset",
title: "Zurücksetzen",
title: "Zur<EFBFBD>cksetzen",
onPress: function () {
callback("RESET");
},
@ -309,7 +321,7 @@ var UI = /** @class */ (function () {
{
type: "custom",
id: "button-finish",
title: "Abschließen",
title: "Abschlie<EFBFBD>en",
onPress: function () {
callback("FINISH");
},

File diff suppressed because one or more lines are too long