UNPKG

twokeys-server

Version:

Server for 2Keys

150 lines (146 loc) 6.05 kB
"use strict"; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /** Copyright 2018 Kishan Sambhi This file is part of 2Keys. 2Keys 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. 2Keys 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 2Keys. If not, see <https://www.gnu.org/licenses/>. */ /** * @overview ZIP file downloader, extracter and copier */ const fs_1 = __importStar(require("fs")); const https_1 = __importDefault(require("https")); const mkdirp_1 = __importDefault(require("mkdirp")); const progress_1 = __importDefault(require("progress")); const logger_1 = __importDefault(require("../../util/logger")); const path_1 = require("path"); const adm_zip_1 = __importDefault(require("adm-zip")); const { open, access } = fs_1.promises; class ZipDownloader { /** * Constructor * @param name Name of software downloading, as referenced in userspace_config.software * @param url URL to download the zip from (ONLY accepts HTTPS) * @param saveTo Dir to save file to * @param saveAs Filename to save as (including the .zip prefix) */ constructor(name, url, saveTo, saveAs, argv) { this.name = name; this.logger = new logger_1.default({ name, }); this.url = url; this.saveTo = saveTo; this.saveAs = saveAs; this.argv = typeof argv === "object" ? argv : {}; this.fullPath = path_1.join(this.saveTo, this.saveAs); // Save full path } /** * Download the file */ fetch_file() { return new Promise(async (resolve, reject) => { this.logger.info(`Downloading package from url ${this.url} to ${this.saveTo} as ${this.saveAs}.zip...`); // Make dirs try { await mkdirp_1.default(this.saveTo); } catch (err) { this.logger.err("Error making download dirs!"); reject(err); return; } this.logger.debug("Created dirs."); // See if exists // Only needed if not forcing if (!this.argv.force) { try { await open(this.fullPath, "wx"); } catch (err) { if (err.code === "EEXIST") { this.logger.err(`${this.name} already downloaded. Please delete the downloaded file if you need to redownload it.`); reject(new Error(`${this.name} already downloaded. Please delete the downloaded file if you need to redownload it.`)); } else { this.logger.err(`Error opening file to save to!`); reject(err); } return; } } // Create request const req = https_1.default.request({ host: this.url.split("/")[2], port: 443, path: this.url.slice(`https://${this.url.split("/")[2]}`.length) }); req.on("response", res => { if (typeof res.headers !== "undefined" && typeof res.headers["content-length"] !== "undefined") { const len = parseInt(res.headers["content-length"], 10); let downloaded = ""; if (!this.logger.isSilent) { const progressBar = new progress_1.default(':bar :percent ETA: :etas', { complete: '▓', incomplete: '░', width: 50, total: isNaN(len) ? 6403580 : len }); res.on("data", (chunk) => { progressBar.tick(chunk.length); }); } } const fileStream = fs_1.createWriteStream(this.fullPath); res.pipe(fileStream); // Pipe to writer res.on("end", () => { // console.log(""); this.logger.info("Download complete."); fileStream.close(); resolve(); }); }); req.end(); }); } /** * Extract the zip folder */ extract() { return new Promise((resolve, reject) => { this.logger.info("Extracting..."); // Validate existence access(this.fullPath, fs_1.default.constants.F_OK) .then(() => { this.logger.debug("Zip file found. Extracting..."); // DO IT // From https://github.com/cthackers/adm-zip const zipFile = new adm_zip_1.default(this.fullPath); zipFile.extractAllTo(this.saveTo, true); resolve(); }) .catch(reject); }); } } exports.default = ZipDownloader; //# sourceMappingURL=zip-downloader.js.map