fix: refactor file path handling for cover image downloads
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
|
import path from "node:path";
|
||||||
|
|
||||||
export async function download_image_from_caa(mbid: string) {
|
export async function download_image_from_caa(mbid: string) {
|
||||||
console.log("[DEBUG]: download_image_from_caa:", mbid);
|
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) {
|
} catch (error) {
|
||||||
console.error("Error fetching data:", error);
|
console.error("Error fetching data:", error);
|
||||||
throw error;
|
throw error;
|
||||||
@@ -62,16 +70,22 @@ export async function download_image_from_itunes(
|
|||||||
throw new Error(`iTunes URL not found for ${rawArtist} - ${rawAlbum}`);
|
throw new Error(`iTunes URL not found for ${rawArtist} - ${rawAlbum}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const artist_dir = `public/covers/itunes/${safeArtist}`;
|
const artist_dist = path.join(
|
||||||
if (!fs.existsSync(artist_dir)) {
|
process.cwd(),
|
||||||
fs.mkdirSync(artist_dir, { recursive: true });
|
"public",
|
||||||
|
"covers",
|
||||||
|
"itunes",
|
||||||
|
safeArtist,
|
||||||
|
);
|
||||||
|
if (!fs.existsSync(artist_dist)) {
|
||||||
|
fs.mkdirSync(artist_dist, { recursive: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
const img_response = await axios.get(itunes_url, {
|
const img_response = await axios.get(itunes_url, {
|
||||||
responseType: "arraybuffer",
|
responseType: "arraybuffer",
|
||||||
});
|
});
|
||||||
|
|
||||||
fs.writeFileSync(`${artist_dir}/${safeAlbum}.png`, img_response.data);
|
fs.writeFileSync(`${artist_dist}/${safeAlbum}.png`, img_response.data);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error fetching data:", error);
|
console.error("Error fetching data:", error);
|
||||||
throw error;
|
throw error;
|
||||||
|
|||||||
18
src/index.ts
18
src/index.ts
@@ -1,6 +1,7 @@
|
|||||||
import { Elysia } from "elysia";
|
import { Elysia } from "elysia";
|
||||||
import { cors } from "@elysiajs/cors";
|
import { cors } from "@elysiajs/cors";
|
||||||
import { node } from "@elysiajs/node";
|
import { node } from "@elysiajs/node";
|
||||||
|
import path from "node:path";
|
||||||
|
|
||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
|
|
||||||
@@ -35,8 +36,21 @@ async function get_image_from_server(
|
|||||||
.trim();
|
.trim();
|
||||||
const safeMbid = mbid.replace(/[^a-zA-Z0-9\-]/g, "");
|
const safeMbid = mbid.replace(/[^a-zA-Z0-9\-]/g, "");
|
||||||
|
|
||||||
const MBFilePath = `public/covers/mb/${safeMbid}.png`;
|
const MBFilePath = path.join(
|
||||||
const iTunesFilePath = `public/covers/itunes/${safeArtist}/${safeAlbum}.png`;
|
process.cwd(),
|
||||||
|
"public",
|
||||||
|
"covers",
|
||||||
|
"mb",
|
||||||
|
`${safeMbid}.png`,
|
||||||
|
);
|
||||||
|
const iTunesFilePath = path.join(
|
||||||
|
process.cwd(),
|
||||||
|
"public",
|
||||||
|
"covers",
|
||||||
|
"itunes",
|
||||||
|
safeArtist,
|
||||||
|
`${safeAlbum}.png`,
|
||||||
|
);
|
||||||
|
|
||||||
let resultFilePath = MBFilePath;
|
let resultFilePath = MBFilePath;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user