-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocumentEditorSessionsController.cs
More file actions
149 lines (126 loc) · 5.77 KB
/
DocumentEditorSessionsController.cs
File metadata and controls
149 lines (126 loc) · 5.77 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//-------------------------------------------------------------------------------------------------------------
// module: TXTextControl.DocumentServices.SamplePlugin
// copyright: © 2026 Text Control GmbH
// author: T. Kummerow
//-------------------------------------------------------------------------------------------------------------
using Microsoft.AspNetCore.Mvc;
using TXTextControl.DocumentServices.DocumentEditor.Abstractions;
using TXTextControl.DocumentServices.DocumentEditor.Enums;
using TXTextControl.DocumentServices.DocumentEditor.Options;
using TXTextControl.DocumentServices.Plugin.Abstractions.DocumentEditor.Models;
namespace TXTextControl.DocumentServices.SamplePlugin.Controllers;
[ApiController]
[Route("plugin/document-editor-sessions/[action]")]
public class DocumentEditorSessionsController : ControllerBase {
private readonly IDocumentEditorSessionService m_sessionService;
public DocumentEditorSessionsController(IDocumentEditorSessionService sessionService) {
m_sessionService = sessionService;
}
[HttpPost]
public async Task<IActionResult> LoadDocument(
[FromQuery] string connectionId,
[FromBody] LoadDocumentRequest request) {
if (!TryGetSession(connectionId, out var session, out var notFoundResult)) {
return notFoundResult;
}
if (!TryDecodeBase64(request.Document, out var documentBytes, out var badRequest)) {
return badRequest;
}
if (!Enum.TryParse<DocumentFormat>(request.Format, true, out var documentFormat)) {
return BadRequest($"Unknown document format '{request.Format}'.");
}
LoadOptions loadOptions = LoadOptions.FromDocumentFormat(documentFormat, documentBytes);
await session.LoadAsync(loadOptions, HttpContext.RequestAborted);
return Ok(new {
ConnectionId = connectionId,
Message = $"Loaded a {documentFormat} document into the editor session."
});
}
[HttpPost]
public async Task<IActionResult> SetParagraphBackColor(
[FromQuery] string connectionId,
[FromQuery] string color = "#FFF59D") {
if (!TryGetSession(connectionId, out var session, out var notFoundResult)) {
return notFoundResult;
}
await session.Selection.ParagraphFormat.SetBackColorAsync(color);
return Ok(new {
ConnectionId = connectionId,
Message = $"Updated the current paragraph background color to '{color}'."
});
}
[HttpGet]
public async Task<IActionResult> GetCurrentApplicationField(
[FromQuery] string connectionId) {
if (!TryGetSession(connectionId, out var session, out var notFoundResult)) {
return notFoundResult;
}
IApplicationField? applicationField = await session.ApplicationFields.GetItemAsync();
if (applicationField is null) {
return NotFound("No application field is available at the current input position.");
}
return Ok(new {
ConnectionId = connectionId,
Name = await applicationField.GetNameAsync(),
TypeName = await applicationField.GetTypeNameAsync(),
Text = await applicationField.GetTextAsync()
});
}
[HttpGet]
public async Task<IActionResult> SaveDocument(
[FromQuery] string connectionId,
[FromQuery] string format = "TX") {
if (!TryGetSession(connectionId, out var session, out var notFoundResult)) {
return notFoundResult;
}
if (!Enum.TryParse<DocumentFormat>(format, true, out var documentFormat)) {
return BadRequest($"Unknown document format '{format}'.");
}
SavedDocument savedDocument = await session.SaveAsync(new SaveOptions(documentFormat), HttpContext.RequestAborted);
return File(savedDocument.Content, GetContentType(savedDocument.Format), GetFileName("session-document", savedDocument.Format));
}
private bool TryGetSession(
string connectionId,
out IDocumentEditorSession session,
out NotFoundObjectResult notFoundResult) {
if (m_sessionService.TryGetSession(connectionId, out session!)) {
notFoundResult = null!;
return true;
}
session = null!;
notFoundResult = NotFound($"No editor session was found for connection ID '{connectionId}'.");
return false;
}
private static bool TryDecodeBase64(string base64, out byte[] documentBytes, out BadRequestObjectResult badRequest) {
try {
documentBytes = System.Convert.FromBase64String(base64);
badRequest = null!;
return true;
}
catch (FormatException) {
documentBytes = Array.Empty<byte>();
badRequest = new BadRequestObjectResult("The request payload must contain a valid base64 encoded document.");
return false;
}
}
private static string GetContentType(DocumentFormat documentFormat) => documentFormat switch {
DocumentFormat.PDF or DocumentFormat.PDFA => "application/pdf",
DocumentFormat.DOCX => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
DocumentFormat.RTF => "application/rtf",
DocumentFormat.HTML => "text/html",
DocumentFormat.PlainText => "text/plain",
_ => "application/octet-stream",
};
private static string GetFileName(string baseName, DocumentFormat documentFormat) => documentFormat switch {
DocumentFormat.TX => $"{baseName}.tx",
DocumentFormat.RTF => $"{baseName}.rtf",
DocumentFormat.PlainText => $"{baseName}.txt",
DocumentFormat.HTML => $"{baseName}.html",
DocumentFormat.DOCX => $"{baseName}.docx",
DocumentFormat.PDF => $"{baseName}.pdf",
DocumentFormat.PDFA => $"{baseName}.pdf",
DocumentFormat.XLSX => $"{baseName}.xlsx",
_ => $"{baseName}.bin",
};
}
public sealed record LoadDocumentRequest(string Document, string Format = "HTML");