This commit is contained in:
Ccjcarkbla
2026-09-13 10:00:13 -04:00
parent 9b04f26c82
commit 349f469602
31 changed files with 1455 additions and 37 deletions
+15 -11
View File
@@ -2,9 +2,7 @@
const express = require('express');
const multer = require('multer');
const path = require('path');
const os = require('os');
const fs = require('fs');
const { models } = require('../../core/db');
const { authenticateToken, authenticateRole } = require('../../core/auth');
const { ok, noContent } = require('../../core/respond');
@@ -80,18 +78,24 @@ router.post('/logo', authenticateToken, authenticateRole(OWNERS), upload.single(
} catch (err) { next(err); }
});
router.get('/logo', authenticateToken, async (req, res, next) => {
// Public: logos are rendered by <img src> (login page, banner), which cannot send
// an Authorization header. Org id comes from the path; legacy /logo falls back to
// the first org, matching GET /branding/public.
router.get('/logo/:orgId?', async (req, res, next) => {
try {
const branding = await svc.getBranding(models, req.user.organizationId);
if (!branding.logoPath) return next(new AppError('NOT_FOUND', 'No custom logo set.', 404));
let orgId = parseInt(req.params.orgId, 10);
if (isNaN(orgId)) {
const org = await models.Organization.findOne({ order: [['id', 'ASC']] });
if (!org) return next(new AppError('NOT_FOUND', 'No custom logo set.', 404));
orgId = org.id;
}
// Find logo file in uploads dir
const uploadsDir = process.env.UPLOADS_DIR || path.join(__dirname, '..', '..', 'uploads');
const files = fs.readdirSync(uploadsDir).filter(f => f.startsWith(`logo_${req.user.organizationId}_`));
if (!files.length) return next(new AppError('NOT_FOUND', 'Logo file not found.', 404));
const filePath = svc.findLogoFile(orgId);
if (!filePath) return next(new AppError('NOT_FOUND', 'No custom logo set.', 404));
const logoFile = files.sort().pop(); // latest
res.sendFile(path.join(uploadsDir, logoFile));
// Path is versioned (?v=), so the file at a given URL never changes
res.set('Cache-Control', 'public, max-age=31536000, immutable');
res.sendFile(filePath);
} catch (err) { next(err); }
});