mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-23 03:26:00 +00:00
open data files using open dialog in electron + drag & drop in electron without uploading
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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 => (
|
||||
<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);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user