refactor(data): move base64 decoding to _genFile and remove inline data field

- Removed inline base64 decoding (`data` field) from `_documents` definitions
- Introduced `_genFile(name)` utility to generate Uint8Array from file name
- Simplified `_documents` by eliminating direct `data` property population
This commit is contained in:
tekh 2025-07-16 14:43:47 +02:00
parent a34270cfc3
commit a88238f209

View File

@ -249,23 +249,10 @@ export const _attributes: Attribute[] = [
// ----------------------------------------------------------------------
function base64ToUint8Array(base64: string): Uint8Array {
const binaryString = atob(base64); // Decode base64 to binary string
const len = binaryString.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes;
}
export const _documents: Doc[] = [
{
id: 1,
name: "example1.pdf",
data: base64ToUint8Array(_base64.example1_pdf),
addedWhen: new Date("2024-01-12T10:00:00Z"),
addedWho: "TekH",
attributes: [
@ -320,12 +307,10 @@ export const _documents: Doc[] = [
type: 'DATETIME'
}
]
},
{
id: 2,
name: "example2.pdf",
data: base64ToUint8Array(_base64.example2_pdf),
addedWhen: new Date("2024-02-03T09:30:00Z"),
addedWho: "bob",
changedWhen: new Date("2024-03-15T12:00:00Z"),
@ -386,7 +371,6 @@ export const _documents: Doc[] = [
{
id: 3,
name: "document1.docx",
data: base64ToUint8Array(_base64.document1_docx),
addedWhen: new Date("2023-12-20T14:45:00Z"),
addedWho: "SchreiberM",
attributes: [
@ -435,7 +419,6 @@ export const _documents: Doc[] = [
{
id: 4,
name: "spreadsheet1.xlsx",
data: base64ToUint8Array(_base64.spreadsheet1_xlsx),
addedWhen: new Date("2024-05-01T08:15:00Z"),
addedWho: "KammM",
changedWhen: new Date("2024-06-10T16:20:00Z"),
@ -486,7 +469,6 @@ export const _documents: Doc[] = [
{
id: 5,
name: "report.docx",
data: base64ToUint8Array(_base64.report_docx),
addedWhen: new Date("2024-04-17T11:25:00Z"),
addedWho: "SchreiberM",
attributes: [
@ -550,4 +532,32 @@ export const _documents: Doc[] = [
type: attr.type as Type
}))
}))
.map(doc => Doc.map(doc));
.map(doc => Doc.map(doc));
function base64ToUint8Array(base64: string): Uint8Array {
const binaryString = atob(base64); // Decode base64 to binary string
const len = binaryString.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes;
}
export function _genFile(name: string): Uint8Array | undefined {
switch (name) {
case "example1.pdf":
return base64ToUint8Array(_base64.example1_pdf);
case "example2.pdf":
return base64ToUint8Array(_base64.example2_pdf);
case "document1.docx":
return base64ToUint8Array(_base64.document1_docx);
case "spreadsheet1.xlsx":
return base64ToUint8Array(_base64.spreadsheet1_xlsx);
case "report.docx":
return base64ToUint8Array(_base64.report_docx);
default:
}
}