File size: 3,767 Bytes
39e65f6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
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
}; |