Progressbar & Download fix

This commit is contained in:
PaulaBras
2022-10-03 13:44:41 +02:00
parent a409778fa6
commit 30e7c48862
8 changed files with 343 additions and 376 deletions

View File

@ -3,32 +3,19 @@
<head>
<meta charset="UTF-8" />
<title>Sectorfile Updater</title>
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
<link rel="stylesheet" href="index.css" />
<script src="./renderer.js"></script>
</head>
<body>
<!--<button id="writeFile" class"button is-primary">Write File</button>
<button id="startDownload" class"button is-primary">Start Download</button>
<button id="stopDownload" class"button is-warning">Stop Download</button>
<button id="startUnzip" class"button is-primary">Start Unzip</button>
<button id="stopUnzip" class"button is-warning">Stop Unzip</button>
<button id="selectVersionBtn" class"button is-warning">Version</button>
<button id="selectPathBtn" class"button is-warning">Path</button> -->
<button id="download" class"button is-primary">Download</button>
<button id="openButton">Open</button>
<!--<h1>Hello World!</h1>
<p>Welcome to your Electron application.</p>
<p>Input Text</p>
<input type="text" id="txtBox" name="name" size="80">
<button id="dirs">Open</button>
<p>Input URL</p>
<input type="text" id="urlBox" name="name" size="80">
<p>Input Directory</p>
<input type="text" id="dirBox" name="name" size="80">
<progress id="file" max="100" value="70"></progress>-->
<progress id="progressbar" max="100" value="70"></progress>
<input type="file" id="dirs" webkitdirectory directory/>
<button type="button" id="btn">Open a File</button>
File path: <strong id="filePath"></strong>
<p id="ouput">Output Test here</p>
<script src="./renderer.js"></script>
</body>
</html>

View File

@ -1,9 +1,8 @@
const { app, BrowserWindow, Menu, dialog, ipcMain, nativeTheme } = require('electron');
const { app, BrowserWindow, dialog, ipcMain } = require('electron');
const { download } = require("electron-dl");
const path = require('path');
//Menu.setApplicationMenu(false);
//Menu.setApplicationMenu(false); // Top Bar removal
var fs = require('fs');
let window;
// Handle creating/removing shortcuts on Windows when installing/uninstalling.
// eslint-disable-next-line global-require
@ -11,34 +10,46 @@ if (require('electron-squirrel-startup')) {
app.quit();
}
async function handleFileOpen() {
const { canceled, filePaths } = await dialog.showOpenDialog()
if (canceled) {
return
} else {
return filePaths[0]
}
}
app.whenReady(() => {
ipcMain.handle('dialog:openFile', handleFileOpen)
createWindow()
})
const createWindow = () => {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
width: 1920,
height: 1080,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
nodeIntegration: true,
contextIsolation: false,
nodeIntegrationInWorker: true,
preload: path.join(__dirname, 'preload.js'),
},
});
mainWindow.loadFile(path.join(__dirname, 'index.html'));
mainWindow.webContents.openDevTools();
// Select Directory
ipcMain.on('select-dirs', async(event, arg) => {
const result = await dialog.showOpenDialog(mainWindow, {
properties: ['openDirectory']
})
console.log('directories selected', result.filePaths)
})
// Download a file
ipcMain.on("download", (event, info) => {
// https://dms.pabr.de/s/SpBiQYADTNak7R5/download
info.properties.onProgress = status => mainWindow.webContents.send("download progress", status);
download(BrowserWindow.getFocusedWindow(), info.url, info.properties)
.then(dl => mainWindow.webContents.send("download complete", dl.getSavePath()));
});
};
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
@ -61,7 +72,6 @@ app.on('activate', () => {
}
});
// Write to a file
ipcMain.on("saveFile", (event, location, txtVal) => {
fs.writeFile(location, txtVal.toString(), (err) => {
@ -69,24 +79,4 @@ ipcMain.on("saveFile", (event, location, txtVal) => {
console.log(err);
}
});
});
ipcMain.on('select-dirs', async(event, arg) => {
const result = await dialog.showOpenDialog(mainWindow, {
properties: ['openDirectory']
})
console.log('directories selected', result.filePaths)
})
// Download a file
ipcMain.on("download", async(event, url) => {
//properties.onProgress = status => window.webContents.send("download progress", status);
download(BrowserWindow.getFocusedWindow(), url)
.then(dl => window.webContents.send("download complete", dl.getSavePath()));
});
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and import them here.
});

View File

@ -1,46 +1 @@
const { contextBridge, ipcRenderer } = require('electron')
contextBridge.exposeInMainWorld('electronAPI', {
openFile: () => ipcRenderer.invoke('dialog:openFile')
});
// Change me for file save
document.addEventListener('DOMContentLoaded', function() {
let writeFile = document.getElementById("writeFile");
writeFile.addEventListener("click", () => {
let txtBox = document.getElementById("txtBox");
let txtval = txtBox.value;
ipcRenderer.send("saveFile", "f:\\file1.txt", txtval);
});
});
// Change me for file save //ex: https://dms.pabr.de/s/RPbgF4nmgeyigkn
document.addEventListener('DOMContentLoaded', function() {
let startDownload = document.getElementById("startDownload");
startDownload.addEventListener("click", () => {
let urlBox = document.getElementById("txtBox");
let urlval = urlBox.value;
let dirBox = document.getElementById("txtBox");
let dirval = dirBox.value;
ipcRenderer.send("download", {
url: urlval,
properties: { directory: dirval }
});
});
});
/*
// Change me for select directory
process.once('loaded', () => {
window.addEventListener('message', evt => {
if (evt.data.type === 'select-dirs') {
ipcRenderer.send('select-dirs')
}
});
});*/
const { contextBridge, ipcRenderer } = require('electron')

View File

@ -1,42 +1,38 @@
// Buttons
const startDownload = document.querySelector('startDownload');
const stopDownload = document.querySelector('stopDownload');
const startUnzip = document.querySelector('startUnzip');
const stopUnzip = document.querySelector('stopUnzip');
const selectVersionBtn = document.querySelector('selectVersion');
const selectPathBtn = document.querySelector('selectPathBtn');
const writeFile = document.querySelector('writeFile');
const { remote, ipcRenderer } = require('electron');
const { writeFile } = require('fs');
document.getElementById('btn').addEventListener('click', () => {
const filePath = window.electronAPI.openFile()
document.getElementById('filePath').innerText = filePath
});
const { ipcRenderer, dialog } = require('electron');
ipcRenderer.on("download complete", (event, file) => {
console.log(file); // Full file path
// Datei entpacken
});
ipcRenderer.on("download progress", (event, progress) => {
console.log(progress); // Progress in fraction, between 0 and 1
const progressInPercentages = progress * 100; // With decimal point and a bunch of numbers
const cleanProgressInPercentages = Math.floor(progress * 100); // Without decimal point
const cleanProgressInPercentages = Math.floor(progress.percent * 100); // Without decimal point
document.getElementById('progressbar').value = cleanProgressInPercentages;
});
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 }
});
});
// Select Folder
document.getElementById('dirs').addEventListener('click', (evt) => {
evt.preventDefault()
window.postMessage({
type: 'select-dirs',
})
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('dirs').addEventListener('click', function() {
openFile();
});
});
function openFile() {
ipcRenderer.send('openFolder', () => {
console.log("Event sent.");
});
}
document.getElementById('openButton').addEventListener('click', () => {
console.log("Test");
ipcRenderer.send('openFile', {})
});
ipcRenderer.on('folderData', (event, data) => {
console.log(data)
})