diff --git a/app/file.html b/app/file.html new file mode 100644 index 00000000..32a47148 --- /dev/null +++ b/app/file.html @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + \ No newline at end of file diff --git a/app/file.html.png b/app/file.html.png new file mode 100644 index 00000000..30b37f1c Binary files /dev/null and b/app/file.html.png differ diff --git a/packages/api/src/controllers/databaseConnections.js b/packages/api/src/controllers/databaseConnections.js index 3a50d538..3599d7c0 100644 --- a/packages/api/src/controllers/databaseConnections.js +++ b/packages/api/src/controllers/databaseConnections.js @@ -339,16 +339,15 @@ module.exports = { }, generateDbDiffReport_meta: 'post', - async generateDbDiffReport({ sourceConid, sourceDatabase, targetConid, targetDatabase }) { + async generateDbDiffReport({ filePath, sourceConid, sourceDatabase, targetConid, targetDatabase }) { const unifiedDiff = await this.getUnifiedDiff({ sourceConid, sourceDatabase, targetConid, targetDatabase }); const diffJson = parse(unifiedDiff); // $: diffHtml = html(diffJson, { outputFormat: 'side-by-side', drawFileList: false }); const diffHtml = html(diffJson, { outputFormat: 'side-by-side' }); - const fileName = `${uuidv1()}.html`; - await fs.writeFile(path.join(uploadsdir(), fileName), diff2htmlPage(diffHtml)); + await fs.writeFile(filePath, diff2htmlPage(diffHtml)); - return fileName; + return true; }, }; diff --git a/packages/api/src/controllers/files.js b/packages/api/src/controllers/files.js index bceed001..ab1ae132 100644 --- a/packages/api/src/controllers/files.js +++ b/packages/api/src/controllers/files.js @@ -117,20 +117,29 @@ module.exports = { return res; }, - exportChart_meta: 'post', - async exportChart({ title, config, image }) { + generateUploadsFile_meta: 'get', + async generateUploadsFile() { const fileName = `${uuidv1()}.html`; + return { + fileName, + filePath: path.join(uploadsdir(), fileName), + }; + }, + + exportChart_meta: 'post', + async exportChart({ filePath, title, config, image }) { + const fileName = path.parse(filePath).base; const imageFile = `${fileName}.png`; const html = getChartExport(title, config, imageFile); - await fs.writeFile(path.join(uploadsdir(), fileName), html); + await fs.writeFile(filePath, html); if (image) { const index = image.indexOf('base64,'); if (index > 0) { const data = image.substr(index + 'base64,'.length); const buf = Buffer.from(data, 'base64'); - await fs.writeFile(path.join(uploadsdir(), imageFile), buf); + await fs.writeFile(filePath + '.png', buf); } } - return fileName; + return true; }, }; diff --git a/packages/web/src/charts/ChartCore.svelte b/packages/web/src/charts/ChartCore.svelte index a45f60aa..e051180c 100644 --- a/packages/web/src/charts/ChartCore.svelte +++ b/packages/web/src/charts/ChartCore.svelte @@ -24,6 +24,7 @@ import contextMenu, { getContextMenu, registerMenu } from '../utility/contextMenu'; import createActivator, { getActiveComponent } from '../utility/createActivator'; +import { saveFileToDisk } from '../utility/exportElectronFile'; import resolveApi from '../utility/resolveApi'; export let data; @@ -60,17 +61,18 @@ }); export async function exportChart() { - const resp = await axiosInstance.post('files/export-chart', { - title, - config: { - type, - data, - options, - }, - image: domChart.toDataURL(), + saveFileToDisk(async filePath => { + await axiosInstance.post('files/export-chart', { + title, + filePath, + config: { + type, + data, + options, + }, + image: domChart.toDataURL(), + }); }); - - window.open(`${resolveApi()}/uploads/get?file=${resp.data}`, '_blank'); } registerMenu({ command: 'chart.export', tag: 'export' }); diff --git a/packages/web/src/tabs/CompareModelTab.svelte b/packages/web/src/tabs/CompareModelTab.svelte index fcd50414..f2dc3198 100644 --- a/packages/web/src/tabs/CompareModelTab.svelte +++ b/packages/web/src/tabs/CompareModelTab.svelte @@ -156,6 +156,7 @@ import { changeTab } from '../utility/common'; import contextMenu, { getContextMenu, registerMenu } from '../utility/contextMenu'; import createActivator, { getActiveComponent } from '../utility/createActivator'; + import { saveFileToDisk } from '../utility/exportElectronFile'; import { useArchiveFolders, useConnectionInfo, useDatabaseInfo } from '../utility/metadataLoaders'; import resolveApi from '../utility/resolveApi'; import { showSnackbarSuccess } from '../utility/snackbar'; @@ -212,14 +213,15 @@ })); export async function showReport() { - const resp = await axiosInstance.post('database-connections/generate-db-diff-report', { - sourceConid: $values?.sourceConid, - sourceDatabase: $values?.sourceDatabase, - targetConid: $values?.targetConid, - targetDatabase: $values?.targetDatabase, + saveFileToDisk(async filePath => { + await axiosInstance.post('database-connections/generate-db-diff-report', { + filePath, + sourceConid: $values?.sourceConid, + sourceDatabase: $values?.sourceDatabase, + targetConid: $values?.targetConid, + targetDatabase: $values?.targetDatabase, + }); }); - - window.open(`${resolveApi()}/uploads/get?file=${resp.data}`, '_blank'); } export function swap() { diff --git a/packages/web/src/utility/exportElectronFile.ts b/packages/web/src/utility/exportElectronFile.ts index 2bf4caef..eceea5cf 100644 --- a/packages/web/src/utility/exportElectronFile.ts +++ b/packages/web/src/utility/exportElectronFile.ts @@ -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'); + } +}