feat(pre-matricula): compress uploaded preview images using sharp to JPEG for WhatsApp
Build and Deploy (Gitea) / build-and-deploy (push) Successful in 2m7s
Details
Build and Deploy (Gitea) / build-and-deploy (push) Successful in 2m7s
Details
This commit is contained in:
parent
850ec464c9
commit
dd0e587551
|
|
@ -4256,8 +4256,31 @@ async function startServer() {
|
|||
if (!req.file) {
|
||||
return res.status(400).json({ error: 'Nenhum arquivo enviado.' });
|
||||
}
|
||||
const fileName = `pre_${Date.now()}_${Math.random().toString(36).substring(7)}_${req.file.originalname.replace(/[^a-zA-Z0-9.-]/g, '_')}`;
|
||||
const url = await uploadFile('prematriculas', fileName, req.file.buffer, req.file.mimetype);
|
||||
|
||||
let fileBuffer = req.file.buffer;
|
||||
let contentType = req.file.mimetype;
|
||||
let fileName = req.file.originalname.replace(/[^a-zA-Z0-9.-]/g, '_');
|
||||
|
||||
// Se for uma imagem, compacta usando sharp e converte para JPEG para garantir compatibilidade com WhatsApp
|
||||
if (contentType.startsWith('image/')) {
|
||||
try {
|
||||
fileBuffer = await sharp(req.file.buffer)
|
||||
.resize(600, 600, { fit: 'inside', withoutEnlargement: true })
|
||||
.jpeg({ quality: 75, progressive: true })
|
||||
.toBuffer();
|
||||
contentType = 'image/jpeg';
|
||||
|
||||
// Garante que a extensão seja .jpg no final do nome
|
||||
const lastDot = fileName.lastIndexOf('.');
|
||||
const baseName = lastDot !== -1 ? fileName.substring(0, lastDot) : fileName;
|
||||
fileName = `${baseName}.jpg`;
|
||||
} catch (sharpError) {
|
||||
console.warn('[PreMatricula:Upload] Falha ao processar com sharp, enviando original:', sharpError.message);
|
||||
}
|
||||
}
|
||||
|
||||
const finalFileName = `pre_${Date.now()}_${Math.random().toString(36).substring(7)}_${fileName}`;
|
||||
const url = await uploadFile('prematriculas', finalFileName, fileBuffer, contentType);
|
||||
res.json({ url });
|
||||
} catch (e) {
|
||||
console.error('[PreMatricula:Upload] Erro de upload:', e);
|
||||
|
|
|
|||
Loading…
Reference in New Issue