added select export directory and save to disk logic
This commit is contained in:
parent
9e910d7225
commit
6319f4fec0
23
src/main.ts
23
src/main.ts
|
@ -1,5 +1,7 @@
|
|||
import { app, BrowserWindow } from 'electron';
|
||||
import * as path from 'path';
|
||||
import * as electron from 'electron';
|
||||
const { dialog, ipcMain } = electron;
|
||||
|
||||
function createWindow() {
|
||||
// Create the browser window.
|
||||
|
@ -12,7 +14,7 @@ function createWindow() {
|
|||
});
|
||||
|
||||
// and load the index.html of the app.
|
||||
mainWindow.loadURL('https://photos.ente.io');
|
||||
mainWindow.loadURL('http://localhost:3000');
|
||||
|
||||
// Open the DevTools.
|
||||
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
|
||||
// code. You can also put them in separate files and require them here.
|
||||
|
|
|
@ -1,14 +1,41 @@
|
|||
// All of the Node.js APIs are available in the preload process.
|
||||
// It has the same sandbox as a Chrome extension.
|
||||
window.addEventListener("DOMContentLoaded", () => {
|
||||
const replaceText = (selector: string, text: string) => {
|
||||
const element = document.getElementById(selector);
|
||||
if (element) {
|
||||
element.innerText = text;
|
||||
import { Readable } from 'stream';
|
||||
import * as fs from 'fs';
|
||||
import * as electron from 'electron';
|
||||
|
||||
const { ipcRenderer } = electron;
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
for (const type of ["chrome", "node", "electron"]) {
|
||||
replaceText(`${type}-version`, process.versions[type as keyof NodeJS.ProcessVersions]);
|
||||
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,
|
||||
};
|
||||
|
|
|
@ -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.
|
Loading…
Reference in a new issue