I'm new to streams, and I'm trying to check for stream length before uploading it to s3. It's not that efficient, but performance is not an issue at the moment.
The below code works fine for many images, but for one specific image it stops at the bytelength validation.
//using graphql-upload
const { createReadStream, filename, encoding, mimetype } = file;
const stream: ReadStream = createReadStream();
const validationStream = stream.pipe(new Stream.PassThrough());
const uploadStream = stream.pipe(new Stream.PassThrough());
try {
debugConsoleLog("File upload begun"); //Ok
//... extension validation
//... mime validation
debugConsoleLog("Type check good"); //Ok
let byteLength = 0;
for await (const uploadChunk of validationStream) {
debugConsoleLog(byteLength); // prints 0 once, never prints again
byteLength += (uploadChunk as Buffer).byteLength;
}
debugConsoleLog("Counted byteSize"); //Never called
//... Check if size is too big and upload //Never called
} catch (err) {
console.log(err);
throw err;
} finally {
stream.destroy();
validationStream.destroy();
uploadStream.destroy();
}
The image that doesn't work is a snapshot of a mac touchbar. Not that anyone will upload it, but it shows that some images are getting permanently stuck in the processing code.
There is something wrong in this part of the code for that image. How can I prevent the endless processing at this file if something is wrong with the image.
let byteLength = 0;
for await (const uploadChunk of validationStream) {
debugConsoleLog(byteLength); // prints 0 once, never prints again
byteLength += (uploadChunk as Buffer).byteLength;
}
Please tell if you need more info about the stream in question! Thank you.