From 9e337ca1abd74bb20b10ce6f25ca83a66d7bbb08 Mon Sep 17 00:00:00 2001 From: kitt Date: Sat, 31 Dec 2022 00:12:38 -0500 Subject: [PATCH] add ImageWriter --- app/index.mjs | 2 ++ app/social/clients/ImageWriter.mjs | 17 +++++++++++++++++ app/social/index.mjs | 2 ++ docker/nginx/conf.d/default.conf | 6 ++++++ package.json | 1 + 5 files changed, 28 insertions(+) create mode 100644 app/social/clients/ImageWriter.mjs diff --git a/app/index.mjs b/app/index.mjs index 587a7be..dc12397 100644 --- a/app/index.mjs +++ b/app/index.mjs @@ -5,6 +5,7 @@ import { sendStatuses, testStatuses } from './social/index.mjs'; import { updateAll } from './data/index.mjs'; import { warmCaches } from "./splatnet/index.mjs"; import MastodonClient from './social/clients/MastodonClient.mjs'; +import ImageWriter from './social/clients/ImageWriter.mjs'; consoleStamp(console); dotenv.config(); @@ -14,6 +15,7 @@ const actions = { social: sendStatuses, socialTest: testStatuses, socialTestMastodon: () => testStatuses([new MastodonClient]), + socialTestImage: () => testStatuses([new ImageWriter]), splatnet: updateAll, warmCaches, } diff --git a/app/social/clients/ImageWriter.mjs b/app/social/clients/ImageWriter.mjs new file mode 100644 index 0000000..0ecb818 --- /dev/null +++ b/app/social/clients/ImageWriter.mjs @@ -0,0 +1,17 @@ +import fs from 'fs/promises'; +import mkdirp from 'mkdirp'; +import Client from "./Client.mjs"; + +export default class ImageWriter extends Client { + key = 'image'; + name = 'ImageWriter'; + + dir = 'dist/status-screenshots'; // `/screenshots` points to the page used by puppeteer + + async send(status, generator) { + await mkdirp(this.dir); + + let imgFilename = `${this.dir}/${generator.key}.png`; + await fs.writeFile(imgFilename, status.media[0].file); + } +} diff --git a/app/social/index.mjs b/app/social/index.mjs index 83195bc..7ea534c 100644 --- a/app/social/index.mjs +++ b/app/social/index.mjs @@ -1,4 +1,5 @@ import FileWriter from "./clients/FileWriter.mjs"; +import ImageWriter from "./clients/ImageWriter.mjs"; import MastodonClient from "./clients/MastodonClient.mjs"; import TwitterClient from "./clients/TwitterClient.mjs"; import DailyDropGearStatus from "./generators/DailyDropGearStatus.mjs"; @@ -26,6 +27,7 @@ function defaultClients() { return [ new TwitterClient, new MastodonClient, + new ImageWriter, ]; } diff --git a/docker/nginx/conf.d/default.conf b/docker/nginx/conf.d/default.conf index f1009e1..bf48151 100644 --- a/docker/nginx/conf.d/default.conf +++ b/docker/nginx/conf.d/default.conf @@ -19,6 +19,12 @@ server { autoindex on; } + location /status-screenshots/ { + add_header Cache-Control no-cache; + add_header Access-Control-Allow-Origin *; + autoindex on; + } + # Gear redirect location ~ /nso/g/(.*) { return 302 https://s.nintendo.com/av5ja-lp1/znca/game/4834290508791808?p=/gesotown/$1; diff --git a/package.json b/package.json index 504013a..c35a77c 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "cron": "node app/index.mjs cron", "social": "node app/index.mjs social", "social:test": "node app/index.mjs socialTest", + "social:test:image": "node app/index.mjs socialTestImage", "social:test:mastodon": "node app/index.mjs socialTestMastodon", "splatnet": "node app/index.mjs splatnet", "warmCaches": "node app/index.mjs warmCaches"