This commit is contained in:
2026-08-24 22:28:00 +01:00
parent dfdc3b2840
commit 36e11816f5
141 changed files with 84728 additions and 75 deletions
+104
View File
@@ -17,6 +17,7 @@ const CONVERTER_MAX_UPLOAD_BYTES = Number(process.env.CONVERTER_MAX_UPLOAD_BYTES
const USERS_FILE = path.join(__dirname, 'server', 'users.json');
const DIST_DIR = path.join(__dirname, 'dist', 'form_metacase', 'browser');
const API_ONLY = process.argv.includes('--api-only');
const LIBRETRANSLATE_URL =process.env.LIBRETRANSLATE_URL || 'http://192.168.1.39:5000';
const accessControlHeaders = {
'Access-Control-Allow-Headers': 'Content-Type, Authorization, X-Bank-Key, X-File-Name',
@@ -445,6 +446,106 @@ async function handleExtractConverter(request, response) {
return true;
}
async function handleTranslation(request, response) {
console.log('[TRADUTOR] Pedido de tradução recebido.');
try {
const body = await readJsonBody(request, 200000);
const text = String(body.text || '').trim();
const source = String(body.source || '').trim();
const target = String(body.target || '').trim();
console.log(`[TRADUTOR] ${source} -> ${target}`);
console.log(`[TRADUTOR] Caracteres recebidos: ${text.length}`);
if (!text) {
sendJson(response, 400, {
message: 'O texto para tradução está vazio.'
});
return true;
}
if (!source || !target) {
sendJson(response, 400, {
message: 'Idioma de origem ou destino inválido.'
});
return true;
}
console.log(
`[TRADUTOR] A contactar LibreTranslate: ${LIBRETRANSLATE_URL}`
);
const libreResponse = await fetch(
`${LIBRETRANSLATE_URL}/translate`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
q: text,
source,
target,
format: 'text'
})
}
);
const rawText = await libreResponse.text();
if (!libreResponse.ok) {
console.error(
`[TRADUTOR] LibreTranslate devolveu ${libreResponse.status}:`,
rawText
);
sendJson(response, 502, {
message: 'Erro no serviço de tradução.',
detail: rawText
});
return true;
}
let data;
try {
data = JSON.parse(rawText);
} catch {
throw new Error('O LibreTranslate devolveu uma resposta inválida.');
}
const translation = String(data.translatedText || '').trim();
if (!translation) {
throw new Error('O LibreTranslate não devolveu texto traduzido.');
}
console.log('[TRADUTOR] Tradução concluída com sucesso.');
sendJson(response, 200, {
translation
});
} catch (error) {
const message =
error instanceof Error
? error.message
: 'Erro desconhecido.';
console.error('[TRADUTOR] ERRO:', message);
sendJson(response, 500, {
message: 'Não foi possível efetuar a tradução.',
detail: message
});
}
return true;
}
async function handleApi(request, response) {
if (request.method === 'OPTIONS') {
sendJson(response, 204, {});
@@ -499,6 +600,9 @@ async function handleApi(request, response) {
return true;
}
if (request.url === '/api/tradutor/traduzir' && request.method === 'POST') {
return handleTranslation(request, response);
}
if (request.url === '/api/conversor-extratos/executar' && request.method === 'POST') {
return handleExtractConverter(request, response);