-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
79 lines (68 loc) · 1.92 KB
/
server.js
File metadata and controls
79 lines (68 loc) · 1.92 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
import express from 'express';
import multer from 'multer';
import cors from 'cors';
import path from 'path';
import fs from 'fs';
import { fileURLToPath } from 'url';
import generateIconManifest from './scripts/generate-icon-manifest.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const PORT = 3001;
// Enable CORS
app.use(cors());
app.use(express.json());
// Configure storage
const storage = multer.diskStorage({
destination: function (req, file, cb) {
const uploadDir = path.join(__dirname, 'public', 'icons');
if (!fs.existsSync(uploadDir)) {
fs.mkdirSync(uploadDir, { recursive: true });
}
cb(null, uploadDir);
},
filename: function (req, file, cb) {
// Sanitize filename
const name = file.originalname.replace(/[^a-zA-Z0-9.-]/g, '_');
cb(null, name);
}
});
const upload = multer({
storage: storage,
fileFilter: (req, file, cb) => {
if (file.mimetype.startsWith('image/')) {
cb(null, true);
} else {
cb(new Error('Only images are allowed'));
}
},
limits: {
fileSize: 5 * 1024 * 1024 // 5MB limit
}
});
// Upload endpoint
app.post('/api/upload', upload.array('icons', 10), (req, res) => {
try {
if (!req.files || req.files.length === 0) {
return res.status(400).json({ error: 'No files uploaded' });
}
console.log(`✅ Uploaded ${req.files.length} files`);
// Regenerate manifest
const manifest = generateIconManifest();
res.json({
success: true,
message: 'Files uploaded successfully',
files: req.files.map(f => ({
filename: f.filename,
path: `/icons/${f.filename}`
})),
manifest
});
} catch (error) {
console.error('Upload error:', error);
res.status(500).json({ error: 'Upload failed' });
}
});
app.listen(PORT, () => {
console.log(`🚀 Server running on http://localhost:${PORT}`);
});