Modular Code

This commit is contained in:
bingus_violet 2024-06-08 16:52:20 -05:00
parent e64c9feea2
commit 9ae8ebfe98

View file

@ -9,7 +9,19 @@ const cachePath = path.join(__dirname, "cache"),
cacheFile = path.join(__dirname, "cache.json")
if (!fs.existsSync(cachePath)) { fs.mkdirSync(cachePath) }
if (!fs.existsSync(cacheFile)) { fs.writeFileSync(cacheFile, "{}") }
if (!fs.existsSync(cacheFile) || !JSON.parse(fs.readFileSync(cacheFile)).songs) {
fs.writeFileSync(cacheFile, fs.readFileSync(path.join(__dirname, "defaults/cache.json")))
}
var cacheDirs = ["songs", "imgs"]
for (var i = 0; i < cacheDirs.length; i++) {
if (!fs.existsSync(path.join(cachePath, cacheDirs[i]))) {
fs.mkdirSync(path.join(cachePath, cacheDirs[i]))
}
}
const songCache = path.join(cachePath, "songs"),
imgCache = path.join(cachePath, "imgs")
var app = express()
@ -18,16 +30,16 @@ const imgWaitMax = 2
app.get("/cached/*", (req, res) => {
var imgURL = req.originalUrl
imgURL = imgURL.substring(imgURL.indexOf("/", 2) + 1)
var imgData = cachedImages[imgURL]
var imgData = cachedImages.imgs[imgURL]
var imgWait = 0
function waitForImage() {
if (imgWait < imgWaitMax) {
imgWait += 0.1
setTimeout(() => {
imgData = cachedImages[imgURL]
imgData = cachedImages.imgs[imgURL]
if (imgData) {
fs.createReadStream(path.join(cachePath, imgData.file)).pipe(res)
fs.createReadStream(path.join(imgCache, imgData.file)).pipe(res)
} else {
waitForImage()
}
@ -37,7 +49,7 @@ app.get("/cached/*", (req, res) => {
}
}
if (imgData) {
fs.createReadStream(path.join(cachePath, imgData.file)).pipe(res)
fs.createReadStream(path.join(imgCache, imgData.file)).pipe(res)
} else {
waitForImage()
}
@ -122,25 +134,25 @@ function socketeer() {
const activity = lanyardData.activities[index];
var imgType = undefined
var imgRes = "256x256/"
var imgRes = "512x512/"
for (var i = 0; i < 2; i++) {
if (get_img_url(activity, imgType)) {
var url = get_img_url(activity, imgType)
var fn = Object.keys(cachedImages).length + "." + imgExtension
var fp = path.join(cachePath, fn)
var fn = Object.keys(cachedImages.imgs).length + "." + imgExtension
var fp = path.join(imgCache, fn)
if (!cachedImages[url]) {
if (!cachedImages.imgs[url]) {
const response = await (await fetch(thumborURL + imgRes + thumborArgs + url)).arrayBuffer()
fs.writeFileSync(fp, Buffer.from(response))
cachedImages[url] = {
cachedImages.imgs[url] = {
"file": fn,
"lastUpdated": Date.now()
}
fs.writeFileSync(cacheFile, JSON.stringify(cachedImages))
}
imgType = "small_image"
imgRes = "64x64/"
imgRes = "128x128/"
}
}
}