fix: sanitize artist, album, and mbid inputs for cover image retrieval

This commit is contained in:
snusxd
2026-04-13 17:42:25 +03:00
parent e858921ca7
commit 063dd2e33e

View File

@@ -27,22 +27,31 @@ async function get_image_from_server(
album: string,
mbid: string,
) {
const MBFilePath = `public/covers/mb/${mbid}.png`;
const iTunesFilePath = `public/covers/itunes/${artist}/${album}.png`;
const safeArtist = artist
.replace(/[^a-zA-Z0-9_\-\.\u0400-\u04FF\s]/g, "")
.trim();
const safeAlbum = album
.replace(/[^a-zA-Z0-9_\-\.\u0400-\u04FF\s]/g, "")
.trim();
const safeMbid = mbid.replace(/[^a-zA-Z0-9\-]/g, "");
const MBFilePath = `public/covers/mb/${safeMbid}.png`;
const iTunesFilePath = `public/covers/itunes/${safeArtist}/${safeAlbum}.png`;
let resultFilePath = MBFilePath;
if (fs.existsSync(MBFilePath) || fs.existsSync(iTunesFilePath)) {
if (fs.existsSync(MBFilePath)) resultFilePath = MBFilePath;
else resultFilePath = iTunesFilePath;
console.log("[DEBUG]: cover found on server");
if (fs.existsSync(MBFilePath)) {
resultFilePath = MBFilePath;
} else if (fs.existsSync(iTunesFilePath)) {
resultFilePath = iTunesFilePath;
} else {
try {
await download_image_from_itunes(artist, album);
resultFilePath = iTunesFilePath;
} catch (first_error) {
try {
await download_image_from_caa(mbid);
if (safeMbid && safeMbid != "0") await download_image_from_caa(mbid);
else throw first_error;
resultFilePath = MBFilePath;
} catch (secong_error) {
console.error(
@@ -50,7 +59,6 @@ async function get_image_from_server(
first_error,
secong_error,
);
// resultFilePath = "public/covers/0.png";
throw Error("cover not found");
}
}