|
import { ParametersUrl } from '../lib/scrapper.js'; |
|
import fetch from "node-fetch"; |
|
import express from 'express'; |
|
import { Readable } from "stream"; |
|
import sharp from "sharp"; |
|
|
|
const CarbonRoutes = express.Router(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
function paramCode(code) { |
|
const params = new URLSearchParams({ code: code }); |
|
return params.toString(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
async function MakerCarbon(args) { |
|
const url = ParametersUrl("maker/carbon"); |
|
try { |
|
const params = paramCode(args); |
|
const finalUrl = `${url}?${params}`; |
|
const response = await fetch(finalUrl, { |
|
method: "GET", |
|
}); |
|
|
|
if (!response.ok) { |
|
console.error(`API Error: ${response.status}`); |
|
return null; |
|
} |
|
|
|
return await response.arrayBuffer(); |
|
} catch (error) { |
|
console.error("Error fetching data:", error.message); |
|
return null; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CarbonRoutes.get("/api/v1/maker/carbon", async (req, res) => { |
|
try { |
|
const code = req.query.code; |
|
if (!code) { |
|
return res.status(400).send("Query parameter 'code' is missing"); |
|
} |
|
|
|
const imageBytes = await MakerCarbon(code); |
|
if (!imageBytes) { |
|
return res.status(500).json({ error: "Failed to fetch image bytes" }); |
|
} |
|
|
|
const buffer = Buffer.isBuffer(imageBytes) ? imageBytes : Buffer.from(imageBytes); |
|
|
|
const processedImage = await sharp(buffer).jpeg().toBuffer(); |
|
res.set("Content-Type", "image/jpeg"); |
|
Readable.from(processedImage).pipe(res); |
|
} catch (error) { |
|
console.error("Error processing image:", error.message); |
|
res.status(500).json({ error: "Internal server error" }); |
|
} |
|
}); |
|
|
|
export { CarbonRoutes }; |
|
|