diff options
| author | Volpeon <git@volpeon.ink> | 2021-10-19 12:20:56 +0200 | 
|---|---|---|
| committer | Volpeon <git@volpeon.ink> | 2021-10-19 12:20:56 +0200 | 
| commit | 1adb1b9d24c66732dd94f2a7120d1f93b08abdf5 (patch) | |
| tree | 1eef1e85ce30fa6153002db525d7d239a0a76cfb /src | |
| parent | Fix dedupe (diff) | |
| download | feralbot-1adb1b9d24c66732dd94f2a7120d1f93b08abdf5.tar.gz feralbot-1adb1b9d24c66732dd94f2a7120d1f93b08abdf5.tar.bz2 feralbot-1adb1b9d24c66732dd94f2a7120d1f93b08abdf5.zip | |
Compress files > 9 MB
Diffstat (limited to 'src')
| -rw-r--r-- | src/index.ts | 24 | 
1 files changed, 17 insertions, 7 deletions
| 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 @@ | |||
| 1 | import * as e621 from "./api/e621"; | 1 | import * as e621 from "./api/e621"; | 
| 2 | import * as mastodon from "./api/mastodon"; | 2 | import * as mastodon from "./api/mastodon"; | 
| 3 | import config from "./config"; | 3 | import config from "./config"; | 
| 4 | import * as cliArgs from 'ts-command-line-args'; | 4 | import Sharp from "sharp"; | 
| 5 | import * as cliArgs from "ts-command-line-args"; | ||
| 5 | 6 | ||
| 6 | const args = cliArgs.parse<{ | 7 | const args = cliArgs.parse<{ | 
| 7 | id?: number; | 8 | id?: number; | 
| @@ -9,11 +10,11 @@ const args = cliArgs.parse<{ | |||
| 9 | }>( | 10 | }>( | 
| 10 | { | 11 | { | 
| 11 | id: { type: Number, optional: true }, | 12 | id: { type: Number, optional: true }, | 
| 12 | help: { type: Boolean, optional: true, alias: 'h' }, | 13 | help: { type: Boolean, optional: true, alias: "h" }, | 
| 13 | }, | 14 | }, | 
| 14 | { | 15 | { | 
| 15 | helpArg: 'help', | 16 | helpArg: "help", | 
| 16 | }, | 17 | } | 
| 17 | ); | 18 | ); | 
| 18 | 19 | ||
| 19 | async function postRandomPicture() { | 20 | async function postRandomPicture() { | 
| @@ -24,7 +25,7 @@ async function postRandomPicture() { | |||
| 24 | const post = await e621.getRandomPost(query); | 25 | const post = await e621.getRandomPost(query); | 
| 25 | 26 | ||
| 26 | console.log(`Got ${post.id} via query ${queryIndex}`); | 27 | console.log(`Got ${post.id} via query ${queryIndex}`); | 
| 27 | 28 | ||
| 28 | await handlePost(post); | 29 | await handlePost(post); | 
| 29 | } | 30 | } | 
| 30 | 31 | ||
| @@ -34,7 +35,7 @@ async function postSpecificPicture(id: number) { | |||
| 34 | const post = await e621.getPostById(id); | 35 | const post = await e621.getPostById(id); | 
| 35 | 36 | ||
| 36 | console.log(`Got ${post.id}`); | 37 | console.log(`Got ${post.id}`); | 
| 37 | 38 | ||
| 38 | await handlePost(post); | 39 | await handlePost(post); | 
| 39 | } | 40 | } | 
| 40 | 41 | ||
| @@ -44,7 +45,16 @@ async function handlePost(post: e621.Post) { | |||
| 44 | 45 | ||
| 45 | console.log(`Downloading image...`); | 46 | console.log(`Downloading image...`); | 
| 46 | 47 | ||
| 47 | const file = await e621.client.get(post.file.url).buffer(); | 48 | let file = await e621.client.get(post.file.url).buffer(); | 
| 49 | |||
| 50 | if (Buffer.byteLength(file) > 1024 * 1024 * 9) { | ||
| 51 | console.log(`Compressing...`); | ||
| 52 | |||
| 53 | file = await Sharp(file) | ||
| 54 | .resize(1000, 1000, { fit: "inside", withoutEnlargement: true }) | ||
| 55 | .jpeg({ quality: 80, mozjpeg: true }) | ||
| 56 | .toBuffer(); | ||
| 57 | } | ||
| 48 | 58 | ||
| 49 | console.log(`Uploading...`); | 59 | console.log(`Uploading...`); | 
| 50 | 60 | ||
