fix: add dynamic timeout for document uploads (#33) #34
@@ -272,20 +272,15 @@ export class DocumentsController {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read first 4100 bytes to detect file type via magic bytes
|
// Collect ALL file chunks first (breaking early from async iterator corrupts stream state)
|
||||||
const chunks: Buffer[] = [];
|
const chunks: Buffer[] = [];
|
||||||
let totalBytes = 0;
|
|
||||||
const targetBytes = 4100;
|
|
||||||
|
|
||||||
for await (const chunk of mp.file) {
|
for await (const chunk of mp.file) {
|
||||||
chunks.push(chunk);
|
chunks.push(chunk);
|
||||||
totalBytes += chunk.length;
|
|
||||||
if (totalBytes >= targetBytes) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
const fullBuffer = Buffer.concat(chunks);
|
||||||
|
|
||||||
const headerBuffer = Buffer.concat(chunks);
|
// Use first 4100 bytes for file type detection via magic bytes
|
||||||
|
const headerBuffer = fullBuffer.subarray(0, Math.min(4100, fullBuffer.length));
|
||||||
|
|
||||||
// Validate actual file content using magic bytes
|
// Validate actual file content using magic bytes
|
||||||
const detectedType = await FileType.fromBuffer(headerBuffer);
|
const detectedType = await FileType.fromBuffer(headerBuffer);
|
||||||
@@ -341,15 +336,9 @@ export class DocumentsController {
|
|||||||
|
|
||||||
const counter = new CountingStream();
|
const counter = new CountingStream();
|
||||||
|
|
||||||
// Create a new readable stream from the header buffer + remaining file chunks
|
// Create readable stream from the complete buffer and pipe through counter
|
||||||
const headerStream = Readable.from([headerBuffer]);
|
const fileStream = Readable.from([fullBuffer]);
|
||||||
const remainingStream = mp.file;
|
fileStream.pipe(counter);
|
||||||
|
|
||||||
// Pipe header first, then remaining content through counter
|
|
||||||
headerStream.pipe(counter, { end: false });
|
|
||||||
headerStream.on('end', () => {
|
|
||||||
remainingStream.pipe(counter);
|
|
||||||
});
|
|
||||||
|
|
||||||
const storage = getStorageService();
|
const storage = getStorageService();
|
||||||
const bucket = 'documents';
|
const bucket = 'documents';
|
||||||
|
|||||||
Reference in New Issue
Block a user