-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
447 lines (384 loc) · 11.8 KB
/
Copy pathserver.js
File metadata and controls
447 lines (384 loc) · 11.8 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
const express = require("express");
const path = require("path");
const axios = require("axios");
require("dotenv").config();
const app = express();
const PORT = process.env.PORT || 3000;
// 1. Pengaturan View Engine (EJS)
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
// 2. Middleware & Folder Statis
// File di dalam folder 'public' (seperti output.css atau JS client) bisa diakses langsung
app.use(express.static(path.join(__dirname, "public")));
// Increase body size limit for Vercel
app.use(express.json({ limit: "50mb" }));
app.use(express.urlencoded({ limit: "50mb", extended: true }));
// Vercel Web Analytics: The tracking script is injected in the head partial (views/partials/head.ejs)
// When deployed to Vercel, the /_vercel/insights/script.js endpoint is automatically available
// The analytics will track page views and user interactions automatically
// 3. Rute Halaman (Routing)
app.get("/", (req, res) => {
res.render("dashboard", {
title: "Stackmasters - Generator",
currentPage: "workstation",
credits: "1,250",
});
});
app.get("/enhancer", (req, res) => {
res.render("enhancer", {
title: "StackMasters - AI Enhancer",
currentPage: "enhancer",
credits: "1,250",
});
});
app.get("/gallery", (req, res) => {
res.render("gallery", {
title: "Stackmasters - Galeri",
currentPage: "gallery",
credits: "1,250",
});
});
app.get("/removebg", (req, res) => {
res.render("removebg", {
title: "Stackmasters - Remove Background",
currentPage: "removebg",
credits: "1,250",
});
});
// 4. Endpoint Proxy API (Contoh Integrasi Pihak Ketiga)
// Ini agar API Key Anda tetap aman di server (tidak terlihat di browser)
app.post("/api/generate", async (req, res) => {
try {
const { prompt } = req.body;
if (!prompt) {
return res.status(400).json({
success: false,
error: "Prompt tidak boleh kosong",
});
}
console.log("Menerima prompt:", prompt);
// Panggil API Gimita
const apiUrl = `https://api.gimita.id/api/ai/txt2img?prompt=${encodeURIComponent(
prompt
)}`;
const response = await axios.get(apiUrl);
if (response.data.success) {
console.log("Gambar berhasil di-generate:", response.data.data.image_url);
res.json({
success: true,
message: "Gambar berhasil dibuat!",
data: {
image_url: response.data.data.image_url,
engine: response.data.data.engine,
prompt: response.data.data.prompt,
},
});
} else {
res.status(500).json({
success: false,
error: "Gagal generate gambar dari API",
});
}
} catch (error) {
console.error("Error saat generate:", error.message);
res.status(500).json({
success: false,
error: error.message || "Terjadi kesalahan saat generate gambar",
});
}
});
// Endpoint untuk download gambar (proxy untuk menghindari CORS)
app.get("/api/download-image", async (req, res) => {
try {
const { url } = req.query;
if (!url) {
return res.status(400).json({
success: false,
error: "URL gambar tidak boleh kosong",
});
}
console.log("Downloading image from:", url);
// Fetch gambar dari URL eksternal
const response = await axios.get(url, {
responseType: "arraybuffer",
});
// Set headers untuk download
res.setHeader(
"Content-Type",
response.headers["content-type"] || "image/jpeg"
);
res.setHeader(
"Content-Disposition",
'attachment; filename="Stackmasters-' + Date.now() + '.jpg"'
);
// Kirim gambar
res.send(response.data);
} catch (error) {
console.error("Error downloading image:", error.message);
res.status(500).json({
success: false,
error: "Gagal mengunduh gambar",
});
}
});
// Endpoint untuk AI Enhancer
app.post("/api/enhance", async (req, res) => {
try {
const { url, prompt, format, size } = req.body;
if (!url) {
return res.status(400).json({
success: false,
error: "URL gambar tidak boleh kosong",
});
}
if (!prompt) {
return res.status(400).json({
success: false,
error: "Prompt tidak boleh kosong",
});
}
console.log("Enhancing image:", url, "with prompt:", prompt);
// Panggil API Gimita AI Enhancer
const apiUrl = `https://api.gimita.id/api/ai/aienhancer?format=${
format || "png"
}&prompt=${encodeURIComponent(prompt)}&size=${
size || "2K"
}&url=${encodeURIComponent(url)}`;
const response = await axios.get(apiUrl);
if (response.data.success) {
console.log("Image enhanced successfully:", response.data.data.output);
res.json({
success: true,
message: "Gambar berhasil di-enhance!",
data: {
task_id: response.data.data.task_id,
input: response.data.data.input,
output: response.data.data.output,
},
});
} else {
res.status(500).json({
success: false,
error: "Gagal enhance gambar dari API",
});
}
} catch (error) {
console.error("Error enhancing image:", error.message);
res.status(500).json({
success: false,
error: error.message || "Terjadi kesalahan saat enhance gambar",
});
}
});
// Endpoint untuk upload gambar (proxy)
const multer = require("multer");
const FormData = require("form-data");
// Configure multer with size limit
const upload = multer({
storage: multer.memoryStorage(),
limits: {
fileSize: 10 * 1024 * 1024, // 10MB limit
},
});
app.post("/api/upload-image", upload.single("file"), async (req, res) => {
try {
if (!req.file) {
return res.status(400).json({
success: false,
error: "File tidak ditemukan",
});
}
console.log(
"Uploading file:",
req.file.originalname,
"Size:",
req.file.size
);
// Upload ke API Ryzumi CDN
const formData = new FormData();
formData.append("file", req.file.buffer, {
filename: req.file.originalname,
contentType: req.file.mimetype,
});
const response = await axios.post(
"https://api.ryzumi.vip/api/uploader/ryzencdn",
formData,
{
headers: {
...formData.getHeaders(),
},
maxContentLength: Infinity,
maxBodyLength: Infinity,
timeout: 60000, // 60 seconds timeout
}
);
if (response.data.success) {
console.log("File uploaded successfully:", response.data.url);
// Return URL langsung dari Ryzumi CDN
res.json({
success: true,
message: "File berhasil diupload!",
data: {
filename: response.data.fileName,
size: response.data.size,
url: response.data.url, // URL langsung dari Ryzumi CDN
},
});
} else {
console.error("Upload failed:", response.data);
res.status(500).json({
success: false,
error: "Gagal upload file ke server",
});
}
} catch (error) {
console.error("Error uploading file:", error.message);
console.error("Error details:", error.response?.data || error);
res.status(500).json({
success: false,
error: error.message || "Terjadi kesalahan saat upload file",
details: error.response?.data || null,
});
}
});
// Endpoint proxy untuk gambar (hide api.gimita.id)
app.get("/api/proxy-image/:filename", async (req, res) => {
try {
const { filename } = req.params;
const imageUrl = `http://api.gimita.id/files/${filename}`;
console.log("Proxying image:", imageUrl);
const response = await axios.get(imageUrl, {
responseType: "arraybuffer",
});
res.setHeader(
"Content-Type",
response.headers["content-type"] || "image/jpeg"
);
res.send(response.data);
} catch (error) {
console.error("Error proxying image:", error.message);
res.status(500).send("Error loading image");
}
});
// Endpoint untuk enhance prompt dengan GPT-4
app.post("/api/enhance-prompt", async (req, res) => {
try {
const { basePrompt, style, lighting, camera } = req.body;
if (!basePrompt) {
return res.status(400).json({
success: false,
error: "Base prompt tidak boleh kosong",
});
}
console.log("Enhancing prompt:", basePrompt);
// Build enhanced prompt
let enhancedPromptTemplate = `You are an elite professional prompt engineer and visual designer. Enhance and expand the following base prompt:
"${basePrompt}"
Transform it into a highly detailed, visually rich, and high-quality text-to-image prompt. Add appropriate details for subject appearance, environment, composition, lighting, camera angle, art style, and overall mood. Improve clarity, realism, and aesthetic quality while staying faithful to the original idea.`;
if (style) {
enhancedPromptTemplate += `\n\nApply a professional visual style: ${style}.`;
}
if (lighting) {
enhancedPromptTemplate += ` Use lighting: ${lighting}.`;
}
if (camera) {
enhancedPromptTemplate += ` Camera/View: ${camera}.`;
}
enhancedPromptTemplate += `\n\nEnhance textures, colors, and details. Ensure the final prompt is clean, descriptive, and optimized for modern AI image generation models, producing a visually striking, coherent, and high-resolution result.`;
// Call GPT-4 API
const apiUrl = `https://api.gimita.id/api/ai/gpt4?prompt=${encodeURIComponent(
enhancedPromptTemplate
)}`;
const response = await axios.get(apiUrl);
if (response.data.success) {
console.log("Prompt enhanced successfully");
res.json({
success: true,
message: "Prompt berhasil ditingkatkan!",
data: {
enhancedPrompt: response.data.data.answer,
originalPrompt: basePrompt,
},
});
} else {
res.status(500).json({
success: false,
error: "Gagal enhance prompt dari API",
});
}
} catch (error) {
console.error("Error enhancing prompt:", error.message);
res.status(500).json({
success: false,
error: error.message || "Terjadi kesalahan saat enhance prompt",
});
}
});
// Endpoint untuk remove background
app.post("/api/removebg", async (req, res) => {
try {
const { url } = req.body;
if (!url) {
return res.status(400).json({
success: false,
error: "URL gambar tidak boleh kosong",
});
}
console.log("Removing background from:", url);
// Panggil API Gimita Remove BG
const apiUrl = `https://api.gimita.id/api/maker/removebg?url=${encodeURIComponent(
url
)}`;
const response = await axios.get(apiUrl);
if (response.data.success) {
console.log(
"Background removed successfully:",
response.data.data.result
);
res.json({
success: true,
message: "Background berhasil dihapus!",
data: {
result: response.data.data.result,
},
});
} else {
res.status(500).json({
success: false,
error: "Gagal menghapus background dari API",
});
}
} catch (error) {
console.error("Error removing background:", error.message);
res.status(500).json({
success: false,
error: error.message || "Terjadi kesalahan saat menghapus background",
});
}
});
// Error handler for multer
app.use((error, req, res, next) => {
if (error instanceof multer.MulterError) {
if (error.code === "LIMIT_FILE_SIZE") {
return res.status(400).json({
success: false,
error: "File terlalu besar! Maksimal 10MB",
});
}
return res.status(400).json({
success: false,
error: error.message,
});
}
next(error);
});
// 5. Jalankan Server
app.listen(PORT, () => {
console.log(`
🚀 Stackmasters Server berjalan!
🌍 URL: http://localhost:${PORT}
📂 Folder Views: ./views
`);
});
// Export for Vercel serverless
module.exports = app;