-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
60 lines (51 loc) · 1.56 KB
/
Copy pathProgram.cs
File metadata and controls
60 lines (51 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System.Text.Json;
using System.Text.Json.Serialization;
using DocxodusService.Models;
using Docxodus;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
var jsonOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
WriteIndented = false,
};
app.MapGet("/health", () => Results.Ok(new { status = "healthy" }));
app.MapPost("/parse", (ParseRequest? request) =>
{
if (request is null || string.IsNullOrWhiteSpace(request.DocxBase64))
{
return Results.BadRequest(new { error = "docx_base64 is required and must not be empty." });
}
byte[] docxBytes;
try
{
docxBytes = Convert.FromBase64String(request.DocxBase64);
}
catch (FormatException)
{
return Results.BadRequest(new { error = "docx_base64 is not valid Base64." });
}
if (docxBytes.Length == 0)
{
return Results.BadRequest(new { error = "Decoded DOCX content is empty." });
}
try
{
var filename = request.Filename ?? "document.docx";
var wmlDoc = new WmlDocument(filename, docxBytes);
var export = OpenContractExporter.Export(wmlDoc);
return Results.Json(export, jsonOptions);
}
catch (Exception ex)
{
return Results.Problem(
detail: ex.Message,
statusCode: 422,
title: "Failed to parse DOCX"
);
}
});
app.Run();
// Make the implicit Program class visible to test projects
public partial class Program { }