export html file both from web page and electron

This commit is contained in:
Jan Prochazka
2021-11-11 14:33:36 +01:00
parent b9da035a97
commit d2299ab926
7 changed files with 113 additions and 26 deletions

View File

@@ -3,6 +3,7 @@ import getElectron from './getElectron';
import axiosInstance from '../utility/axiosInstance';
import socket from '../utility/socket';
import { showSnackbar, showSnackbarInfo, showSnackbarError, closeSnackbar } from '../utility/snackbar';
import resolveApi from './resolveApi';
export async function exportElectronFile(dataName, reader, format) {
const electron = getElectron();
@@ -54,3 +55,27 @@ export async function exportElectronFile(dataName, reader, format) {
socket.on(`runner-done-${runid}`, handleRunnerDone);
}
export async function saveFileToDisk(
filePathFunc,
options: any = { formatLabel: 'HTML page', formatExtension: 'html' }
) {
const { formatLabel, formatExtension } = options;
const electron = getElectron();
if (electron) {
const filters = [{ name: formatLabel, extensions: [formatExtension] }];
const filePath = electron.remote.dialog.showSaveDialogSync(electron.remote.getCurrentWindow(), {
filters,
defaultPath: `file.${formatExtension}`,
properties: ['showOverwriteConfirmation'],
});
if (!filePath) return;
await filePathFunc(filePath);
electron.shell.openExternal('file:///' + filePath);
} else {
const resp = await axiosInstance.get('files/generate-uploads-file');
await filePathFunc(resp.data.filePath);
window.open(`${resolveApi()}/uploads/get?file=${resp.data.fileName}`, '_blank');
}
}