2022-10-14 19:00:47 +00:00
|
|
|
import fetch from "node-fetch";
|
|
|
|
import sharp from "sharp";
|
2022-10-20 17:00:27 +00:00
|
|
|
import RedisUtils from "../../../../src/utils/redisUtils";
|
2022-02-20 23:52:57 +00:00
|
|
|
|
2022-10-20 17:00:27 +00:00
|
|
|
const KEY = "BS_MAP_ART_";
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {Request} req
|
|
|
|
* @param {Response} res
|
|
|
|
* @returns
|
|
|
|
*/
|
2022-02-20 23:52:57 +00:00
|
|
|
export default async function handler(req, res) {
|
2022-10-14 19:00:47 +00:00
|
|
|
const mapHash = req.query.hash.replace("custom_level_", "").toLowerCase();
|
2022-10-20 17:00:27 +00:00
|
|
|
const ext = req.query.ext || "jpg";
|
|
|
|
|
|
|
|
const exists = await RedisUtils.exists(`${KEY}${mapHash}`);
|
|
|
|
if (exists) {
|
|
|
|
const data = await RedisUtils.getValue(`${KEY}${mapHash}`);
|
|
|
|
const buffer = Buffer.from(data, "base64");
|
|
|
|
res.writeHead(200, {
|
|
|
|
"Content-Type": "image/" + ext,
|
|
|
|
"Content-Length": buffer.length,
|
|
|
|
"Cache-Status": "hit",
|
|
|
|
});
|
|
|
|
return res.end(buffer);
|
|
|
|
}
|
2022-02-20 23:52:57 +00:00
|
|
|
|
2022-10-20 17:00:27 +00:00
|
|
|
const data = await fetch(`https://eu.cdn.beatsaver.com/${mapHash}.${ext}`);
|
|
|
|
if (data.status === 404) {
|
|
|
|
return res.status(404).json({
|
|
|
|
status: 404,
|
|
|
|
message: "Unknown Map Hash",
|
|
|
|
});
|
2022-10-14 19:00:47 +00:00
|
|
|
}
|
2022-10-20 17:00:27 +00:00
|
|
|
|
|
|
|
let buffer = await data.buffer(); // Change to arrayBuffer at some point to make it shush
|
|
|
|
buffer = await sharp(buffer).resize(150, 150).toBuffer();
|
|
|
|
const bytes = buffer.toString("base64");
|
|
|
|
|
|
|
|
await RedisUtils.setValue(`${KEY}${mapHash}`, bytes);
|
|
|
|
res.setHeader("Cache-Status", "miss");
|
2022-10-19 16:45:12 +00:00
|
|
|
res.setHeader("Content-Type", "image/" + ext);
|
2022-10-20 17:00:27 +00:00
|
|
|
res.status(200).send(buffer);
|
2022-02-20 23:52:57 +00:00
|
|
|
}
|