test: fix raw upload assertions

This commit is contained in:
root
2026-10-17 09:57:43 +08:00
parent f24088f359
commit 10fa64b5c8
17 changed files with 150 additions and 56 deletions
+53 -15
View File
@@ -272,25 +272,63 @@ const server = http.createServer(async (req, res) => {
});
}
// 附件直传图床:attachment_add 落本地后同步推 Lsky(COS),失败自动回退本地
// 附件上传(原始二进制 PUT):无 base64 膨胀;图床上传改为后台异步,保存立即返回
if (req.method === 'PUT' && route === '/sync/attachment/raw') {
const q = url.searchParams;
const name = (q.get('name') || 'file').slice(0, 200);
const mime = (q.get('mime') || 'application/octet-stream').slice(0, 120);
const ownerId = Number(q.get('owner') || 0);
if (!ownerId) return send(400, false, '缺少 owner');
const chunks = [];
for await (const c of req) chunks.push(c);
const buf = Buffer.concat(chunks);
if (!buf.length) return send(400, false, '空请求体');
if (buf.length > 80 * 1024 * 1024) return send(400, false, '单个文件不超过 80MB');
const thumbB64 = String(q.get('thumb') || '');
const data = commandsFor(auth.user_id).handleCommand('attachment_add', {
owner_id: ownerId, file_name: name, mime, size: buf.length, data_b64: buf.toString('base64')
});
const cmds = commandsFor(auth.user_id);
const itemId = data.item.id;
// 缩略图回填(带 base64,跟随本响应返回后由客户端二次调用亦可;此处直接从 query 收)
// 图床异步直传:失败仅记日志,可用 /admin/attachments/sync-lsky 补传
if (LSKY_API && LSKY_TOKEN) {
(async () => {
try {
const up = await lskyUpload(buf, name, mime);
let thumbUrl = null;
if (thumbB64) {
const tUp = await lskyUpload(Buffer.from(thumbB64, 'base64'), 'thumb-' + name + '.webp', 'image/webp');
thumbUrl = tUp.thumb_url || tUp.url;
}
cmds.handleCommand('attachment_set_remote', { id: itemId, remote_url: up.url, remote_thumb: thumbUrl });
console.log('[lsky] 附件 #' + itemId + ' 已上图床');
} catch (e) { console.log('[lsky] 附件 #' + itemId + ' 后台上传失败(可用 sync-lsky 补传):', e.message); }
})();
}
return send(200, true, data);
}
// 附件直传图床(兼容旧 base64 通道):同样异步
if (req.method === 'POST' && route === '/api/attachment_add') {
const body = await readJson(req);
const data = commandsFor(auth.user_id).handleCommand('attachment_add', body);
if (LSKY_API && LSKY_TOKEN) {
try {
const orig = Buffer.from(body.data_b64 || '', 'base64');
const up = await lskyUpload(orig, body.file_name || ('file-' + data.item.id), body.mime);
let thumbUrl = null;
if (body.thumb_b64) {
const tUp = await lskyUpload(Buffer.from(body.thumb_b64, 'base64'), 'thumb-' + (body.file_name || 'img') + '.webp', 'image/webp');
thumbUrl = tUp.thumb_url || tUp.url;
}
commandsFor(auth.user_id).handleCommand('attachment_set_remote', { id: data.item.id, remote_url: up.url, remote_thumb: thumbUrl });
data.item.remote_url = up.url;
data.item.remote_thumb = thumbUrl;
} catch (e) {
console.log('[lsky] 直传失败,保留本地存储:', e.message);
}
const orig = Buffer.from(body.data_b64 || '', 'base64');
const itemId = data.item.id;
(async () => {
try {
const up = await lskyUpload(orig, body.file_name || ('file-' + itemId), body.mime);
let thumbUrl = null;
if (body.thumb_b64) {
const tUp = await lskyUpload(Buffer.from(body.thumb_b64, 'base64'), 'thumb-' + (body.file_name || 'img') + '.webp', 'image/webp');
thumbUrl = tUp.thumb_url || tUp.url;
}
cmds = null;
commandsFor(auth.user_id).handleCommand('attachment_set_remote', { id: itemId, remote_url: up.url, remote_thumb: thumbUrl });
console.log('[lsky] 附件 #' + itemId + ' 已上图床');
} catch (e) { console.log('[lsky] 附件 #' + itemId + ' 后台上传失败:', e.message); }
})();
}
return send(200, true, data);
}