feat: add article search index endpoint and category export.zip
- Add GET /api/articleIndex returning the article search index (listSearchIndex) - Add GET /api/category/export.zip returning a kategorien.zip with a Windows-1252 CSV (depth-first, Ebene N columns) plus one AVIF per category image (content-addressed, deduplicated) using jszip
This commit is contained in:
76
src/util/kategorienCsv.js
Normal file
76
src/util/kategorienCsv.js
Normal file
@@ -0,0 +1,76 @@
|
||||
export const ROOT_CATEGORY_NAME = 'Home';
|
||||
|
||||
/** Escape a value for a CSV field ("..." double-quoting per RFC 4180). */
|
||||
function csvField(value) {
|
||||
const text = value == null ? '' : String(value);
|
||||
return /[";\n\r]/.test(text) ? `"${text.replace(/"/g, '""')}"` : text;
|
||||
}
|
||||
|
||||
function categoryDisplayName(name, categoryId, rootCategoryId) {
|
||||
if (rootCategoryId != null && String(categoryId) === String(rootCategoryId)) {
|
||||
return ROOT_CATEGORY_NAME;
|
||||
}
|
||||
return (name ?? '').trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the kategorien.csv string from the flattened category tree returned by
|
||||
* `getTreeForCompany`. Each row carries FullHash;Id and one name cell in the
|
||||
* `Ebene N` column matching the category's tree depth (root = Ebene 1).
|
||||
* Mirrors the frontend's client-side export format exactly.
|
||||
*/
|
||||
export function buildKategorienCsv(tree, rootCategoryId) {
|
||||
if (!Array.isArray(tree) || tree.length === 0) return '';
|
||||
|
||||
const children = new Map();
|
||||
const keyOf = (parentId) =>
|
||||
parentId == null || parentId === 0 ? 'ROOT' : String(parentId);
|
||||
for (const node of tree) {
|
||||
const key = keyOf(node.parentId);
|
||||
if (!children.has(key)) children.set(key, []);
|
||||
children.get(key).push(node);
|
||||
}
|
||||
children.forEach((arr) =>
|
||||
arr.sort((a, b) => (a.sort ?? 0) - (b.sort ?? 0) || a.id - b.id),
|
||||
);
|
||||
|
||||
const root = tree.find((n) => keyOf(n.parentId) === 'ROOT');
|
||||
const ordered = [];
|
||||
const visited = new Set();
|
||||
const walk = (node, depth) => {
|
||||
if (node == null || visited.has(String(node.id))) return;
|
||||
visited.add(String(node.id));
|
||||
ordered.push({ node, depth });
|
||||
(children.get(String(node.id)) ?? []).forEach((kid) => walk(kid, depth + 1));
|
||||
};
|
||||
walk(root, 0);
|
||||
|
||||
const maxDepth = ordered.reduce((m, item) => Math.max(m, item.depth), 0);
|
||||
const levelHeaders = Array.from({ length: maxDepth + 1 }, (_, i) => `Ebene ${i + 1}`);
|
||||
const headers = ['FullHash', 'Id', ...levelHeaders];
|
||||
|
||||
const lines = [
|
||||
'sep=;',
|
||||
headers.map(csvField).join(';'),
|
||||
...ordered.map(({ node, depth }) => {
|
||||
const name = categoryDisplayName(node.name, node.id, rootCategoryId);
|
||||
return [
|
||||
csvField(node.fullHash ?? ''),
|
||||
csvField(node.id),
|
||||
...levelHeaders.map((_, index) => (index === depth ? csvField(name) : '')),
|
||||
].join(';');
|
||||
}),
|
||||
];
|
||||
|
||||
return lines.join('\r\n');
|
||||
}
|
||||
|
||||
/** Encode a string as Windows-1252 bytes (single-byte; out-of-range → '?'). */
|
||||
export function toWindows1252(str) {
|
||||
const bytes = Buffer.alloc(str.length);
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
const code = str.charCodeAt(i);
|
||||
bytes[i] = code <= 0xff ? code : 0x3f; // '?'
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
Reference in New Issue
Block a user