From 18bf6e5979bb9c68445cb17a7bd8823157c15937 Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Sun, 31 Jan 2021 09:21:54 +0100 Subject: [PATCH] open data files using open dialog in electron + drag & drop in electron without uploading --- app/src/electron.js | 4 +- .../src/impexp/ImportExportConfigurator.js | 21 ++++++- packages/web/src/modals/ImportExportModal.js | 7 ++- packages/web/src/utility/UploadsProvider.js | 2 +- .../web/src/utility/useOpenElectronFile.js | 62 ++++++++++++++++--- 5 files changed, 83 insertions(+), 13 deletions(-) diff --git a/app/src/electron.js b/app/src/electron.js index 431398ab..d6df7c9b 100644 --- a/app/src/electron.js +++ b/app/src/electron.js @@ -129,7 +129,7 @@ function buildMenu() { { label: 'DbGate on GitHub', click() { - require('electron').shell.openExternal('https://github.com/dbshell/dbgate'); + require('electron').shell.openExternal('https://github.com/dbgate/dbgate'); }, }, { @@ -141,7 +141,7 @@ function buildMenu() { { label: 'Report problem or feature request', click() { - require('electron').shell.openExternal('https://github.com/dbshell/dbgate/issues/new'); + require('electron').shell.openExternal('https://github.com/dbgate/dbgate/issues/new'); }, }, { diff --git a/packages/web/src/impexp/ImportExportConfigurator.js b/packages/web/src/impexp/ImportExportConfigurator.js index b575c0de..17c843aa 100644 --- a/packages/web/src/impexp/ImportExportConfigurator.js +++ b/packages/web/src/impexp/ImportExportConfigurator.js @@ -411,7 +411,11 @@ function SourceName({ name }) { ); } -export default function ImportExportConfigurator({ uploadedFile = undefined, onChangePreview = undefined }) { +export default function ImportExportConfigurator({ + uploadedFile = undefined, + openedFile = undefined, + onChangePreview = undefined, +}) { const { values, setFieldValue, setValues } = useForm(); const targetDbinfo = useDatabaseInfo({ conid: values.targetConnectionId, database: values.targetDatabaseName }); const sourceConnectionInfo = useConnectionInfo({ conid: values.sourceConnectionId }); @@ -453,6 +457,21 @@ export default function ImportExportConfigurator({ uploadedFile = undefined, onC if (uploadedFile) { handleUpload(uploadedFile); } + if (openedFile) { + addFilesToSourceList( + extensions, + [ + { + fileName: openedFile.filePath, + shortName: openedFile.shortName, + }, + ], + values, + setValues, + !sourceList || sourceList.length == 0 ? openedFile.storageType : null, + setPreviewSource + ); + } }, []); const supportsPreview = diff --git a/packages/web/src/modals/ImportExportModal.js b/packages/web/src/modals/ImportExportModal.js index 02836387..b7b2b319 100644 --- a/packages/web/src/modals/ImportExportModal.js +++ b/packages/web/src/modals/ImportExportModal.js @@ -120,6 +120,7 @@ export default function ImportExportModal({ modalState, initialValues, uploadedFile = undefined, + openedFile = undefined, importToArchive = false, }) { const [executeNumber, setExecuteNumber] = React.useState(0); @@ -195,7 +196,11 @@ export default function ImportExportModal({ Import/Export {busy && } - + diff --git a/packages/web/src/utility/UploadsProvider.js b/packages/web/src/utility/UploadsProvider.js index bd4e49e8..726a54db 100644 --- a/packages/web/src/utility/UploadsProvider.js +++ b/packages/web/src/utility/UploadsProvider.js @@ -36,7 +36,7 @@ export function useUploadFiles() { console.log('FILE', file); - if (electron && canOpenByElectron(file.path)) { + if (electron && canOpenByElectron(file.path, extensions)) { openElectronFileCore(file.path); return; } diff --git a/packages/web/src/utility/useOpenElectronFile.js b/packages/web/src/utility/useOpenElectronFile.js index 04af3706..5240cca0 100644 --- a/packages/web/src/utility/useOpenElectronFile.js +++ b/packages/web/src/utility/useOpenElectronFile.js @@ -1,18 +1,33 @@ +import _ from 'lodash'; +import React from 'react'; +import ImportExportModal from '../modals/ImportExportModal'; +import useShowModal from '../modals/showModal'; import useNewQuery from '../query/useNewQuery'; import getElectron from './getElectron'; +import useExtensions from './useExtensions'; -export function canOpenByElectron(file) { - return file && file.toLowerCase().endsWith('.sql'); +export function canOpenByElectron(file, extensions) { + if (!file) return false; + const nameLower = file.toLowerCase(); + if (nameLower.endsWith('.sql')) return true; + for (const format of extensions.fileFormats) { + if (nameLower.endsWith(`.${format.extension}`)) return true; + } + return false; } export function useOpenElectronFileCore() { const newQuery = useNewQuery(); + const extensions = useExtensions(); + const showModal = useShowModal(); return filePath => { - if (filePath.toLowerCase().endsWith('.sql')) { - const path = window.require('path'); - const fs = window.require('fs'); - const parsed = path.parse(filePath); + const nameLower = filePath.toLowerCase(); + const path = window.require('path'); + const fs = window.require('fs'); + const parsed = path.parse(filePath); + + if (nameLower.endsWith('.sql')) { const data = fs.readFileSync(filePath, { encoding: 'utf-8' }); newQuery({ @@ -23,19 +38,50 @@ export function useOpenElectronFileCore() { savedFormat: 'text', }); } + for (const format of extensions.fileFormats) { + if (nameLower.endsWith(`.${format.extension}`)) { + showModal(modalState => ( + + )); + } + } }; } +function getFileFormatFilters(extensions) { + return extensions.fileFormats.filter(x => x.readerFunc).map(x => ({ name: x.name, extensions: [x.extension] })); +} + +function getFileFormatExtensions(extensions) { + return extensions.fileFormats.filter(x => x.readerFunc).map(x => x.extension); +} + export default function useOpenElectronFile() { const electron = getElectron(); const openElectronFileCore = useOpenElectronFileCore(); + const extensions = useExtensions(); return () => { const filePaths = electron.remote.dialog.showOpenDialogSync(electron.remote.getCurrentWindow(), { - filters: [{ name: `SQL files`, extensions: ['sql'] }], + filters: [ + { name: `All supported files`, extensions: ['sql', ...getFileFormatExtensions(extensions)] }, + { name: `SQL files`, extensions: ['sql'] }, + ...getFileFormatFilters(extensions), + ], }); const filePath = filePaths && filePaths[0]; - if (canOpenByElectron(filePath)) { + if (canOpenByElectron(filePath, extensions)) { openElectronFileCore(filePath); } };