Local image converting

This commit is contained in:
bingus_violet 2024-09-26 23:41:36 -05:00
parent 17c528c23d
commit 7c89ef047d
3 changed files with 532 additions and 998 deletions

View file

@ -1,7 +1,8 @@
const express = require("express"), const express = require("express"),
fs = require("fs"), fs = require("fs"),
path = require("path"), path = require("path"),
WebSocket = require('ws') WebSocket = require('ws'),
sharp = require("sharp")
const PORT = process.env.PORT || 8080 const PORT = process.env.PORT || 8080
@ -135,7 +136,7 @@ function socketeer() {
const activity = lanyardData.activities[index]; const activity = lanyardData.activities[index];
var imgType = undefined var imgType = undefined
var imgRes = "512x512/" var imgRes = 512
for (var i = 0; i < 2; i++) { for (var i = 0; i < 2; i++) {
if (get_img_url(activity, imgType)) { if (get_img_url(activity, imgType)) {
var url = get_img_url(activity, imgType) var url = get_img_url(activity, imgType)
@ -143,17 +144,24 @@ function socketeer() {
var fp = path.join(imgCache, fn) var fp = path.join(imgCache, fn)
if (!cachedImages.imgs[url]) { if (!cachedImages.imgs[url]) {
const response = await (await fetch(thumborURL + imgRes + thumborArgs + url)).arrayBuffer() const response = await (await fetch(url)).arrayBuffer()
const converted = await sharp(response)
.resize(imgRes, imgRes)
.png({ progressive: true })
.toBuffer()
fs.writeFileSync(fp, converted)
fs.writeFileSync(fp, Buffer.from(response))
cachedImages.imgs[url] = { cachedImages.imgs[url] = {
"file": fn, "file": fn,
"lastUpdated": Date.now() "lastUpdated": Date.now()
} }
fs.writeFileSync(cacheFile, JSON.stringify(cachedImages)) fs.writeFileSync(cacheFile, JSON.stringify(cachedImages))
} }
imgType = "small_image" imgType = "small_image"
imgRes = "128x128/" imgRes = 128
} }
} }
} }

1510
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -4,7 +4,7 @@
}, },
"dependencies": { "dependencies": {
"express": "^4.19.2", "express": "^4.19.2",
"spotifydl-core": "^0.2.0", "sharp": "^0.33.5",
"ws": "^8.17.0" "ws": "^8.17.0"
} }
} }