Kinda pointless to cache this, so cya
This commit is contained in:
parent
ec9cf81a38
commit
6b7d4320b9
@ -1,59 +0,0 @@
|
|||||||
import fetch from "node-fetch";
|
|
||||||
import sharp from "sharp";
|
|
||||||
import { isValidSteamId } from "../../src/helpers/validateSteamId";
|
|
||||||
import { getValue, setValue, valueExists } from "../../src/utils/redisUtils";
|
|
||||||
|
|
||||||
const KEY = "STEAM_AVATAR_";
|
|
||||||
const ext = "jpg";
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param {Request} req
|
|
||||||
* @param {Response} res
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
export default async function handler(req, res) {
|
|
||||||
const steamId = req.query.steamid;
|
|
||||||
const isValid = await isValidSteamId(steamId);
|
|
||||||
if (isValid == false) {
|
|
||||||
return res.status(404).json({
|
|
||||||
status: 404,
|
|
||||||
message: "Unknown Steam Avatar",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const exists = await valueExists(`${KEY}${steamId}`);
|
|
||||||
if (exists) {
|
|
||||||
const data = await getValue(`${KEY}${steamId}`);
|
|
||||||
const buffer = Buffer.from(data, "base64");
|
|
||||||
res.writeHead(200, {
|
|
||||||
"Content-Type": "image/" + ext,
|
|
||||||
"Content-Length": buffer.length,
|
|
||||||
"Cache-Status": "hit",
|
|
||||||
});
|
|
||||||
return res.end(buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
const before = Date.now();
|
|
||||||
const data = await fetch(
|
|
||||||
`https://cdn.scoresaber.com/avatars/${steamId}.${ext}`
|
|
||||||
);
|
|
||||||
if (data.status === 404) {
|
|
||||||
return res.status(404).json({
|
|
||||||
status: 404,
|
|
||||||
message: "Unknown Steam Avatar",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
let buffer = await data.buffer(); // Change to arrayBuffer at some point to make it shush
|
|
||||||
buffer = await sharp(buffer).resize(400, 400).toBuffer();
|
|
||||||
const bytes = buffer.toString("base64");
|
|
||||||
|
|
||||||
await setValue(`${KEY}${steamId}`, bytes);
|
|
||||||
console.log(
|
|
||||||
`[Cache]: Cached Avatar for id ${steamId} in ${Date.now() - before}ms`
|
|
||||||
);
|
|
||||||
res.setHeader("Cache-Status", "miss");
|
|
||||||
res.setHeader("Content-Type", "image/" + ext);
|
|
||||||
res.status(200).send(buffer);
|
|
||||||
}
|
|
@ -443,7 +443,7 @@ export default class Overlay extends Component {
|
|||||||
country={data.country}
|
country={data.country}
|
||||||
countryRank={data.countryRank.toLocaleString()}
|
countryRank={data.countryRank.toLocaleString()}
|
||||||
websiteType={websiteType}
|
websiteType={websiteType}
|
||||||
avatar={`/api/steamavatar?steamid=${id}`}
|
avatar={`https://cdn.scoresaber.com/avatars/${id}.jpg`}
|
||||||
loadedDuringSong={this.state.loadedDuringSong}
|
loadedDuringSong={this.state.loadedDuringSong}
|
||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
|
Reference in New Issue
Block a user