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 --- package.json | 1 + src/services/jobs.ts | 9 +++++++-- yarn.lock | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 8cf504a..2d7cc6e 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "form-data": "^4.0.0", "fp-ts": "^2.11.5", "got": "^11.8.2", + "image-size": "^1.0.0", "io-ts": "^2.2.16", "nanoid": "^3.1.30", "sharp": "^0.29.1", 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(); } diff --git a/yarn.lock b/yarn.lock index 9ea15d5..709a413 100644 --- a/yarn.lock +++ b/yarn.lock @@ -492,6 +492,13 @@ ieee754@^1.1.13, ieee754@^1.2.1: resolved "https://registry.yarnpkg.com/ieee754/-/ieee754-1.2.1.tgz#8eb7a10a63fff25d15a57b001586d177d1b0d352" integrity sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA== +image-size@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/image-size/-/image-size-1.0.0.tgz#58b31fe4743b1cec0a0ac26f5c914d3c5b2f0750" + integrity sha512-JLJ6OwBfO1KcA+TvJT+v8gbE6iWbj24LyDNFgFEN0lzegn6cC6a/p3NIDaepMsJjQjlUWqIC7wJv8lBFxPNjcw== + dependencies: + queue "6.0.2" + inherits@^2.0.3, inherits@^2.0.4, inherits@~2.0.3: version "2.0.4" resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c" @@ -701,6 +708,13 @@ pump@^3.0.0: end-of-stream "^1.1.0" once "^1.3.1" +queue@6.0.2: + version "6.0.2" + resolved "https://registry.yarnpkg.com/queue/-/queue-6.0.2.tgz#b91525283e2315c7553d2efa18d83e76432fed65" + integrity sha512-iHZWu+q3IdFZFX36ro/lKBkSvfkztY5Y7HMiPlOUjhupPcG2JMfst2KKEpu5XndviX/3UhFbRngUPNKtgvtZiA== + dependencies: + inherits "~2.0.3" + quick-lru@^5.1.1: version "5.1.1" resolved "https://registry.yarnpkg.com/quick-lru/-/quick-lru-5.1.1.tgz#366493e6b3e42a3a6885e2e99d18f80fb7a8c932" -- cgit v1.2.3-70-g09d2