| const axios = require('axios'); | |
| const fs = require('fs'); | |
| const os = require('os'); | |
| const path = require('path'); | |
| async function img2pixel(imgUrl) { | |
| let tempFilePath = null; | |
| try { | |
| const response = await axios.get(imgUrl, { | |
| responseType: 'arraybuffer', | |
| timeout: 30000 | |
| }); | |
| const imageBuffer = Buffer.from(response.data); | |
| const tempFileName = `img2pixel_${Date.now()}_${Math.random().toString(36).substring(7)}.jpg`; | |
| tempFilePath = path.join(os.tmpdir(), tempFileName); | |
| fs.writeFileSync(tempFilePath, imageBuffer); | |
| const filename = imgUrl.split('/').pop().split('?')[0] || tempFileName; | |
| const contentType = 'image/jpeg'; | |
| const presignedResponse = await axios.post( | |
| 'https://pixelartgenerator.app/api/upload/presigned-url', | |
| { | |
| filename: filename, | |
| contentType: contentType, | |
| type: 'pixel-art-source' | |
| }, | |
| { | |
| headers: { | |
| 'Content-Type': 'application/json' | |
| } | |
| } | |
| ); | |
| const { uploadUrl, key, publicUrl } = presignedResponse.data.data; | |
| await axios.put(uploadUrl, imageBuffer, { | |
| headers: { | |
| 'Content-Type': contentType | |
| } | |
| }); | |
| if (tempFilePath && fs.existsSync(tempFilePath)) { | |
| fs.unlinkSync(tempFilePath); | |
| tempFilePath = null; | |
| } | |
| const generateResponse = await axios.post( | |
| 'https://pixelartgenerator.app/api/pixel/generate', | |
| { | |
| size: '1:1', | |
| type: 'image', | |
| imageKey: key, | |
| prompt: '' | |
| }, | |
| { | |
| headers: { | |
| 'Content-Type': 'application/json' | |
| } | |
| } | |
| ); | |
| const { taskId } = generateResponse.data.data; | |
| let attempts = 0; | |
| const maxAttempts = 60; | |
| while (attempts < maxAttempts) { | |
| const statusResponse = await axios.get( | |
| `https://pixelartgenerator.app/api/pixel/status?taskId=${taskId}` | |
| ); | |
| const { status, progress, images, error } = statusResponse.data.data; | |
| if (status === 'SUCCESS' && images.length > 0) { | |
| return images[0]; | |
| } | |
| if (error) { | |
| throw new Error(`Generation failed: ${error}`); | |
| } | |
| attempts++; | |
| await new Promise(resolve => setTimeout(resolve, 3000)); | |
| } | |
| throw new Error('Timeout: Generation took too long'); | |
| } catch (error) { | |
| if (tempFilePath && fs.existsSync(tempFilePath)) { | |
| try { | |
| fs.unlinkSync(tempFilePath); | |
| } catch (cleanupError) { | |
| console.error('Failed to cleanup temp file:', cleanupError); | |
| } | |
| } | |
| throw new Error(`img2pixel error: ${error.message}`); | |
| } | |
| } | |
| const handler = async (req, res) => { | |
| try { | |
| const { img, key } = req.query; | |
| if (!img) { | |
| return res.status(400).json({ | |
| author: "Herza", | |
| success: false, | |
| error: 'Missing required parameter: img (image URL)' | |
| }); | |
| } | |
| if (!key) { | |
| return res.status(400).json({ | |
| author: "Herza", | |
| success: false, | |
| error: 'Missing required parameter: key' | |
| }); | |
| } | |
| try { | |
| new URL(img); | |
| } catch (e) { | |
| return res.status(400).json({ | |
| author: "Herza", | |
| success: false, | |
| error: 'Invalid image URL format' | |
| }); | |
| } | |
| const result = await img2pixel(img); | |
| res.json({ | |
| author: "Herza", | |
| success: true, | |
| data: { | |
| result: result | |
| } | |
| }); | |
| } catch (error) { | |
| res.status(500).json({ | |
| author: "Herza", | |
| success: false, | |
| error: error.message | |
| }); | |
| } | |
| }; | |
| module.exports = { | |
| name: 'Image to Pixel Art', | |
| description: 'Convert image to pixel art style', | |
| type: 'GET', | |
| routes: ['api/AI/img2pixel'], | |
| tags: ['ai', 'image', 'pixel-art'], | |
| main: ['AI'], | |
| parameters: ['img', 'key'], | |
| enabled: true, | |
| handler | |
| }; |