export
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
import express from 'express';
|
||||
import JSZip from 'jszip';
|
||||
import * as db from '../../db/article.js';
|
||||
import * as picDb from '../../db/pic.js';
|
||||
import { buildArtikelCsv, toWindows1252 } from '../../util/artikelCsv.js';
|
||||
|
||||
const AVIF_BODY_LIMIT = '20mb';
|
||||
const FULL_HASH_RE = /^[a-fA-F0-9]{64}$/;
|
||||
@@ -138,6 +141,110 @@ export function register(app, basePath = '') {
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /article/export.zip:
|
||||
* get:
|
||||
* tags: [Article]
|
||||
* summary: Export articles as ZIP
|
||||
* description: |
|
||||
* Builds an `artikel.zip` for the authenticated user's selected company.
|
||||
* `categoryId` (optional) restricts the export to that category and below;
|
||||
* it must be reachable via the company's root category. Defaults to the
|
||||
* whole company root tree.
|
||||
* The ZIP contains `artikel.csv` (semicolon-separated, Windows-1252)
|
||||
* plus one AVIF entry per article image, named `<fullHash>.avif`
|
||||
* (content-addressed, so shared hashes are deduplicated).
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* parameters:
|
||||
* - in: query
|
||||
* name: categoryId
|
||||
* required: false
|
||||
* schema:
|
||||
* type: integer
|
||||
* description: Restrict export to this category subtree
|
||||
* responses:
|
||||
* 200:
|
||||
* description: ZIP archive
|
||||
* content:
|
||||
* application/zip:
|
||||
* schema:
|
||||
* type: string
|
||||
* format: binary
|
||||
* 400:
|
||||
* description: No company selected
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* 401:
|
||||
* description: Unauthorized
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* 403:
|
||||
* description: Not authorized
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* 404:
|
||||
* description: Company not found
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
*/
|
||||
app.get(`${basePath}/article/export.zip`, async (req, res) => {
|
||||
const companyId = req.user.selectedCompanyId;
|
||||
if (companyId == null) {
|
||||
return res.status(400).json({ error: 'No company selected' });
|
||||
}
|
||||
|
||||
let categoryId = null;
|
||||
if (req.query.categoryId != null && req.query.categoryId !== '') {
|
||||
const parsed = parsePositiveInt(req.query.categoryId, 'categoryId');
|
||||
if (parsed.error) {
|
||||
return res.status(400).json({ error: parsed.error });
|
||||
}
|
||||
categoryId = parsed.id;
|
||||
}
|
||||
|
||||
try {
|
||||
const rows = await db.listExport(req.user.id, companyId, categoryId);
|
||||
|
||||
const csv = buildArtikelCsv(rows);
|
||||
|
||||
const zip = new JSZip();
|
||||
zip.file('artikel.csv', toWindows1252(csv));
|
||||
|
||||
const seen = new Set();
|
||||
for (const row of rows) {
|
||||
for (const fullHash of row.fullHashes ?? []) {
|
||||
if (!fullHash || seen.has(fullHash)) continue;
|
||||
seen.add(fullHash);
|
||||
try {
|
||||
const { buffer } = await picDb.getImageByHash(req.user.id, fullHash);
|
||||
zip.file(`${fullHash}.avif`, buffer);
|
||||
} catch (error) {
|
||||
// Skip images this user cannot access; CSV already carries the hash.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const content = await zip.generateAsync({ type: 'nodebuffer' });
|
||||
res.setHeader('Content-Type', 'application/zip');
|
||||
res.setHeader('Content-Disposition', 'attachment; filename="artikel.zip"');
|
||||
res.status(200).send(content);
|
||||
} catch (error) {
|
||||
const status = articleErrorStatus(error);
|
||||
if (status === 500) console.error('Export articles failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /article/{sku}:
|
||||
|
||||
Reference in New Issue
Block a user