u
This commit is contained in:
@@ -1,7 +1,29 @@
|
||||
import chalk from 'chalk';
|
||||
|
||||
const BINARY_PREVIEW_BYTES = 16;
|
||||
|
||||
function summarizeBuffer(buf) {
|
||||
const head = buf.subarray(0, BINARY_PREVIEW_BYTES);
|
||||
const hex = [...head].map((b) => b.toString(16).padStart(2, '0')).join(' ');
|
||||
return {
|
||||
type: 'Buffer',
|
||||
length: buf.length,
|
||||
headHex: hex,
|
||||
};
|
||||
}
|
||||
|
||||
function safeJson(value) {
|
||||
try {
|
||||
if (Buffer.isBuffer(value)) {
|
||||
return JSON.stringify(summarizeBuffer(value), null, 2);
|
||||
}
|
||||
if (
|
||||
Array.isArray(value) &&
|
||||
value.length > 64 &&
|
||||
value.every((n) => Number.isInteger(n) && n >= 0 && n <= 255)
|
||||
) {
|
||||
return JSON.stringify(summarizeBuffer(Buffer.from(value)), null, 2);
|
||||
}
|
||||
return JSON.stringify(value, null, 2);
|
||||
} catch {
|
||||
return String(value);
|
||||
@@ -46,6 +68,7 @@ export const loggerMiddleware = (req, res, next) => {
|
||||
console.log(`${method} ${url} ${status} ${chalk.gray(`(${duration}ms)`)}`);
|
||||
console.log(chalk.gray(' params:'), safeJson(req.params ?? {}));
|
||||
console.log(chalk.gray(' query:'), safeJson(req.query ?? {}));
|
||||
console.log(chalk.gray(' content-type:'), req.headers['content-type'] ?? '(none)');
|
||||
console.log(chalk.gray(' body:'), safeJson(req.body ?? {}));
|
||||
console.log(chalk.gray(' response:'), safeJson(getBody()));
|
||||
});
|
||||
|
||||
@@ -19,11 +19,6 @@ function articleErrorStatus(error) {
|
||||
return 500;
|
||||
}
|
||||
|
||||
function isLikelyImageDecodeError(error) {
|
||||
const msg = error?.message || '';
|
||||
return /unsupported|corrupt|invalid|input buffer|vips/i.test(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers article routes (requires auth middleware).
|
||||
* @param {import('express').Application} app - Express app
|
||||
@@ -346,9 +341,6 @@ export function register(app, basePath = '') {
|
||||
const image = await db.addImage(req.user.id, decodeURIComponent(sku), imageBuffer);
|
||||
res.status(201).json({ image });
|
||||
} catch (error) {
|
||||
if (isLikelyImageDecodeError(error)) {
|
||||
return res.status(400).json({ error: 'Invalid image' });
|
||||
}
|
||||
const status = articleErrorStatus(error);
|
||||
if (status === 500) console.error('Add article image failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
|
||||
Reference in New Issue
Block a user