Spaces:
Sleeping
Sleeping
Update index.js
Browse files
index.js
CHANGED
|
@@ -2,15 +2,15 @@ import 'dotenv/config';
|
|
| 2 |
import express from 'express';
|
| 3 |
import path from 'path';
|
| 4 |
import fs from 'fs';
|
| 5 |
-
import { fileURLToPath } from 'url';
|
| 6 |
import { execSync } from 'child_process';
|
|
|
|
| 7 |
import RemoteFetcher from './lib/remote.js';
|
| 8 |
|
| 9 |
const __filename = fileURLToPath(import.meta.url);
|
| 10 |
const __dirname = path.dirname(__filename);
|
| 11 |
|
| 12 |
const app = express();
|
| 13 |
-
const PORT = process.env.PORT ||
|
| 14 |
|
| 15 |
const STORAGE_DIR = path.join(__dirname, 'storage');
|
| 16 |
const TEMP_DIR = path.join(__dirname, 'temp');
|
|
@@ -19,16 +19,38 @@ if (!fs.existsSync(STORAGE_DIR)) fs.mkdirSync(STORAGE_DIR, { recursive: true });
|
|
| 19 |
if (!fs.existsSync(TEMP_DIR)) fs.mkdirSync(TEMP_DIR, { recursive: true });
|
| 20 |
|
| 21 |
app.use(express.static('public'));
|
| 22 |
-
app.use('/view', express.static(STORAGE_DIR));
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
| 25 |
app.get('/api/files', (req, res) => {
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
});
|
| 29 |
|
| 30 |
app.listen(PORT, () => {
|
| 31 |
-
console.log(`🚀 Servidor SIMPLE
|
| 32 |
startSimpleWorker();
|
| 33 |
});
|
| 34 |
|
|
@@ -45,41 +67,40 @@ async function startSimpleWorker() {
|
|
| 45 |
await fetcher.connect();
|
| 46 |
console.log('✅ Conexión SSH lista.');
|
| 47 |
|
| 48 |
-
const
|
| 49 |
-
const regions = await fetcher.listDirectories(remoteRoot);
|
| 50 |
|
| 51 |
-
for (const
|
| 52 |
-
|
| 53 |
-
const regionPath = path.posix.join(remoteRoot, region);
|
| 54 |
const zips = await fetcher.listFiles(regionPath, '.zip');
|
| 55 |
|
| 56 |
for (const zipName of zips) {
|
| 57 |
-
const localZipPath = path.join(
|
| 58 |
const remoteZipPath = path.posix.join(regionPath, zipName);
|
| 59 |
|
| 60 |
-
// LIMPIEZA:
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
try { fs.unlinkSync(path.join(STORAGE_DIR, f)); } catch(e) {}
|
| 65 |
}
|
| 66 |
|
| 67 |
console.log(`📦 Descargando ${zipName}...`);
|
| 68 |
await fetcher.fetchFile(remoteZipPath, localZipPath);
|
| 69 |
-
|
| 70 |
-
console.log(`📂 Descomprimiendo ${zipName}...`);
|
| 71 |
-
try {
|
| 72 |
-
execSync(`unzip -o "${localZipPath}" -d "${STORAGE_DIR}"`);
|
| 73 |
-
} catch (e) {
|
| 74 |
-
console.log(`❌ Error unzip: ${e.message}`);
|
| 75 |
-
}
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
}
|
| 81 |
}
|
| 82 |
} catch (err) {
|
| 83 |
-
console.
|
|
|
|
|
|
|
| 84 |
}
|
| 85 |
}
|
|
|
|
| 2 |
import express from 'express';
|
| 3 |
import path from 'path';
|
| 4 |
import fs from 'fs';
|
|
|
|
| 5 |
import { execSync } from 'child_process';
|
| 6 |
+
import { fileURLToPath } from 'url';
|
| 7 |
import RemoteFetcher from './lib/remote.js';
|
| 8 |
|
| 9 |
const __filename = fileURLToPath(import.meta.url);
|
| 10 |
const __dirname = path.dirname(__filename);
|
| 11 |
|
| 12 |
const app = express();
|
| 13 |
+
const PORT = process.env.PORT || 7860;
|
| 14 |
|
| 15 |
const STORAGE_DIR = path.join(__dirname, 'storage');
|
| 16 |
const TEMP_DIR = path.join(__dirname, 'temp');
|
|
|
|
| 19 |
if (!fs.existsSync(TEMP_DIR)) fs.mkdirSync(TEMP_DIR, { recursive: true });
|
| 20 |
|
| 21 |
app.use(express.static('public'));
|
|
|
|
| 22 |
|
| 23 |
+
let currentZipPath = null;
|
| 24 |
+
let currentFileList = [];
|
| 25 |
+
|
| 26 |
+
// Endpoint para listar archivos dentro del ZIP actual
|
| 27 |
app.get('/api/files', (req, res) => {
|
| 28 |
+
res.json(currentFileList);
|
| 29 |
+
});
|
| 30 |
+
|
| 31 |
+
// Ver acta: Extraer solo el archivo solicitado del ZIP
|
| 32 |
+
app.get('/view/:filename', (req, res) => {
|
| 33 |
+
if (!currentZipPath || !fs.existsSync(currentZipPath)) {
|
| 34 |
+
return res.status(404).send('No hay paquete cargado o el archivo se borró.');
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
const fileName = req.params.filename;
|
| 38 |
+
const outputPath = path.join(TEMP_DIR, fileName);
|
| 39 |
+
|
| 40 |
+
try {
|
| 41 |
+
// Extraemos SOLO este archivo del ZIP
|
| 42 |
+
execSync(`unzip -p "${currentZipPath}" "${fileName}" > "${outputPath}"`);
|
| 43 |
+
res.sendFile(outputPath, () => {
|
| 44 |
+
// Borramos el temporal después de enviar
|
| 45 |
+
try { fs.unlinkSync(outputPath); } catch(e) {}
|
| 46 |
+
});
|
| 47 |
+
} catch (e) {
|
| 48 |
+
res.status(500).send('Error al extraer el acta.');
|
| 49 |
+
}
|
| 50 |
});
|
| 51 |
|
| 52 |
app.listen(PORT, () => {
|
| 53 |
+
console.log(`🚀 Servidor SIMPLE (Bajo Demanda) en puerto ${PORT}`);
|
| 54 |
startSimpleWorker();
|
| 55 |
});
|
| 56 |
|
|
|
|
| 67 |
await fetcher.connect();
|
| 68 |
console.log('✅ Conexión SSH lista.');
|
| 69 |
|
| 70 |
+
const regions = ['AFRICA', 'OCEANIA', 'ASIA', 'CALLAO', 'PIURA', 'LIMA', 'LA_LIBERTAD', 'AREQUIPA', 'CUSCO', 'CAJAMARCA'];
|
|
|
|
| 71 |
|
| 72 |
+
for (const regionName of regions) {
|
| 73 |
+
const regionPath = `/home/user110426@vi/ACTAS/${regionName}`;
|
|
|
|
| 74 |
const zips = await fetcher.listFiles(regionPath, '.zip');
|
| 75 |
|
| 76 |
for (const zipName of zips) {
|
| 77 |
+
const localZipPath = path.join(STORAGE_DIR, zipName);
|
| 78 |
const remoteZipPath = path.posix.join(regionPath, zipName);
|
| 79 |
|
| 80 |
+
// LIMPIEZA: Borramos el ZIP anterior para dejar espacio al nuevo
|
| 81 |
+
if (currentZipPath && fs.existsSync(currentZipPath)) {
|
| 82 |
+
console.log(`🧹 Borrando paquete anterior para liberar espacio...`);
|
| 83 |
+
fs.unlinkSync(currentZipPath);
|
|
|
|
| 84 |
}
|
| 85 |
|
| 86 |
console.log(`📦 Descargando ${zipName}...`);
|
| 87 |
await fetcher.fetchFile(remoteZipPath, localZipPath);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
currentZipPath = localZipPath;
|
| 90 |
+
|
| 91 |
+
// Listamos los archivos del ZIP sin extraerlos
|
| 92 |
+
console.log(`📜 Listando actas de ${zipName}...`);
|
| 93 |
+
const listOutput = execSync(`unzip -l "${localZipPath}"`).toString();
|
| 94 |
+
currentFileList = listOutput.split('\n')
|
| 95 |
+
.filter(line => line.includes('.jpg') || line.includes('.pdf') || line.includes('.png'))
|
| 96 |
+
.map(line => line.trim().split(/\s+/).pop());
|
| 97 |
+
|
| 98 |
+
console.log(`✨ ${zipName} listo. ${currentFileList.length} actas detectadas.`);
|
| 99 |
}
|
| 100 |
}
|
| 101 |
} catch (err) {
|
| 102 |
+
console.log(`❌ Error: ${err.message}`);
|
| 103 |
+
} finally {
|
| 104 |
+
fetcher.disconnect();
|
| 105 |
}
|
| 106 |
}
|