From a022d881f8112804a8f168dd55db9b5c6add2562 Mon Sep 17 00:00:00 2001 From: Volpeon Date: Fri, 22 Oct 2021 07:11:25 +0200 Subject: Add image dimension check --- src/services/jobs.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/services/jobs.ts b/src/services/jobs.ts index 354c66d..878593b 100644 --- a/src/services/jobs.ts +++ b/src/services/jobs.ts @@ -1,6 +1,7 @@ import * as e621 from "../api/e926"; import * as mastodon from "../api/mastodon"; import config from "../config"; +import { imageSize } from "image-size"; import Sharp from "sharp"; export async function postSpecificPicture(id: number) { @@ -33,11 +34,15 @@ async function handlePost(post: e621.Post) { let file = await e621.client.get(post.file.url).buffer(); - if (Buffer.byteLength(file) > 1024 * 1024 * 9) { + const dims = imageSize(file); + const width = dims.width ?? 0; + const height = dims.height ?? 0; + + if (Buffer.byteLength(file) > 1024 * 1024 * 9 || width > 2000 || height > 2000) { console.log(`Compressing...`); file = await Sharp(file) - .resize(1800, 1800, { fit: "inside", withoutEnlargement: true }) + .resize(2000, 2000, { fit: "inside", withoutEnlargement: true }) .jpeg({ quality: 80, mozjpeg: true }) .toBuffer(); } -- cgit v1.2.3-70-g09d2