Repository navigation
Expand file tree
/
Copy pathindex.ts
More file actions
266 lines (242 loc) Β· 8.41 KB
/
Copy pathindex.ts
File metadata and controls
266 lines (242 loc) Β· 8.41 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
import express from 'express';
import multer from 'multer';
import cors from 'cors';
import path from 'path';
import fs from 'fs';
import { convertDngToPng } from './dng-converter';
const app = express();
const PORT = process.env.PORT || 3001;
// Configure
8000
CORS
app.use(cors());
app.use(express.json());
// Configure multer for file uploads
const storage = multer.diskStorage({
destination: (req, file, cb) => {
const uploadDir = 'uploads';
if (!fs.existsSync(uploadDir)) {
fs.mkdirSync(uploadDir, { recursive: true });
}
cb(null, uploadDir);
},
filename: (req, file, cb) => {
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);
cb(null, file.fieldname + '-' + uniqueSuffix + path.extname(file.originalname));
}
});
const upload = multer({
storage: storage,
fileFilter: (req, file, cb) => {
// Accept DNG files
if (file.mimetype === 'image/x-adobe-dng' || file.originalname.toLowerCase().endsWith('.dng')) {
cb(null, true);
} else {
cb(new Error('Only DNG files are allowed!'));
}
},
limits: {
fileSize: 50 * 1024 * 1024 // 50MB limit
}
});
// Serve the API mode interface
app.get('/', (req, res) => {
res.send(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DNG to PNG Converter - API Mode Only</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #f5f5f5;
}
.container {
background: white;
padding: 30px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
h1 {
color: #333;
text-align: center;
margin-bottom: 10px;
}
.mode-badge {
background: #007bff;
color: white;
padding: 5px 15px;
border-radius: 20px;
font-size: 14px;
text-align: center;
margin-bottom: 20px;
}
form {
margin: 20px 0;
}
input[type="file"] {
width: 100%;
padding: 10px;
margin: 10px 0;
border: 2px dashed #ccc;
border-radius: 5px;
}
button {
background-color: #007bff;
color: white;
padding: 12px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
width: 100%;
}
button:hover {
background-color: #0056b3;
}
button:disabled {
background-color: #6c757d;
cursor: not-allowed;
}
.result {
margin-top: 20px;
padding: 15px;
border-radius: 5px;
}
.success {
background-color: #d4edda;
color: #155724;
border: 1px solid #c3e6cb;
}
.error {
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
.loading {
background-color: #fff3cd;
color: #856404;
border: 1px solid #ffeaa7;
}
.info {
background-color: #e2f3ff;
color: #004085;
border: 1px solid #b8daff;
margin-bottom: 20px;
padding: 15px;
border-radius: 5px;
}
</style>
</head>
<body>
<div class="container">
<h1>π DNG to PNG Converter</h1>
<div class="mode-badge">API Mode - Server-side Processing Only</div>
<div class="info">
<strong>Standalone API Mode:</strong><br>
This server runs only the API mode for server-side DNG to PNG conversion.<br>
Running on port ${PORT}
</div>
<form id="uploadForm" enctype="multipart/form-data">
<input type="file" id="dngFile" name="dngFile" accept=".dng" required>
<button type="submit" id="convertBtn">Convert to PNG</button>
</form>
<div id="result"></div>
</div>
<script>
document.getElementById('uploadForm').addEventListener('submit', async function(e) {
e.preventDefault();
const fileInput = document.getElementById('dngFile');
const result = document.getElementById('result');
const convertBtn = document.getElementById('convertBtn');
if (!fileInput.files[0]) {
result.innerHTML = '<div class="error">Please select a DNG file.</div>';
return;
}
const formData = new FormData();
formData.append('dngFile', fileInput.files[0]);
// Show loading state
convertBtn.disabled = true;
convertBtn.textContent = 'Converting...';
result.innerHTML = '<div class="loading">Converting DNG file... Please wait.</div>';
try {
const response = await fetch('/convert', {
method: 'POST',
body: formData
});
if (response.ok) {
const data = await response.json();
result.innerHTML = \`
<div class="success">
β
Conversion completed successfully!<br>
<a href="/download/\${data.filename}" download style="color: #155724; font-weight: bold;">π₯ Download PNG file</a>
</div>
\`;
} else {
const error = await response.json();
result.innerHTML = \`<div class="error">β Error: \${error.message}</div>\`;
}
} catch (error) {
result.innerHTML = \`<div class="error">β Conversion failed: \${error.message}</div>\`;
} finally {
convertBtn.disabled = false;
convertBtn.textContent = 'Convert to PNG';
}
});
</script>
</body>
</html>
`);
});
// Convert DNG to PNG endpoint
app.post('/convert', upload.single('dngFile'), async (req, res) => {
try {
if (!req.file) {
return res.status(400).json({ message: 'DNG file is required.' });
}
const inputPath = req.file.path;
const outputFilename = req.file.filename.replace(/\.[^/.]+$/, '.png');
const outputPath = path.join('uploads', outputFilename);
await convertDngToPng(inputPath, outputPath);
// Clean up input file
fs.unlinkSync(inputPath);
res.json({
message: 'Conversion completed successfully.',
filename: outputFilename
});
} catch (error) {
console.error('Conversion error:', error);
res.status(500).json({
message: 'Error occurred during DNG conversion.',
error: error instanceof Error ? error.message : String(error)
});
}
});
// Download converted file
app.get('/download/:filename', (req, res) => {
const filename = req.params.filename;
const filePath = path.join('uploads', filename);
if (fs.existsSync(filePath)) {
res.download(filePath, (err) => {
if (err) {
console.error('Download error:', err);
} else {
// Clean up file after download
setTimeout(() => {
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
}
}, 5000);
}
});
} else {
res.status(404).json({ message: 'File not found.' });
}
});
app.listen(PORT, () => {
console.log(`π DNG to PNG Converter API Server running on http://localhost:${PORT}`);
console.log(`π API-only mode: Server-side conversion with dcraw + Sharp`);