Fix image caching. fully.
This commit is contained in:
parent
2e91ab74a4
commit
c863ea1363
4 changed files with 28 additions and 83 deletions
66
index.js
66
index.js
|
@ -2,8 +2,7 @@ const express = require('express'),
|
|||
path = require('path'),
|
||||
fs = require('fs'),
|
||||
WebSocket = require('ws'),
|
||||
minify = require('minify-html'),
|
||||
sha256 = require('sha256')
|
||||
minify = require('minify-html')
|
||||
|
||||
var app = express()
|
||||
|
||||
|
@ -38,13 +37,15 @@ app.listen(PORT, () => {
|
|||
console.log("Violet's Purgatory is now listening on port: " + PORT)
|
||||
})
|
||||
|
||||
var cachedImages = {}
|
||||
|
||||
if (!fs.existsSync(path.join(staticpath, 'cached'))) {
|
||||
fs.mkdirSync(path.join(staticpath, 'cached'))
|
||||
}
|
||||
|
||||
var randomQuotes = config.quotes
|
||||
|
||||
function get_img_url(activity, size="large_image") {
|
||||
function get_img_url(activity, size = "large_image") {
|
||||
|
||||
if ("assets" in activity) {
|
||||
var image = activity.assets[size]
|
||||
|
@ -113,8 +114,8 @@ async function pageUpdate() {
|
|||
"color": "rgb(255, 255, 75)"
|
||||
},
|
||||
"offline": {
|
||||
"text": "Offline",
|
||||
"color": "rgb(125, 125, 125)"
|
||||
"text": "",
|
||||
"color": "rgb(255, 255, 255)"
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -194,16 +195,10 @@ async function pageUpdate() {
|
|||
}
|
||||
|
||||
|
||||
function get_img(activity, size="large_image") {
|
||||
if (get_img_url(activity, size)) {
|
||||
var fn = sha256(get_img_url(activity, size))
|
||||
function get_img(activity, size = "large_image") {
|
||||
if (cachedImages[get_img_url(activity, size)]) {
|
||||
var fn = cachedImages[get_img_url(activity, size)]
|
||||
var fp = path.join(staticpath, 'cached', fn)
|
||||
|
||||
if (!fs.existsSync(fp)) {
|
||||
return 'imgs/notFound.png'
|
||||
} else if (fs.statSync(fp).size < 1000) {
|
||||
fs.rmSync(fp)
|
||||
}
|
||||
} else {
|
||||
return 'imgs/notFound.png'
|
||||
}
|
||||
|
@ -394,7 +389,7 @@ function beat(dur) {
|
|||
}, dur);
|
||||
}
|
||||
|
||||
lanyard.addEventListener("message", (res) => {
|
||||
lanyard.addEventListener("message", async (res) => {
|
||||
var data = JSON.parse(res.data)
|
||||
if (data.op == 1) {
|
||||
beat(data.d.heartbeat_interval)
|
||||
|
@ -412,41 +407,30 @@ lanyard.addEventListener("message", (res) => {
|
|||
const activity = lanyardData.activities[index];
|
||||
|
||||
if (get_img_url(activity)) {
|
||||
var fn = sha256(get_img_url(activity))
|
||||
var url = get_img_url(activity)
|
||||
var fn = Math.ceil(Math.random() * 100_000_000_000).toString()
|
||||
var fp = path.join(__dirname, 'static/cached', fn)
|
||||
if (!fs.existsSync(fp)) {
|
||||
var wrst = fs.createWriteStream(fp)
|
||||
|
||||
fetch(`${get_img_url(activity)}`)
|
||||
.then((response) => response.body)
|
||||
.then((body) => {
|
||||
const stream = new WritableStream({
|
||||
write(chunk) {
|
||||
wrst.write(chunk)
|
||||
}
|
||||
})
|
||||
if (!cachedImages[url]) {
|
||||
const response = await (await fetch(url)).arrayBuffer()
|
||||
|
||||
body.pipeTo(stream)
|
||||
})
|
||||
fs.writeFileSync(fp, Buffer.from(response))
|
||||
|
||||
cachedImages[url] = fn
|
||||
}
|
||||
}
|
||||
|
||||
if (get_img_url(activity, "small_image")) {
|
||||
var fn = sha256(get_img_url(activity, "small_image"))
|
||||
var url = get_img_url(activity, "small_image")
|
||||
var fn = Math.ceil(Math.random() * 100_000_000_000).toString()
|
||||
var fp = path.join(__dirname, 'static/cached', fn)
|
||||
if (!fs.existsSync(fp)) {
|
||||
var wrst = fs.createWriteStream(fp)
|
||||
|
||||
fetch(`${get_img_url(activity, "small_image")}`)
|
||||
.then((response) => response.body)
|
||||
.then((body) => {
|
||||
const stream = new WritableStream({
|
||||
write(chunk) {
|
||||
wrst.write(chunk)
|
||||
}
|
||||
})
|
||||
if (!cachedImages[url]) {
|
||||
const response = await (await fetch(url)).arrayBuffer()
|
||||
|
||||
body.pipeTo(stream)
|
||||
})
|
||||
fs.writeFileSync(fp, Buffer.from(response))
|
||||
|
||||
cachedImages[url] = fn
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
39
package-lock.json
generated
39
package-lock.json
generated
|
@ -11,7 +11,6 @@
|
|||
"dependencies": {
|
||||
"express": "^4.18.2",
|
||||
"minify-html": "^0.0.2",
|
||||
"sha256": "^0.2.0",
|
||||
"ws": "^8.14.2"
|
||||
}
|
||||
},
|
||||
|
@ -94,16 +93,6 @@
|
|||
"node": ">= 0.6"
|
||||
}
|
||||
},
|
||||
"node_modules/convert-hex": {
|
||||
"version": "0.1.0",
|
||||
"resolved": "https://registry.npmjs.org/convert-hex/-/convert-hex-0.1.0.tgz",
|
||||
"integrity": "sha512-w20BOb1PiR/sEJdS6wNrUjF5CSfscZFUp7R9NSlXH8h2wynzXVEPFPJECAnkNylZ+cvf3p7TyRUHggDmrwXT9A=="
|
||||
},
|
||||
"node_modules/convert-string": {
|
||||
"version": "0.1.0",
|
||||
"resolved": "https://registry.npmjs.org/convert-string/-/convert-string-0.1.0.tgz",
|
||||
"integrity": "sha512-1KX9ESmtl8xpT2LN2tFnKSbV4NiarbVi8DVb39ZriijvtTklyrT+4dT1wsGMHKD3CJUjXgvJzstm9qL9ICojGA=="
|
||||
},
|
||||
"node_modules/cookie": {
|
||||
"version": "0.5.0",
|
||||
"resolved": "https://registry.npmjs.org/cookie/-/cookie-0.5.0.tgz",
|
||||
|
@ -568,15 +557,6 @@
|
|||
"resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.2.0.tgz",
|
||||
"integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw=="
|
||||
},
|
||||
"node_modules/sha256": {
|
||||
"version": "0.2.0",
|
||||
"resolved": "https://registry.npmjs.org/sha256/-/sha256-0.2.0.tgz",
|
||||
"integrity": "sha512-kTWMJUaez5iiT9CcMv8jSq6kMhw3ST0uRdcIWl3D77s6AsLXNXRp3heeqqfu5+Dyfu4hwpQnMzhqHh8iNQxw0w==",
|
||||
"dependencies": {
|
||||
"convert-hex": "~0.1.0",
|
||||
"convert-string": "~0.1.0"
|
||||
}
|
||||
},
|
||||
"node_modules/side-channel": {
|
||||
"version": "1.0.4",
|
||||
"resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.0.4.tgz",
|
||||
|
@ -753,16 +733,6 @@
|
|||
"resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.5.tgz",
|
||||
"integrity": "sha512-nTjqfcBFEipKdXCv4YDQWCfmcLZKm81ldF0pAopTvyrFGVbcR6P/VAAd5G7N+0tTr8QqiU0tFadD6FK4NtJwOA=="
|
||||
},
|
||||
"convert-hex": {
|
||||
"version": "0.1.0",
|
||||
"resolved": "https://registry.npmjs.org/convert-hex/-/convert-hex-0.1.0.tgz",
|
||||
"integrity": "sha512-w20BOb1PiR/sEJdS6wNrUjF5CSfscZFUp7R9NSlXH8h2wynzXVEPFPJECAnkNylZ+cvf3p7TyRUHggDmrwXT9A=="
|
||||
},
|
||||
"convert-string": {
|
||||
"version": "0.1.0",
|
||||
"resolved": "https://registry.npmjs.org/convert-string/-/convert-string-0.1.0.tgz",
|
||||
"integrity": "sha512-1KX9ESmtl8xpT2LN2tFnKSbV4NiarbVi8DVb39ZriijvtTklyrT+4dT1wsGMHKD3CJUjXgvJzstm9qL9ICojGA=="
|
||||
},
|
||||
"cookie": {
|
||||
"version": "0.5.0",
|
||||
"resolved": "https://registry.npmjs.org/cookie/-/cookie-0.5.0.tgz",
|
||||
|
@ -1108,15 +1078,6 @@
|
|||
"resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.2.0.tgz",
|
||||
"integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw=="
|
||||
},
|
||||
"sha256": {
|
||||
"version": "0.2.0",
|
||||
"resolved": "https://registry.npmjs.org/sha256/-/sha256-0.2.0.tgz",
|
||||
"integrity": "sha512-kTWMJUaez5iiT9CcMv8jSq6kMhw3ST0uRdcIWl3D77s6AsLXNXRp3heeqqfu5+Dyfu4hwpQnMzhqHh8iNQxw0w==",
|
||||
"requires": {
|
||||
"convert-hex": "~0.1.0",
|
||||
"convert-string": "~0.1.0"
|
||||
}
|
||||
},
|
||||
"side-channel": {
|
||||
"version": "1.0.4",
|
||||
"resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.0.4.tgz",
|
||||
|
|
|
@ -19,7 +19,6 @@
|
|||
"dependencies": {
|
||||
"express": "^4.18.2",
|
||||
"minify-html": "^0.0.2",
|
||||
"sha256": "^0.2.0",
|
||||
"ws": "^8.14.2"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -134,6 +134,7 @@ a {
|
|||
left: 74px;
|
||||
border-radius: 50px;
|
||||
background: black;
|
||||
padding: 5px;
|
||||
/* border: 2px gray solid; */
|
||||
transform: scale(0.8);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue