|
import fetch from "node-fetch"; |
|
import express from 'express'; |
|
import { Readable } from "stream"; |
|
import sharp from "sharp"; |
|
import * as config from '../config.js'; |
|
const FluxRoutes = express.Router(); |
|
|
|
|
|
|
|
|
|
async function schellwithflux(args) { |
|
const EncodeBaseUrl = "aHR0cHM6Ly9hcGktaW5mZXJlbmNlLmh1Z2dpbmdmYWNlLmNvL21vZGVscy9ibGFjay1mb3Jlc3QtbGFicy9GTFVYLjEtc2NobmVsbA=="; |
|
try { |
|
const response = await fetch(atob(EncodeBaseUrl), { |
|
method: "POST", |
|
headers: { |
|
"Authorization": `Bearer ${config.HUGGING_TOKEN}`, |
|
"Content-Type": "application/json", |
|
}, |
|
body: JSON.stringify({ inputs: args }), |
|
}); |
|
|
|
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; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FluxRoutes.post("/api/v1/fluxai-ai", async (req, res) => { |
|
try { |
|
const query = req.body.query; |
|
if (!query) { |
|
return res.status(400).send("Query parameter is missing"); |
|
} |
|
|
|
const imageBytes = await schellwithflux(query); |
|
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 { FluxRoutes }; |