bot-functions 1.7.1 → 1.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +74 -1
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1485,6 +1485,78 @@ const uploadEvidence = async (pathDownloads, params, reintentos = 3) => {
|
|
|
1485
1485
|
throw new Error(`[S3] Upload fallido tras ${reintentos} intentos: ${ultimoError.message}`);
|
|
1486
1486
|
};
|
|
1487
1487
|
|
|
1488
|
+
const insertBotEvidence = async (rutaFile, params) => {
|
|
1489
|
+
return new Promise(async function (resolve) {
|
|
1490
|
+
|
|
1491
|
+
await axios.post(process.env.INSERT_BOT_EVIDENCE, {
|
|
1492
|
+
idgfc: params.robot.idgfc,
|
|
1493
|
+
idpc: params.robot.idPC,
|
|
1494
|
+
proyecto: params.robot.proyecto.toUpperCase(),
|
|
1495
|
+
grupo: params.robot.cadena.toUpperCase(),
|
|
1496
|
+
cadena: params.robot.cadena.toUpperCase(),
|
|
1497
|
+
fecha: params.reporte.date1,
|
|
1498
|
+
tipo: params.reporte.type,
|
|
1499
|
+
fileInsert: rutaFile
|
|
1500
|
+
});
|
|
1501
|
+
resolve(true);
|
|
1502
|
+
});
|
|
1503
|
+
}
|
|
1504
|
+
|
|
1505
|
+
const uploadBotEvidence = async (pathDownloads, params, reintentos = 3) => {
|
|
1506
|
+
|
|
1507
|
+
log.info(`[S3] Región: ${process.env.AWS_REGION_BUCKET} | Bucket: ${process.env.S3_BUCKET}`);
|
|
1508
|
+
const s3 = new S3Client({ region: process.env.AWS_REGION_BUCKET });
|
|
1509
|
+
|
|
1510
|
+
const zipPath = `${pathDownloads}/${params.reporte.filename}.zip`;
|
|
1511
|
+
const key = `${params.reporte.pathS3}/${params.reporte.filename}.zip`;
|
|
1512
|
+
|
|
1513
|
+
// Armar el zip
|
|
1514
|
+
const zip = new AdmZip();
|
|
1515
|
+
zip.addLocalFile(`${pathDownloads}/${params.reporte.filename}${process.env.EXTENSION_FILE}`);
|
|
1516
|
+
zip.writeZip(zipPath);
|
|
1517
|
+
|
|
1518
|
+
// Validar antes de intentar subir
|
|
1519
|
+
validarZip(zipPath);
|
|
1520
|
+
|
|
1521
|
+
let ultimoError;
|
|
1522
|
+
|
|
1523
|
+
for (let intento = 1; intento <= reintentos; intento++) {
|
|
1524
|
+
try {
|
|
1525
|
+
log.info(`[S3] Intento ${intento}/${reintentos} — subiendo ${key}`);
|
|
1526
|
+
|
|
1527
|
+
const upload = new Upload({
|
|
1528
|
+
client: s3,
|
|
1529
|
+
params: {
|
|
1530
|
+
Bucket: process.env.S3_BUCKET,
|
|
1531
|
+
Key: key,
|
|
1532
|
+
Body: fs.createReadStream(zipPath),
|
|
1533
|
+
ContentType: "application/zip",
|
|
1534
|
+
ACL: "public-read",
|
|
1535
|
+
},
|
|
1536
|
+
queueSize: 4,
|
|
1537
|
+
partSize: 10 * 1024 * 1024,
|
|
1538
|
+
});
|
|
1539
|
+
|
|
1540
|
+
await upload.done();
|
|
1541
|
+
const url = `https://${process.env.S3_BUCKET}.s3.${process.env.AWS_REGION_BUCKET}.amazonaws.com/${key}`;
|
|
1542
|
+
log.info(`[S3] Upload exitoso en intento ${intento} — ${key}`);
|
|
1543
|
+
await insertBotEvidence(url, params);
|
|
1544
|
+
await insertLog2(params, 305);
|
|
1545
|
+
return key;
|
|
1546
|
+
|
|
1547
|
+
} catch (error) {
|
|
1548
|
+
ultimoError = error;
|
|
1549
|
+
log.error(`[S3] Intento ${intento} fallido: ${error.message}`);
|
|
1550
|
+
|
|
1551
|
+
if (intento < reintentos) {
|
|
1552
|
+
const espera = 2000 * intento;
|
|
1553
|
+
log.info(`[S3] Reintentando en ${espera / 1000}s...`);
|
|
1554
|
+
await sleepFile(espera);
|
|
1555
|
+
}
|
|
1556
|
+
}
|
|
1557
|
+
}
|
|
1558
|
+
throw new Error(`[S3] Upload fallido tras ${reintentos} intentos: ${ultimoError.message}`);
|
|
1559
|
+
};
|
|
1488
1560
|
|
|
1489
1561
|
module.exports = {
|
|
1490
1562
|
waitNtype,
|
|
@@ -1542,5 +1614,6 @@ module.exports = {
|
|
|
1542
1614
|
uploadImageToS3,
|
|
1543
1615
|
insertCatspecs,
|
|
1544
1616
|
uploadEvidence,
|
|
1545
|
-
sendDataClickHouse
|
|
1617
|
+
sendDataClickHouse,
|
|
1618
|
+
uploadBotEvidence
|
|
1546
1619
|
}
|