-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
142 lines (117 loc) · 3.88 KB
/
Copy pathserver.js
File metadata and controls
142 lines (117 loc) · 3.88 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
import express from 'express';
import cors from 'cors';
import path from 'path';
import {fileURLToPath} from 'url';
import fs from 'fs';
import {spawn} from 'child_process';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const PORT = 3000;
app.use(cors());
app.use(express.json());
app.use(express.static('public'));
app.use('/generated', express.static('generated'));
// Ana sayfa
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// Görüntü oluşturma endpoint'i
app.post('/api/generate', (req, res) => {
const {type, orientation, pattern, useSignature, customText} = req.body;
const args = ['index.js'];
if (orientation === 'vertical') {
args.push('-v');
}
if (pattern === 'mandelbrot') {
args.push('-m');
}
if (useSignature) {
args.push('-s');
}
if (type === 'video') {
args.push('--video');
}
// Özel metin varsa -f parametresi ile ekle
if (customText && customText.trim()) {
args.push('-f');
args.push(customText.trim());
}
const nodeProcess = spawn('node', args);
let output = '';
let errorOutput = '';
nodeProcess.stdout.on('data', (data) => {
output += data.toString();
console.log(data.toString());
});
nodeProcess.stderr.on('data', (data) => {
errorOutput += data.toString();
console.error(data.toString());
});
nodeProcess.on('close', (code) => {
if (code === 0) {
// Oluşturulan dosyayı bul
const generatedDir = path.join(__dirname, 'generated');
const files = fs.readdirSync(generatedDir);
// En son oluşturulan dosyayı bul
const latestFile = files
.map(f => ({
name: f,
time: fs.statSync(path.join(generatedDir, f)).mtime.getTime()
}))
.sort((a, b) => b.time - a.time)[0];
if (latestFile) {
res.json({
success: true,
file: `/generated/${latestFile.name}`,
type: latestFile.name.endsWith('.mp4') ? 'video' : 'image',
message: output
});
} else {
res.json({
success: false,
error: 'Dosya oluşturulamadı'
});
}
} else {
res.json({
success: false,
error: errorOutput || 'Oluşturma başarısız'
});
}
});
});
// Oluşturulan dosyaları listeleme
app.get('/api/files', (req, res) => {
const generatedDir = path.join(__dirname, 'generated');
if (!fs.existsSync(generatedDir)) {
return res.json({files: []});
}
const files = fs.readdirSync(generatedDir)
.filter(f => f.endsWith('.bmp') || f.endsWith('.mp4'))
.map(f => ({
name: f,
path: `/generated/${f}`,
type: f.endsWith('.mp4') ? 'video' : 'image',
size: fs.statSync(path.join(generatedDir, f)).size,
created: fs.statSync(path.join(generatedDir, f)).mtime
}))
.sort((a, b) => b.created - a.created);
res.json({files});
});
// Dosya silme
app.delete('/api/files/:filename', (req, res) => {
const filename = req.params.filename;
const filePath = path.join(__dirname, 'generated', filename);
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
res.json({success: true});
} else {
res.json({success: false, error: 'Dosya bulunamadı'});
}
});
app.listen(PORT, () => {
console.log(`\n🚀 Image Generator Web UI`);
console.log(`📍 Server çalışıyor: http://localhost:${PORT}`);
console.log(`\n🎨 Tarayıcınızda açın ve görüntü üretmeye başlayın!\n`);
});