open data files using open dialog in electron + drag & drop in electron without uploading

This commit is contained in:
Jan Prochazka 2021-01-31 09:21:54 +01:00
parent 5c7a011efb
commit 18bf6e5979
5 changed files with 83 additions and 13 deletions

View File

@ -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');
},
},
{

View File

@ -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 =

View File

@ -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({
<ModalHeader modalState={modalState}>Import/Export {busy && <FontIcon icon="icon loading" />}</ModalHeader>
<Wrapper>
<ContentWrapper theme={theme}>
<ImportExportConfigurator uploadedFile={uploadedFile} onChangePreview={setPreviewReader} />
<ImportExportConfigurator
uploadedFile={uploadedFile}
openedFile={openedFile}
onChangePreview={setPreviewReader}
/>
</ContentWrapper>
<WidgetColumnWrapper theme={theme}>
<WidgetColumnBar>

View File

@ -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;
}

View File

@ -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 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 => (
<ImportExportModal
openedFile={{
filePath,
storageType: format.storageType,
shortName: parsed.name,
}}
modalState={modalState}
importToArchive
initialValues={{
sourceStorageType: format.storageType,
}}
/>
));
}
}
};
}
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);
}
};