check size in image and pdf handler

This commit is contained in:
daniel31x13
2024-06-28 12:20:56 -04:00
parent 1e2ed6c293
commit d66c784d3f
3 changed files with 72 additions and 49 deletions
@@ -0,0 +1,35 @@
import { Link } from "@prisma/client";
import { prisma } from "../db";
import createFile from "../storage/createFile";
const imageHandler = async ({ url, id }: Link, extension: string) => {
const image = await fetch(url as string).then((res) => res.blob());
const buffer = Buffer.from(await image.arrayBuffer());
if (
Buffer.byteLength(buffer) >
1024 * 1024 * Number(process.env.SCREENSHOT_MAX_BUFFER || 2)
)
return console.log("Error archiving as Screenshot: Buffer size exceeded");
const linkExists = await prisma.link.findUnique({
where: { id },
});
if (linkExists) {
await createFile({
data: buffer,
filePath: `archives/${linkExists.collectionId}/${id}.${extension}`,
});
await prisma.link.update({
where: { id },
data: {
image: `archives/${linkExists.collectionId}/${id}.${extension}`,
},
});
}
};
export default imageHandler;