This repository has been archived on 2023-11-06. You can view files and clone it, but cannot push or open issues or pull requests.
beatsaber-overlay/pages/api/beatsaver/art/[hash].js

27 lines
972 B
JavaScript
Raw Normal View History

2022-10-14 19:00:47 +00:00
import fs from "fs";
import fetch from "node-fetch";
import path from "path";
import sharp from "sharp";
2022-10-19 16:33:56 +00:00
import cacheDir from "../../../../src/caches/SongArtCacheDir";
export default async function handler(req, res) {
2022-10-14 19:00:47 +00:00
const mapHash = req.query.hash.replace("custom_level_", "").toLowerCase();
const ext = req.query.ext;
2022-10-14 19:00:47 +00:00
const imagePath = cacheDir + path.sep + mapHash + "." + ext;
const exists = fs.existsSync(imagePath);
if (!exists) {
const data = await fetch(`https://eu.cdn.beatsaver.com/${mapHash}.${ext}`);
2022-10-19 17:56:46 +00:00
let buffer = await data.buffer(); // Change to arrayBuffer at some point to make it shush
2022-10-14 19:00:47 +00:00
buffer = await sharp(buffer).resize(150, 150).toBuffer();
fs.writeFileSync(imagePath, buffer);
res.setHeader("Content-Type", "image/" + ext);
res.send(buffer);
console.log('Song Art Cache - Added song "' + mapHash + '"');
return;
}
const buffer = fs.readFileSync(imagePath);
2022-10-19 16:45:12 +00:00
res.setHeader("Content-Type", "image/" + ext);
2022-10-14 19:00:47 +00:00
res.send(buffer);
}