fix: refactor file path handling for cover image downloads

This commit is contained in:
snusxd
2026-04-14 13:25:27 +03:00
parent 4bdd791647
commit ba9e0336b3
2 changed files with 35 additions and 7 deletions

View File

@@ -1,5 +1,6 @@
import axios from "axios";
import fs from "node:fs";
import path from "node:path";
export async function download_image_from_caa(mbid: string) {
console.log("[DEBUG]: download_image_from_caa:", mbid);
@@ -14,7 +15,14 @@ export async function download_image_from_caa(mbid: string) {
},
);
fs.writeFileSync(`public/covers/mb/${mbid}.png`, img_response.data);
const mb_dist = path.join(
process.cwd(),
"public",
"covers",
"mb",
`${mbid}.png`,
);
fs.writeFileSync(mb_dist, img_response.data);
} catch (error) {
console.error("Error fetching data:", error);
throw error;
@@ -62,16 +70,22 @@ export async function download_image_from_itunes(
throw new Error(`iTunes URL not found for ${rawArtist} - ${rawAlbum}`);
}
const artist_dir = `public/covers/itunes/${safeArtist}`;
if (!fs.existsSync(artist_dir)) {
fs.mkdirSync(artist_dir, { recursive: true });
const artist_dist = path.join(
process.cwd(),
"public",
"covers",
"itunes",
safeArtist,
);
if (!fs.existsSync(artist_dist)) {
fs.mkdirSync(artist_dist, { recursive: true });
}
const img_response = await axios.get(itunes_url, {
responseType: "arraybuffer",
});
fs.writeFileSync(`${artist_dir}/${safeAlbum}.png`, img_response.data);
fs.writeFileSync(`${artist_dist}/${safeAlbum}.png`, img_response.data);
} catch (error) {
console.error("Error fetching data:", error);
throw error;