More api fixes

This commit is contained in:
Liam 2022-10-20 18:27:43 +01:00
parent 923318af86
commit 253cd4a103
4 changed files with 12 additions and 8 deletions

@ -12,25 +12,26 @@ const KEY = "BL_MAP_STAR_";
*/
export default async function handler(req, res) {
const mapHash = req.query.hash.replace("custom_level_", "").toLowerCase();
const difficulty = req.query.difficulty;
const difficulty = req.query.difficulty.replace(" ", "");
const characteristic = req.query.characteristic;
const exists = await RedisUtils.exists(`${KEY}${mapHash}`);
if (exists) {
const data = await RedisUtils.getValue(
`${KEY}${difficulty}-${characteristic}-${mapHash}`
`${KEY}${difficulty}-${characteristic}-${mapHash}`.replace(" ", "")
);
res.setHeader("Cache-Status", "hit");
return res.status(200).json({
status: "OK",
stars: Number.parseFloat(data),
difficulty: difficulty,
});
}
const data = await fetch(
WebsiteTypes.BeatLeader.ApiUrl.MapData.replace("%h", mapHash)
.replace("%d", difficulty.replace("+", "Plus"))
.replace("%d", difficulty)
.replace("%m", characteristic),
{
headers: {
@ -46,7 +47,7 @@ export default async function handler(req, res) {
}
const json = await data.json();
RedisUtils.setValue(
`${KEY}${difficulty}-${characteristic}-${mapHash}`,
`${KEY}${difficulty}-${characteristic}-${mapHash}`.replace(" ", ""),
json.difficulty.stars
);
res.setHeader("Cache-Status", "miss");

@ -14,7 +14,7 @@ export default async function handler(req, res) {
const mapHash = req.query.hash.replace("custom_level_", "").toLowerCase();
const ext = req.query.ext || "jpg";
const exists = await RedisUtils.exists(`${KEY}${mapHash}`);
const exists = await RedisUtils.exists(`${KEY}${mapHash}`.replace(" ", ""));
if (exists) {
const data = await RedisUtils.getValue(`${KEY}${mapHash}`);
const buffer = Buffer.from(data, "base64");
@ -38,7 +38,7 @@ export default async function handler(req, res) {
buffer = await sharp(buffer).resize(150, 150).toBuffer();
const bytes = buffer.toString("base64");
await RedisUtils.setValue(`${KEY}${mapHash}`, bytes);
await RedisUtils.setValue(`${KEY}${mapHash}`.replace(" ", ""), bytes);
res.setHeader("Cache-Status", "miss");
res.setHeader("Content-Type", "image/" + ext);
res.status(200).send(buffer);

@ -6,7 +6,10 @@ export default async function handler(req, res) {
const mapData = await Utils.getMapData(mapHash.replace("custom_level_", ""));
if (mapData === undefined) {
// Check if a map hash was provided
return res.status(200).json({ error: true, message: "Unknown map" });
return res.status(404).json({
status: 404,
message: "Unknown Map Hash",
});
}
const data = {
// The maps data from the provided map hash

@ -271,7 +271,7 @@ export default class Overlay extends Component {
let mapHash = levelId.replace("custom_level_", "");
const mapStars = await LeaderboardType.BeatLeader.getMapStarCount(
mapHash,
difficulty,
difficulty.replace("+", "Plus"),
characteristic
);
this.setState({ mapStarCount: mapStars });