From 1adb1b9d24c66732dd94f2a7120d1f93b08abdf5 Mon Sep 17 00:00:00 2001 From: Volpeon Date: Tue, 19 Oct 2021 12:20:56 +0200 Subject: Compress files > 9 MB --- src/index.ts | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) (limited to 'src/index.ts') diff --git a/src/index.ts b/src/index.ts index 3b8b0a8..e7bb328 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,8 @@ import * as e621 from "./api/e621"; import * as mastodon from "./api/mastodon"; import config from "./config"; -import * as cliArgs from 'ts-command-line-args'; +import Sharp from "sharp"; +import * as cliArgs from "ts-command-line-args"; const args = cliArgs.parse<{ id?: number; @@ -9,11 +10,11 @@ const args = cliArgs.parse<{ }>( { id: { type: Number, optional: true }, - help: { type: Boolean, optional: true, alias: 'h' }, + help: { type: Boolean, optional: true, alias: "h" }, }, { - helpArg: 'help', - }, + helpArg: "help", + } ); async function postRandomPicture() { @@ -24,7 +25,7 @@ async function postRandomPicture() { const post = await e621.getRandomPost(query); console.log(`Got ${post.id} via query ${queryIndex}`); - + await handlePost(post); } @@ -34,7 +35,7 @@ async function postSpecificPicture(id: number) { const post = await e621.getPostById(id); console.log(`Got ${post.id}`); - + await handlePost(post); } @@ -44,7 +45,16 @@ async function handlePost(post: e621.Post) { console.log(`Downloading image...`); - const file = await e621.client.get(post.file.url).buffer(); + let file = await e621.client.get(post.file.url).buffer(); + + if (Buffer.byteLength(file) > 1024 * 1024 * 9) { + console.log(`Compressing...`); + + file = await Sharp(file) + .resize(1000, 1000, { fit: "inside", withoutEnlargement: true }) + .jpeg({ quality: 80, mozjpeg: true }) + .toBuffer(); + } console.log(`Uploading...`); -- cgit v1.2.3-70-g09d2