Carpincho/src/core/actualizar.js
2024-12-10 10:03:23 -03:00

160 lines
5.8 KiB
JavaScript

/**
* Archivo de Actualizacion de EMA Libre.
*
* Archivo se ejectuta cuando se lanza EMA libre con el parametro update y
* contiene las funciones para realizar dicha tarea.
*
* LICENSE: This file is part of EMA Libre.
* EMA Libre is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EMA Libre is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EMA Libre. If not, see <http://www.gnu.org/licenses/>.
*
* @copyright Copyright (c) 2019 GUGLER (http://www.gugler.com.ar)
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL License
* @version 3.0
* @link http://www.gugler.com.ar
* @since File available since Release 3.0
*/
/***************************************
*** DECLARACIONES INICIALES ******
**************************************/
var ConfGlobal = require('./../conf/ConfiguracionGlobal.js');
var targz = require('targz');
var fs = require('fs');
var path = require('path');
var rutaNube = '/index.php/s/dpOA4iZs9GIUUKg/download?path=%2F';
if (process.platform === 'win32' || process.platform === 'win64') { rutaNube = rutaNube + 'Windows'; } else { rutaNube = rutaNube + 'GNULinux'; }
module.exports = FUNCIONACTUALIZAR = {
Control: function (actualizar) {
FUNCIONACTUALIZAR.BorrarDescarga('./tmp');
const https = require('https');
const options = {
hostname: 'nube.gugler.com.ar',
port: 443,
path: rutaNube + '&files=Control_UltimaVersion.txt',
method: 'GET'
};
const req = https.request(options, (res) => {
res.on('data', (datos) => {
// Compatibilidad con version anterior
var VersionActual = ConfGlobal.ConfGlobal.Version;
if (VersionActual < datos) { actualizar(true, true, datos); } else { actualizar(false, true, datos); }
});
});
req.on('error', (e) => {
actualizar(false, false, e);
});
req.end();
},
Descargar: function (version, resultado) {
FUNCIONACTUALIZAR.BorrarDescarga('./tmp');
var versionactualizada = 'EMA_Carpincho_v' + version + '.tar.gz';
var nombredescarga = 'EMA_Carpincho.tar.gz';
var file = fs.createWriteStream('./tmp/' + nombredescarga);
const https = require('https');
const options = {
hostname: 'nube.gugler.com.ar',
port: 443,
path: rutaNube + '&files=' + versionactualizada,
method: 'GET'
};
const req = https.request(options, (res) => {
var len = parseInt(res.headers['content-length'], 10);
var body = '';
var cur = 0;
var total = len / 1048576; // 1048576 - bytes in 1Megabyte
res.pipe(file);
file.on('finish', function () {
file.close();
resultado(true, true, true);
});
res.on('data', function (chunk) {
body += chunk;
cur += chunk.length;
var descargadoPorcentaje = (100.0 * cur / len).toFixed(2);
var descargado = (cur / 1048576).toFixed(2);
var texto = 'Bajando ' + descargadoPorcentaje + '% ' + descargado + ' Mb';
resultado(false, texto, descargadoPorcentaje);
});
});
req.on('error', (e) => {
resultado(false, false, false);
});
req.end();
},
Descomprimir: function (resultado) {
var nombredescarga = './tmp/EMA_Carpincho.tar.gz';
targz.decompress({
src: nombredescarga,
dest: './tmp/'
}, function (err) {
if (err) {
resultado(false);
} else {
// Luego de descomprimir se borrar el archivo tar.gz
fs.unlink(nombredescarga, function (err) {
if (err) return resultado(false);
});
resultado(true);
}
});
},
Desplegar: function (resultado) {
var despliegeOrigen = './tmp';
var despliegeDestino = './';
// se borrar los archivos de configuracion y tanques antes de desplegar, para no pisar.
// Manteniendo el archivo de configuracion particular, del usuario.
fs.unlink('./tmp/conf/ConfiguracionGeneral.js', function (err) { if (err) return resultado(false); });
fs.unlink('./tmp/logs/debug.log', function (err) { if (err) return resultado(false); });
fs.unlink('./tmp/logs/registro_diario.log', function (err) { if (err) return resultado(false); });
fs.unlink('./tmp/logs/TanquePendientes.log', function (err) { if (err) return resultado(false); });
// Despliege recursivo
resultado(true);
},
LanzarAplicativoIndependiente: function () {
const { spawn } = require('child_process');
const child = spawn('./tmp/EmaLibre.exe', ['update', '--user-data-dir=./tmp/tmp/cache_temporal'], { detached: true, stdio: 'ignore' });
child.unref();
gui.App.closeAllWindows();
gui.App.quit();
process.exit(1);
win.close();
},
BorrarDescarga: function (despliegeOrigen) {
if (fs.existsSync(despliegeOrigen)) {
fs.readdirSync(despliegeOrigen).forEach(function (entry) {
var rutaArchivo = path.join(despliegeOrigen, entry);
if (fs.lstatSync(rutaArchivo).isDirectory()) {
FUNCIONACTUALIZAR.BorrarDescarga(rutaArchivo);
} else {
fs.unlinkSync(rutaArchivo);
}
});
if (despliegeOrigen !== './tmp') { fs.rmdirSync(despliegeOrigen); }
} else { fs.mkdirSync(despliegeOrigen); } // Crea directorio que no existe
}
};
// termina el Objeto/Clase