HerzaJ commited on
Commit
39e65f6
·
verified ·
1 Parent(s): 1d65fdd

Create img2pixel.js

Browse files
Files changed (1) hide show
  1. plugins/img2pixel.js +163 -0
plugins/img2pixel.js ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const axios = require('axios');
2
+ const fs = require('fs');
3
+ const os = require('os');
4
+ const path = require('path');
5
+
6
+ async function img2pixel(imgUrl) {
7
+ let tempFilePath = null;
8
+
9
+ try {
10
+ const response = await axios.get(imgUrl, {
11
+ responseType: 'arraybuffer',
12
+ timeout: 30000
13
+ });
14
+
15
+ const imageBuffer = Buffer.from(response.data);
16
+
17
+ const tempFileName = `img2pixel_${Date.now()}_${Math.random().toString(36).substring(7)}.jpg`;
18
+ tempFilePath = path.join(os.tmpdir(), tempFileName);
19
+ fs.writeFileSync(tempFilePath, imageBuffer);
20
+
21
+ const filename = imgUrl.split('/').pop().split('?')[0] || tempFileName;
22
+ const contentType = 'image/jpeg';
23
+
24
+ const presignedResponse = await axios.post(
25
+ 'https://pixelartgenerator.app/api/upload/presigned-url',
26
+ {
27
+ filename: filename,
28
+ contentType: contentType,
29
+ type: 'pixel-art-source'
30
+ },
31
+ {
32
+ headers: {
33
+ 'Content-Type': 'application/json'
34
+ }
35
+ }
36
+ );
37
+
38
+ const { uploadUrl, key, publicUrl } = presignedResponse.data.data;
39
+
40
+ await axios.put(uploadUrl, imageBuffer, {
41
+ headers: {
42
+ 'Content-Type': contentType
43
+ }
44
+ });
45
+
46
+ if (tempFilePath && fs.existsSync(tempFilePath)) {
47
+ fs.unlinkSync(tempFilePath);
48
+ tempFilePath = null;
49
+ }
50
+
51
+ const generateResponse = await axios.post(
52
+ 'https://pixelartgenerator.app/api/pixel/generate',
53
+ {
54
+ size: '1:1',
55
+ type: 'image',
56
+ imageKey: key,
57
+ prompt: ''
58
+ },
59
+ {
60
+ headers: {
61
+ 'Content-Type': 'application/json'
62
+ }
63
+ }
64
+ );
65
+
66
+ const { taskId } = generateResponse.data.data;
67
+
68
+ let attempts = 0;
69
+ const maxAttempts = 60;
70
+
71
+ while (attempts < maxAttempts) {
72
+ const statusResponse = await axios.get(
73
+ `https://pixelartgenerator.app/api/pixel/status?taskId=${taskId}`
74
+ );
75
+
76
+ const { status, progress, images, error } = statusResponse.data.data;
77
+
78
+ if (status === 'SUCCESS' && images.length > 0) {
79
+ return images[0];
80
+ }
81
+
82
+ if (error) {
83
+ throw new Error(`Generation failed: ${error}`);
84
+ }
85
+
86
+ attempts++;
87
+ await new Promise(resolve => setTimeout(resolve, 3000));
88
+ }
89
+
90
+ throw new Error('Timeout: Generation took too long');
91
+
92
+ } catch (error) {
93
+ if (tempFilePath && fs.existsSync(tempFilePath)) {
94
+ try {
95
+ fs.unlinkSync(tempFilePath);
96
+ } catch (cleanupError) {
97
+ console.error('Failed to cleanup temp file:', cleanupError);
98
+ }
99
+ }
100
+ throw new Error(`img2pixel error: ${error.message}`);
101
+ }
102
+ }
103
+
104
+ const handler = async (req, res) => {
105
+ try {
106
+ const { img, key } = req.query;
107
+
108
+ if (!img) {
109
+ return res.status(400).json({
110
+ author: "Herza",
111
+ success: false,
112
+ error: 'Missing required parameter: img (image URL)'
113
+ });
114
+ }
115
+
116
+ if (!key) {
117
+ return res.status(400).json({
118
+ author: "Herza",
119
+ success: false,
120
+ error: 'Missing required parameter: key'
121
+ });
122
+ }
123
+
124
+ try {
125
+ new URL(img);
126
+ } catch (e) {
127
+ return res.status(400).json({
128
+ author: "Herza",
129
+ success: false,
130
+ error: 'Invalid image URL format'
131
+ });
132
+ }
133
+
134
+ const result = await img2pixel(img);
135
+
136
+ res.json({
137
+ author: "Herza",
138
+ success: true,
139
+ data: {
140
+ result: result
141
+ }
142
+ });
143
+
144
+ } catch (error) {
145
+ res.status(500).json({
146
+ author: "Herza",
147
+ success: false,
148
+ error: error.message
149
+ });
150
+ }
151
+ };
152
+
153
+ module.exports = {
154
+ name: 'Image to Pixel Art',
155
+ description: 'Convert image to pixel art style',
156
+ type: 'GET',
157
+ routes: ['api/AI/img2pixel'],
158
+ tags: ['ai', 'image', 'pixel-art'],
159
+ main: ['AI'],
160
+ parameters: ['img', 'key'],
161
+ enabled: true,
162
+ handler
163
+ };