Spaces:
Running
Running
| const axios = require('axios'); | |
| const sharp = require('sharp'); | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| const handler = async (req, res) => { | |
| try { | |
| const { text } = req.query; | |
| if (!text) { | |
| return res.status(400).json({ | |
| success: false, | |
| error: 'Missing required parameter: text' | |
| }); | |
| } | |
| const targetUrl = `https://fast-flux-demo.replicate.workers.dev/api/generate-image?text=${encodeURIComponent(text)}`; | |
| const proxyUrl = `https://proxy-sigma-roan.vercel.app/api/proxy?url=${encodeURIComponent(targetUrl)}`; | |
| const result = await axios.get(proxyUrl, { | |
| responseType: 'arraybuffer' | |
| }); | |
| const buffer = Buffer.from(result.data); | |
| const jpegBuffer = await sharp(buffer) | |
| .jpeg({ quality: 90 }) | |
| .toBuffer(); | |
| const randomName = `flux-${Math.random().toString(36).substring(2, 15)}.jpg`; | |
| const tmpPath = path.join('/tmp', randomName); | |
| fs.writeFileSync(tmpPath, jpegBuffer); | |
| setTimeout(() => { | |
| if (fs.existsSync(tmpPath)) { | |
| fs.unlinkSync(tmpPath); | |
| } | |
| }, 5 * 60 * 1000); | |
| const host = req.headers.host || req.get('host'); | |
| const protocol = req.headers['x-forwarded-proto'] || req.protocol || 'https'; | |
| const imageUrl = `${protocol}://tmp.dashx.dpdns.org/tmp/${randomName}`; | |
| res.json({ | |
| author: "Herza", | |
| success: true, | |
| data: { | |
| img_url: imageUrl | |
| } | |
| }); | |
| } catch (error) { | |
| res.status(500).json({ | |
| success: false, | |
| error: error.message | |
| }); | |
| } | |
| }; | |
| module.exports = { | |
| name: 'Flux AIv2', | |
| description: 'Generate image using Flux AI', | |
| type: 'GET', | |
| routes: ['api/AI/flux2'], | |
| tags: ['ai', 'Flux', 'Image Generator'], | |
| main: ['AI'], | |
| parameters: ['text', 'key'], | |
| enabled: true, | |
| limit: 2, | |
| handler | |
| }; |