mirror of
https://github.com/dbgate/dbgate
synced 2024-11-07 20:26:23 +00:00
uploads - moved logic to FE because of plugins
This commit is contained in:
parent
7d1c0c5c18
commit
3cdba4339f
@ -2,20 +2,20 @@ const path = require('path');
|
||||
const { uploadsdir } = require('../utility/directories');
|
||||
const uuidv1 = require('uuid/v1');
|
||||
|
||||
const extensions = [
|
||||
{
|
||||
ext: '.xlsx',
|
||||
type: 'excel',
|
||||
},
|
||||
{
|
||||
ext: '.jsonl',
|
||||
type: 'jsonl',
|
||||
},
|
||||
{
|
||||
ext: '.csv',
|
||||
type: 'csv',
|
||||
},
|
||||
];
|
||||
// const extensions = [
|
||||
// {
|
||||
// ext: '.xlsx',
|
||||
// type: 'excel',
|
||||
// },
|
||||
// {
|
||||
// ext: '.jsonl',
|
||||
// type: 'jsonl',
|
||||
// },
|
||||
// {
|
||||
// ext: '.csv',
|
||||
// type: 'csv',
|
||||
// },
|
||||
// ];
|
||||
|
||||
module.exports = {
|
||||
upload_meta: {
|
||||
@ -31,19 +31,19 @@ module.exports = {
|
||||
const uploadName = uuidv1();
|
||||
const filePath = path.join(uploadsdir(), uploadName);
|
||||
console.log(`Uploading file ${data.name}, size=${data.size}`);
|
||||
let storageType = null;
|
||||
let shortName = data.name;
|
||||
for (const { ext, type } of extensions) {
|
||||
if (data.name.endsWith(ext)) {
|
||||
storageType = type;
|
||||
shortName = data.name.slice(0, -ext.length);
|
||||
}
|
||||
}
|
||||
// let storageType = null;
|
||||
// let shortName = data.name;
|
||||
// for (const { ext, type } of extensions) {
|
||||
// if (data.name.endsWith(ext)) {
|
||||
// storageType = type;
|
||||
// shortName = data.name.slice(0, -ext.length);
|
||||
// }
|
||||
// }
|
||||
data.mv(filePath, () => {
|
||||
res.json({
|
||||
originalName: data.name,
|
||||
shortName,
|
||||
storageType,
|
||||
// shortName,
|
||||
// storageType,
|
||||
uploadName,
|
||||
filePath,
|
||||
});
|
||||
|
30
packages/types/extensions.d.ts
vendored
Normal file
30
packages/types/extensions.d.ts
vendored
Normal file
@ -0,0 +1,30 @@
|
||||
export interface FileFormatDefinition {
|
||||
storageType: string;
|
||||
extension: string;
|
||||
name: string;
|
||||
readerFunc?: string;
|
||||
writerFunc?: string;
|
||||
args?: any[];
|
||||
addFilesToSourceList?: (
|
||||
file: {
|
||||
full: string;
|
||||
},
|
||||
newSources: string[],
|
||||
newValues: {
|
||||
[key: string]: any;
|
||||
}
|
||||
) => void;
|
||||
getDefaultOutputName?: (sourceName, values) => string;
|
||||
getOutputParams?: (sourceName, values) => any;
|
||||
}
|
||||
|
||||
export interface PluginDefinition {
|
||||
packageName: string;
|
||||
manifest: any;
|
||||
content: any;
|
||||
}
|
||||
|
||||
export interface ExtensionsDirectory {
|
||||
plugins: PluginDefinition[];
|
||||
fileFormats: FileFormatDefinition[];
|
||||
}
|
20
packages/types/fileformat.d.ts
vendored
20
packages/types/fileformat.d.ts
vendored
@ -1,20 +0,0 @@
|
||||
export interface FileFormatDefinition {
|
||||
storageType: string;
|
||||
extension: string;
|
||||
name: string;
|
||||
readerFunc?: string;
|
||||
writerFunc?: string;
|
||||
args?: any[];
|
||||
addFilesToSourceList: (
|
||||
file: {
|
||||
full: string;
|
||||
},
|
||||
newSources: string[],
|
||||
newValues: {
|
||||
[key: string]: any;
|
||||
}
|
||||
) => void;
|
||||
getDefaultOutputName?: (sourceName, values) => string;
|
||||
getOutputParams?: (sourceName, values) => any;
|
||||
}
|
||||
|
2
packages/types/index.d.ts
vendored
2
packages/types/index.d.ts
vendored
@ -39,4 +39,4 @@ export * from './query';
|
||||
export * from './dialect';
|
||||
export * from './dumper';
|
||||
export * from './dbtypes';
|
||||
export * from './fileformat';
|
||||
export * from './extensions';
|
||||
|
@ -30,6 +30,7 @@ export function useUploadsZone() {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('FILE', file);
|
||||
const formData = new FormData();
|
||||
formData.append('data', file);
|
||||
|
||||
@ -42,6 +43,15 @@ export function useUploadsZone() {
|
||||
const resp = await fetch(`${apiBase}/uploads/upload`, fetchOptions);
|
||||
const fileData = await resp.json();
|
||||
|
||||
fileData.shortName = file.name;
|
||||
|
||||
for (const format of extensions.fileFormats) {
|
||||
if (file.name.endsWith('.' + format.extension)) {
|
||||
fileData.shortName = file.name.slice(-format.extension.length - 1);
|
||||
fileData.storageType = format.storageType;
|
||||
}
|
||||
}
|
||||
|
||||
if (uploadListener) {
|
||||
uploadListener(fileData);
|
||||
} else {
|
||||
|
@ -56,6 +56,7 @@ const jsonlFormat = {
|
||||
writerFunc: 'jsonLinesWriter',
|
||||
};
|
||||
|
||||
/** @returns {import('dbgate-types').FileFormatDefinition[]} */
|
||||
export function buildFileFormats(plugins) {
|
||||
const res = [excelFormat, jsonlFormat];
|
||||
for (const { content } of plugins) {
|
||||
|
@ -11,6 +11,7 @@ export function ExtensionsProvider({ children }) {
|
||||
}
|
||||
|
||||
export function buildExtensions(plugins) {
|
||||
/** @type {import('dbgate-types').ExtensionsDirectory} */
|
||||
const extensions = {
|
||||
plugins,
|
||||
fileFormats: buildFileFormats(plugins),
|
||||
@ -18,6 +19,7 @@ export function buildExtensions(plugins) {
|
||||
return extensions;
|
||||
}
|
||||
|
||||
/** @returns {import('dbgate-types').ExtensionsDirectory} */
|
||||
export default function useExtensions() {
|
||||
return React.useContext(ExtensionsContext);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user