-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
308 lines (268 loc) · 8.55 KB
/
server.js
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
const express = require('express');
const fs = require('fs').promises;
const path = require('path');
const multer = require('multer');
const sharp = require('sharp');
const app = express();
const port = process.env.PORT || 3000;
const dataDir = 'data';
const dataFile = path.join(dataDir, 'wishlist.json');
// Currency mapping
const CURRENCY_SYMBOLS = {
USD: '$',
EUR: '€',
GBP: '£',
JPY: '¥',
CNY: '¥',
KRW: '₩',
INR: '₹',
RUB: '₽',
BRL: 'R$',
AUD: 'A$',
CAD: 'C$',
CHF: 'Fr',
HKD: 'HK$',
NZD: 'NZ$',
SEK: 'kr',
NOK: 'kr',
DKK: 'kr',
PLN: 'zł',
THB: '฿',
MXN: '$',
};
// Configuration
const config = {
port: process.env.PORT || 3000,
pinProtection: process.env.DUMBDO_PIN ? 'enabled' : 'disabled',
maxImageSize: 1024,
currencyCode: (process.env.DUMBWISH_CURRENCY || 'USD').toUpperCase(),
title: process.env.DUMBWISH_TITLE || 'DumbWish',
get currencySymbol() {
return CURRENCY_SYMBOLS[this.currencyCode] || '$';
}
};
// Configure multer for image uploads
const upload = multer({
storage: multer.memoryStorage(),
limits: {
fileSize: 10 * 1024 * 1024, // 10MB max file size
},
fileFilter: (req, file, cb) => cb(null, file.mimetype.startsWith('image/'))
});
// Ensure data directory exists
const ensureDataDir = async () => {
try {
await fs.mkdir(dataDir, { recursive: true });
try {
await fs.access(dataFile);
} catch {
await fs.writeFile(dataFile, '[]');
}
} catch (err) {
console.error('Error initializing data directory:', err);
process.exit(1); // Exit if we can't initialize data directory
}
};
// Process and save image
async function processImage(buffer, id) {
const imagePath = path.join(dataDir, `${id}.jpeg`);
// Get image metadata
const metadata = await sharp(buffer).metadata();
// Calculate dimensions maintaining aspect ratio
let width = metadata.width;
let height = metadata.height;
const maxDimension = config.maxImageSize;
if (width > maxDimension || height > maxDimension) {
const ratio = Math.min(maxDimension / width, maxDimension / height);
width = Math.round(width * ratio);
height = Math.round(height * ratio);
}
await sharp(buffer)
.resize(width, height, {
fit: 'inside',
withoutEnlargement: true
})
.jpeg({
quality: 80,
chromaSubsampling: '4:4:4' // Better quality for images with text
})
.toFile(imagePath);
return `/api/images/${id}.jpeg`;
}
// Delete image if exists
async function deleteImage(id) {
try {
await fs.unlink(path.join(dataDir, `${id}.jpeg`));
} catch (err) {
// Ignore if file doesn't exist
if (err.code !== 'ENOENT') {
console.error('Error deleting image:', err);
}
}
}
app.use(express.json());
app.use(express.static('public'));
// Serve images
app.use('/api/images', express.static(dataDir));
// Error handler middleware
const errorHandler = (err, req, res, next) => {
console.error('Error:', err);
res.status(err.status || 500).json({ error: err.message || 'Internal server error' });
};
// PIN auth middleware
const auth = (req, res, next) => {
const pin = process.env.DUMBDO_PIN;
if (!pin) return next();
const authHeader = req.headers.authorization;
if (!authHeader) {
const err = new Error('No authorization header');
err.status = 401;
return next(err);
}
const providedPin = authHeader.split(' ')[1];
if (providedPin !== pin) {
const err = new Error('Invalid PIN');
err.status = 401;
return next(err);
}
next();
};
// Get all wishlist items
app.get('/api/wishlist', async (req, res, next) => {
try {
const data = await fs.readFile(dataFile, 'utf8');
res.json({
items: JSON.parse(data),
config: {
currencySymbol: config.currencySymbol,
currencyCode: config.currencyCode
}
});
} catch (err) {
next(err);
}
});
// Upload image for item
app.post('/api/wishlist/image/:id', auth, upload.single('image'), async (req, res) => {
try {
if (!req.file) {
return res.status(400).json({ error: 'No image file provided' });
}
const id = parseInt(req.params.id);
const imageUrl = await processImage(req.file.buffer, id);
res.json({ imageUrl });
} catch (err) {
console.error('Error processing image:', err);
res.status(500).json({ error: 'Error processing image' });
}
});
// Add new wishlist item (protected)
app.post('/api/wishlist', auth, upload.single('image'), async (req, res, next) => {
try {
const { title, url, price, note } = req.body;
if (!title) {
const err = new Error('Title is required');
err.status = 400;
throw err;
}
const data = JSON.parse(await fs.readFile(dataFile, 'utf8'));
const id = Date.now();
let image = req.body.image || null;
if (req.file) {
image = await processImage(req.file.buffer, id);
}
const newItem = {
id,
title,
url,
price,
note,
image,
dateAdded: new Date().toISOString()
};
data.push(newItem);
await fs.writeFile(dataFile, JSON.stringify(data, null, 2));
res.json(newItem);
} catch (err) {
next(err);
}
});
// Update wishlist item (protected)
app.put('/api/wishlist/:id', auth, upload.single('image'), async (req, res) => {
try {
const { title, url, price, note } = req.body;
if (!title) {
res.status(400).json({ error: 'Title is required' });
return;
}
const data = JSON.parse(await fs.readFile(dataFile, 'utf8'));
const itemIndex = data.findIndex(item => item.id === parseInt(req.params.id));
if (itemIndex === -1) {
res.status(404).json({ error: 'Item not found' });
return;
}
let image = req.body.image || data[itemIndex].image;
// Process uploaded image if exists
if (req.file) {
image = await processImage(req.file.buffer, data[itemIndex].id);
}
const updatedItem = {
...data[itemIndex],
title,
url,
price,
note,
image,
dateUpdated: new Date().toISOString()
};
data[itemIndex] = updatedItem;
await fs.writeFile(dataFile, JSON.stringify(data, null, 2));
res.json(updatedItem);
} catch (err) {
res.status(500).json({ error: 'Error updating item' });
}
});
// Delete wishlist item (protected)
app.delete('/api/wishlist/:id', auth, async (req, res) => {
try {
const id = parseInt(req.params.id);
const data = JSON.parse(await fs.readFile(dataFile, 'utf8'));
const newData = data.filter(item => item.id !== id);
await fs.writeFile(dataFile, JSON.stringify(newData, null, 2));
// Delete associated image
await deleteImage(id);
res.json({ success: true });
} catch (err) {
res.status(500).json({ error: 'Error deleting item' });
}
});
app.get('/', (req, res) => {
// Read and serve index.html with dynamic title
fs.readFile('public/index.html', 'utf8')
.then(content => {
content = content.replace('<title>DumbWish - Simple Wishlist</title>', `<title>${config.title}</title>`);
res.send(content);
})
.catch(err => {
console.error('Error reading index.html:', err);
res.status(500).send('Server Error');
});
});
// Serve static files except index.html
app.use(express.static('public', {
index: false
}));
// Use error handler
app.use(errorHandler);
ensureDataDir().then(() => {
app.listen(port, () => {
console.log('\x1b[32m%s\x1b[0m', `🎁 ${config.title}`);
console.log('\x1b[36m%s\x1b[0m', '--------------------');
console.log('📡 Port:', config.port);
console.log('🔒 PIN Protection:', config.pinProtection);
console.log('💾 Data Storage:', dataFile);
console.log('📸 Max Image Size:', config.maxImageSize + 'px');
console.log('💰 Currency:', `${config.currencyCode} (${config.currencySymbol})`);
console.log('\x1b[36m%s\x1b[0m', '--------------------');
});
});