better content length handling

This commit is contained in:
mertalev 2025-10-07 15:01:21 -04:00
parent 0db8c10601
commit 1915e3ceb2
No known key found for this signature in database
GPG key ID: DF6ABC77AAD98C95

View file

@ -310,10 +310,13 @@ export class AssetUploadService extends BaseService {
req.on('data', (data: Buffer) => { req.on('data', (data: Buffer) => {
if (receivedLength + data.length > size) { if (receivedLength + data.length > size) {
writeStream.destroy(); writeStream.destroy();
req.destroy(); void this.onCancel(id, path).catch((error: any) =>
void this.onCancel(id, path) this.logger.error(`Failed to remove ${id} after too much data: ${error.message}`),
.catch((error: any) => this.logger.error(`Failed to remove ${id} after too much data: ${error.message}`)) );
.finally(() => res.status(400).send('Received more data than specified in content-length')); if (!res.headersSent) {
res.status(400).send('Received more data than specified in content-length');
}
res.on('finish', () => req.destroy());
return; return;
} }
receivedLength += data.length; receivedLength += data.length;
@ -323,15 +326,7 @@ export class AssetUploadService extends BaseService {
} }
}); });
req.on('end', () => { req.on('end', () => writeStream.end());
if (receivedLength === size) {
return writeStream.end();
}
writeStream.destroy();
void this.onCancel(id, path)
.catch((error: any) => this.logger.error(`Failed to remove ${id} after unexpected length: ${error.message}`))
.finally(() => res.status(400).send(`Received ${receivedLength} bytes when expecting ${size}`));
});
return writeStream; return writeStream;
} }