check size in image and pdf handler
This commit is contained in:
@@ -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;
|
||||
Reference in New Issue
Block a user