Fix uploads?

This commit is contained in:
Rory& 2025-09-23 21:41:37 +02:00
parent 2a10b3528f
commit ac807dcf8d
2 changed files with 29 additions and 14 deletions

View File

@ -122,29 +122,43 @@ router.put("/:channel_id/:batch_id/:attachment_id/:filename", multer.single("fil
const att = await CloudAttachment.findOneOrFail({ const att = await CloudAttachment.findOneOrFail({
where: { where: {
uploadFilename: `${channel_id}/${batch_id}/${attachment_id}/${filename}`, uploadFilename: `${channel_id}/${batch_id}/${attachment_id}/${filename}`,
channelId: channel_id,
userAttachmentId: attachment_id,
userFilename: filename
}, },
}); });
if (!req.file) throw new HTTPError("file missing"); const maxLength = Config.get().cdn.maxAttachmentSize;
const { buffer, mimetype, size } = req.file; const chunks: Buffer[] = [];
const path = `${channel_id}/${batch_id}/${attachment_id}/${filename}`; let length = 0;
await storage.set(path, buffer); req.on("data", (chunk) => {
if (mimetype.includes("image")) { chunks.push(chunk);
const dimensions = imageSize(buffer); length += chunk.length;
if (dimensions) { if (length > maxLength) {
att.width = dimensions.width; res.status(413).send("File too large");
att.height = dimensions.height; req.destroy();
} }
} });
req.on("end", async () => {
const buffer = Buffer.concat(chunks);
const path = `${channel_id}/${batch_id}/${attachment_id}/${filename}`;
att.size = size; await storage.set(path, buffer);
att.contentType = att.userOriginalContentType ?? mimetype; if (att.userOriginalContentType?.includes("image")) {
const dimensions = imageSize(buffer);
if (dimensions) {
att.width = dimensions.width;
att.height = dimensions.height;
}
}
await att.save(); att.size = buffer.length;
await att.save();
return res.status(200); res.status(200);
});
}); });
export default router; export default router;

View File

@ -23,4 +23,5 @@ export class CdnConfiguration extends EndpointConfiguration {
resizeWidthMax: number = 1000; resizeWidthMax: number = 1000;
imagorServerUrl: string | null = null; imagorServerUrl: string | null = null;
proxyCacheHeaderSeconds: number = 60 * 60 * 24; proxyCacheHeaderSeconds: number = 60 * 60 * 24;
maxAttachmentSize: number = 25 * 1024 * 1024; // 25 MB
} }