I am building a fastify server to upload/download files to s3, when I want to download a file I am able to get it using a node stream and return it, so I prevent loading the whole file in memory, I would like to do something like:
- get the file web stream from the bucket
- scan it using clamAV, streaming data into the AV
return the file stream to client only if the file is not infected
I have tried with piplines but I need to to check when the scan is done, and after I send the stream from s3 to the AV, the original stream is consumed, hence, I cannot send it to the client.
do you guys have any suggestion or any gotcha here?
code I have right now:
const stream = body.transformToWebStream();
const antivirusPassthrough = app.avClient.passthrough();
const downloadPassthrough = new PassThrough();
antivirusPassthrough.once("error", (err) => {
throw err
});
antivirusPassthrough.once("scan-complete", (result) => {
const { isInfected } = result;
if (isInfected) {
throw new Errpr("File is infected");
}
});
pipeline(
stream,
antivirusPassthrough,
downloadPassthrough,
(err) => {
if (err) {
app.log.error(err);
}
}
);
return reply.send(downloadPassthrough);