From 62459081defb075db5647291f0eab51b5c7211f5 Mon Sep 17 00:00:00 2001 From: PaulaBras <68814660+PaulaBras@users.noreply.github.com> Date: Tue, 8 Nov 2022 18:59:33 +0100 Subject: [PATCH] self merge --- updater/src/index.html | 12 ++- updater/src/index.js | 32 +++++- updater/src/renderer.js | 227 +++++++++++++++++++++++++++++++++++++--- 3 files changed, 252 insertions(+), 19 deletions(-) diff --git a/updater/src/index.html b/updater/src/index.html index 2513c44..5fce786 100644 --- a/updater/src/index.html +++ b/updater/src/index.html @@ -12,10 +12,18 @@

Output Test here

Input URL

- +

Input Directory

- + + + + + + + + + diff --git a/updater/src/index.js b/updater/src/index.js index 0ad3423..059584b 100644 --- a/updater/src/index.js +++ b/updater/src/index.js @@ -5,6 +5,11 @@ const path = require('path'); var fs = require('fs'); const yaml = require('js-yaml'); var DecompressZip = require('decompress-zip'); +const { Http2ServerRequest } = require('http2'); +const superagent = require('superagent').agent(); +const http = require('node:http'); +const { options } = require('superagent'); + // Handle creating/removing shortcuts on Windows when installing/uninstalling. // eslint-disable-next-line global-require @@ -65,8 +70,8 @@ const createWindow = () => { ipcMain.on('select-dirs', async(event, arg) => { filepath = await dialog.showOpenDialog(mainWindow, { properties: ['openDirectory'] - }) - console.log('directories selected', filepath.filePaths) + }); + console.log('directories selected', filepath.filePaths); mainWindow.webContents.send("filepath", filepath.filePaths); // Save directory to file }); @@ -75,6 +80,29 @@ const createWindow = () => { ipcMain.on("download", (event, info) => { // https://dms.pabr.de/s/SpBiQYADTNak7R5/download info.properties.onProgress = status => mainWindow.webContents.send("download progress", status); + + ipcMain.on("download", (event, info) => { + // https://dms.pabr.de/s/SpBiQYADTNak7R5/download + info.properties.onProgress = status => mainWindow.webContents.send("download progress", status); + // http.request + let file = superagent.get('https://files.aero-nav.com/EDGG/Full_Package_20221104183433-221101-3.zip') + .set('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:104.0) Gecko/20100101 Firefox/104.0') + .set('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8') + .set('Accept-Language', 'en-US,en;q=0.5') + .set('Accept-Encoding', 'gzip, deflate, br') + .set('DNT', '1') + .set('Connection', 'keep-alive') + .set('Referer', 'http://files.aero-nav.com/') + .set('Upgrade-Insecure-Requests', '1') + .set('Sec-Fetch-Dest', 'document') + .set('Sec-Fetch-Mode', 'navigate') + .set('Sec-Fetch-Site', 'cross-site') + .set('Sec-Fetch-User', '?1'); + // Working Download + download(BrowserWindow.getFocusedWindow(), info.url, info.properties) + .then(dl => mainWindow.webContents.send("download complete", dl.getSavePath())); + }); + download(BrowserWindow.getFocusedWindow(), info.url, info.properties) .then(dl => mainWindow.webContents.send("download complete", dl.getSavePath())); }); diff --git a/updater/src/renderer.js b/updater/src/renderer.js index 9e679eb..e046e2a 100644 --- a/updater/src/renderer.js +++ b/updater/src/renderer.js @@ -1,5 +1,10 @@ const { ipcRenderer, dialog } = require('electron'); -var DecompressZip = require('decompress-zip'); +const superagent = require('superagent').agent(); +var fs = require('fs'); + +// Global Variabels +let hrefLinks = ""; + ipcRenderer.on("download complete", (event, file) => { console.log(file); // Full file path @@ -11,33 +16,225 @@ ipcRenderer.on("download progress", (event, progress) => { document.getElementById('progressbar').value = cleanProgressInPercentages; }); +let fileSelect = document.getElementById('files'); let donwloadBtn = document.getElementById('download'); donwloadBtn.addEventListener('click', (e) => { let directoryPath = document.getElementById('dirBox'); let urlPath = document.getElementById('urlBox'); ipcRenderer.send("download", { - url: urlPath.value, - properties: { directory: directoryPath.value } + url: hrefLinks[fileSelect.options.selectedIndex], + properties: { + directory: directoryPath.value + } }); }); let directoryBtn = document.getElementById('dirs'); directoryBtn.addEventListener('click', (e) => { - window.postMessage({ - type: 'select-dirs' - }) + ipcRenderer.send('select-dirs'); }); ipcRenderer.on("filepath", (event, file) => { - document.getElementById('dirBox').innerText = file; + document.getElementById('dirBox').value = file; }); -let extractBtn = document.getElementById('extract'); -extractBtn.addEventListener('click', () => { - let directoryPath = document.getElementById('dirBox'); - let urlPath = document.getElementById('urlBox'); - ipcRenderer.send("extract", { - url: urlPath.value, - properties: { directory: directoryPath.value } +// Check update +let dropDownGNG = document.getElementById('gng'); +let dropDownFiles = document.getElementById('files'); + +let updateBtn = document.getElementById('update'); +updateBtn.addEventListener('click', (e) => { + removeFileItems(); + getUpdates(); +}); + +// Remove all files when changing Region --> WIP no nicht +const removeFileItems = async() => { + var i, L = dropDownFiles.options.length - 1; + for (i = L; i >= 0; i--) { + dropDownFiles.remove(i); + } +} + +const getUpdates = async() => { + + + // Get all GNG Options + let courses = await superagent.get('https://files.aero-nav.com/'); + let text = courses.text.split("Download Pages").pop(); + let textArray = text.split("\n"); + let liste = ""; + let firstElement = ""; + let lastElement = ""; + textArray.forEach(element => { + if (element.includes(firstElement)) { + liste += element.substring( + element.indexOf(firstElement) + firstElement.length, + element.indexOf(lastElement, element.indexOf(firstElement))) + + "\n"; + } }); -}); \ No newline at end of file + const listeArray = liste.split("\n"); + + // Add to html selector + listeArray.pop(); + listeArray.forEach(optionsAdd); + + // Add Elements to Drop Down + function optionsAdd(item) { + var option = document.createElement("option"); + option.text = item; + dropDownGNG.add(option); + } +} + +// Check Files +let getFilesBtn = document.getElementById('getFiles'); +getFilesBtn.addEventListener('click', (e) => { + hrefLinks = getFiles(); + console.log("leaveme alone " + hrefLinks); +}); + +const getFiles = async() => { + removeFileItems(); + // Get all GNG Package Options + let region = "https://files.aero-nav.com/" + dropDownGNG.options[dropDownGNG.selectedIndex].text; + let courses = await superagent.get(region); + let text = courses.text.split("ReleasedDownload").pop(); + text = text.split("

AIRAC News

")[0] + //console.log(text); + let rows = ""; + + // As an idea + + // textArray = text.split("\n"); + // let liste = ""; + // let firstElement = ""; + // let lastElement = ""; + // textArray.forEach(element => { + // if (element.includes(firstElement)) { + // liste += element.substring( + // element.indexOf(firstElement) + firstElement.length, + // element.indexOf(lastElement, element.indexOf(firstElement))) + + // "\n"; + // } + // }); + // let outArray2 = liste.split("\n"); + // outArray2.pop(); + // console.log(outArray2); + + for (var i = 0; i < text.length; i++) { + if (text[i] + text[i + 1] + text[i + 2] + text[i + 3] === "") { + let i2 = i + 4; + while (text[i2] + text[i2 + 1] + text[i2 + 2] + text[i2 + 3] + text[i2 + 4] !== "") { + rows += text[i2]; + i2++; + } + rows += "\n"; + } + } + //console.log(rows); // For debugging only + + // All Rows in Table + const listeArray = rows.split("\n"); + listeArray.pop(); + let fileNames = ""; + // Select only Package names + for (var i = 1; i < listeArray.length; i = i + 5) { + fileNames += listeArray[i] + "\n"; + } + const fileNamesArray = fileNames.split("\n"); + fileNamesArray.pop(); + fileNamesArray.forEach(optionsAdd); + + // Add Elements to Drop Down + function optionsAdd(item) { + var option = document.createElement("option"); + option.text = item; + dropDownFiles.add(option); + } + + let firstElement = "href="; + let lastElement = "class="; + let hrefLinksList = ""; + for (var i = 4; i < listeArray.length; i = i + 5) { + hrefLinksList += listeArray[i].substring( + listeArray[i].indexOf(firstElement) + firstElement.length + 1, + listeArray[i].indexOf(lastElement) - 2) + "\n"; + } + const hrefLinksArray = hrefLinksList.split("\n"); + hrefLinksArray.pop(); + + console.log(hrefLinksArray); + return hrefLinksArray; +} + + + + + + +// Download idea from https://damieng.com/blog/2017/03/10/downloading-files-with-progress-in-electron/ + +download("https://files.aero-nav.com/EDGG/Full_Package_20221104183433-221101-3.zip", "Full_Package_20221104183433-221101-3.zip", (bytes, percent) => console.log(`Downloaded ${bytes} (${percent})`)); + + +//import fs from "fs"; + +async function download( + sourceUrl, + targetFile, + progressCallback, + length +) { + const request = new Request(sourceUrl, { + headers: new Headers({ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:104.0) Gecko/20100101 Firefox/104.0", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8", "Accept-Language": "en-US,en;q=0.5", "Accept-Encoding": "gzip, deflate, br", "DNT": "1", "Connection": "keep-alive", "Referer": "http://files.aero-nav.com/", "Upgrade-Insecure-Requests": "1", "Sec-Fetch-Dest": "document", "Sec-Fetch-Mode": "navigate", "Sec-Fetch-Site": "cross-site", "Sec-Fetch-User": "?1" }), + }); + + const response = await fetch(request); + if (!response.ok) { + throw Error( + `Unable to download, server returned ${response.status} ${response.statusText}` + ); + } + + const body = response.body; + if (body == null) { + throw Error("No response body"); + } + + const finalLength = + length || parseInt(response.headers.get("Content-Length" || "0"), 10); + const reader = body.getReader(); + const writer = fs.createWriteStream(targetFile); + + await streamWithProgress(finalLength, reader, writer, progressCallback); + writer.end(); +} + +async function streamWithProgress(length, reader, writer, progressCallback) { + let bytesDone = 0; + + while (true) { + const result = await reader.read(); + if (result.done) { + if (progressCallback != null) { + progressCallback(length, 100); + } + return; + } + + const chunk = result.value; + if (chunk == null) { + throw Error("Empty chunk received during download"); + } else { + writer.write(Buffer.from(chunk)); + if (progressCallback != null) { + bytesDone += chunk.byteLength; + const percent = + length === 0 ? null : Math.floor((bytesDone / length) * 100); + progressCallback(bytesDone, percent); + } + } + } +} \ No newline at end of file