72 lines
2.0 KiB
JavaScript
72 lines
2.0 KiB
JavaScript
/**
|
|
* Settings Routes
|
|
* Handles logo upload and settings
|
|
*/
|
|
const express = require('express');
|
|
const router = express.Router();
|
|
const multer = require('multer');
|
|
const path = require('path');
|
|
const fs = require('fs').promises;
|
|
|
|
// Configure multer for logo upload
|
|
const storage = multer.diskStorage({
|
|
destination: async (req, file, cb) => {
|
|
const uploadDir = path.join(__dirname, '..', '..', 'public', 'uploads');
|
|
try {
|
|
await fs.mkdir(uploadDir, { recursive: true });
|
|
} catch (err) {
|
|
console.error('Error creating upload directory:', err);
|
|
}
|
|
cb(null, uploadDir);
|
|
},
|
|
filename: (req, file, cb) => {
|
|
cb(null, 'company-logo.png');
|
|
}
|
|
});
|
|
|
|
const upload = multer({
|
|
storage: storage,
|
|
limits: { fileSize: 5 * 1024 * 1024 },
|
|
fileFilter: (req, file, cb) => {
|
|
if (file.mimetype.startsWith('image/')) {
|
|
cb(null, true);
|
|
} else {
|
|
cb(new Error('Only image files are allowed'));
|
|
}
|
|
}
|
|
});
|
|
|
|
// GET logo info
|
|
router.get('/logo-info', async (req, res) => {
|
|
try {
|
|
const logoPath = path.join(__dirname, '..', '..', 'public', 'uploads', 'company-logo.png');
|
|
try {
|
|
await fs.access(logoPath);
|
|
res.json({ hasLogo: true, logoPath: '/uploads/company-logo.png' });
|
|
} catch {
|
|
res.json({ hasLogo: false });
|
|
}
|
|
} catch (error) {
|
|
console.error('Error checking logo:', error);
|
|
res.status(500).json({ error: 'Error checking logo' });
|
|
}
|
|
});
|
|
|
|
// POST upload logo
|
|
router.post('/upload-logo', upload.single('logo'), async (req, res) => {
|
|
try {
|
|
if (!req.file) {
|
|
return res.status(400).json({ error: 'No file uploaded' });
|
|
}
|
|
res.json({
|
|
message: 'Logo uploaded successfully',
|
|
path: '/uploads/company-logo.png'
|
|
});
|
|
} catch (error) {
|
|
console.error('Upload error:', error);
|
|
res.status(500).json({ error: 'Error uploading logo' });
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|