added select export directory and save to disk logic

This commit is contained in:
Abhinav-grd 2021-03-29 16:58:29 +05:30
parent 9e910d7225
commit 6319f4fec0
3 changed files with 62 additions and 20 deletions

View file

@ -1,5 +1,7 @@
import { app, BrowserWindow } from 'electron'; import { app, BrowserWindow } from 'electron';
import * as path from 'path'; import * as path from 'path';
import * as electron from 'electron';
const { dialog, ipcMain } = electron;
function createWindow() { function createWindow() {
// Create the browser window. // Create the browser window.
@ -12,7 +14,7 @@ function createWindow() {
}); });
// and load the index.html of the app. // and load the index.html of the app.
mainWindow.loadURL('https://photos.ente.io'); mainWindow.loadURL('http://localhost:3000');
// Open the DevTools. // Open the DevTools.
mainWindow.webContents.openDevTools(); mainWindow.webContents.openDevTools();
@ -40,5 +42,24 @@ app.on('window-all-closed', () => {
} }
}); });
ipcMain.on('select-dir', async (event) => {
let dialogWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: false,
enableRemoteModule: false,
contextIsolation: true,
sandbox: true,
},
});
const result = await dialog.showOpenDialog(dialogWindow, {
properties: ['openDirectory'],
});
const dir =
result.filePaths && result.filePaths.length > 0 && result.filePaths[0];
dialogWindow.close();
event.returnValue = dir;
});
// In this file you can include the rest of your app"s specific main process // 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 require them here. // code. You can also put them in separate files and require them here.

View file

@ -1,14 +1,41 @@
// All of the Node.js APIs are available in the preload process. import { Readable } from 'stream';
// It has the same sandbox as a Chrome extension. import * as fs from 'fs';
window.addEventListener("DOMContentLoaded", () => { import * as electron from 'electron';
const replaceText = (selector: string, text: string) => {
const element = document.getElementById(selector);
if (element) {
element.innerText = text;
}
};
for (const type of ["chrome", "node", "electron"]) { const { ipcRenderer } = electron;
replaceText(`${type}-version`, process.versions[type as keyof NodeJS.ProcessVersions]);
} const responseToReadable = (fileStream: any) => {
}); const reader = fileStream.getReader();
const rs = new Readable();
rs._read = async () => {
const result = await reader.read();
console.log(result);
if (!result.done) {
rs.push(Buffer.from(result.value));
} else {
rs.push(null);
return;
}
};
return rs;
};
const saveToDisk = (path: string, fileStream: ReadableStream<any>) => {
const writeable = fs.createWriteStream(path);
const readable = responseToReadable(fileStream);
readable.pipe(writeable);
};
const selectDirectory = async () => {
try {
return await ipcRenderer.sendSync('select-dir');
} catch (e) {
console.error(e);
}
};
var windowObject: any = window;
windowObject['ElectronAPIs'] = {
saveToDisk,
selectDirectory,
};

View file

@ -1,6 +0,0 @@
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// No Node.js APIs are available in this process unless
// nodeIntegration is set to true in webPreferences.
// Use preload.js to selectively enable features
// needed in the renderer process.