mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-27 02:05:59 +00:00
uploads - moved logic to FE because of plugins
This commit is contained in:
@@ -2,20 +2,20 @@ const path = require('path');
|
|||||||
const { uploadsdir } = require('../utility/directories');
|
const { uploadsdir } = require('../utility/directories');
|
||||||
const uuidv1 = require('uuid/v1');
|
const uuidv1 = require('uuid/v1');
|
||||||
|
|
||||||
const extensions = [
|
// const extensions = [
|
||||||
{
|
// {
|
||||||
ext: '.xlsx',
|
// ext: '.xlsx',
|
||||||
type: 'excel',
|
// type: 'excel',
|
||||||
},
|
// },
|
||||||
{
|
// {
|
||||||
ext: '.jsonl',
|
// ext: '.jsonl',
|
||||||
type: 'jsonl',
|
// type: 'jsonl',
|
||||||
},
|
// },
|
||||||
{
|
// {
|
||||||
ext: '.csv',
|
// ext: '.csv',
|
||||||
type: 'csv',
|
// type: 'csv',
|
||||||
},
|
// },
|
||||||
];
|
// ];
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
upload_meta: {
|
upload_meta: {
|
||||||
@@ -31,19 +31,19 @@ module.exports = {
|
|||||||
const uploadName = uuidv1();
|
const uploadName = uuidv1();
|
||||||
const filePath = path.join(uploadsdir(), uploadName);
|
const filePath = path.join(uploadsdir(), uploadName);
|
||||||
console.log(`Uploading file ${data.name}, size=${data.size}`);
|
console.log(`Uploading file ${data.name}, size=${data.size}`);
|
||||||
let storageType = null;
|
// let storageType = null;
|
||||||
let shortName = data.name;
|
// let shortName = data.name;
|
||||||
for (const { ext, type } of extensions) {
|
// for (const { ext, type } of extensions) {
|
||||||
if (data.name.endsWith(ext)) {
|
// if (data.name.endsWith(ext)) {
|
||||||
storageType = type;
|
// storageType = type;
|
||||||
shortName = data.name.slice(0, -ext.length);
|
// shortName = data.name.slice(0, -ext.length);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
data.mv(filePath, () => {
|
data.mv(filePath, () => {
|
||||||
res.json({
|
res.json({
|
||||||
originalName: data.name,
|
originalName: data.name,
|
||||||
shortName,
|
// shortName,
|
||||||
storageType,
|
// storageType,
|
||||||
uploadName,
|
uploadName,
|
||||||
filePath,
|
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 './dialect';
|
||||||
export * from './dumper';
|
export * from './dumper';
|
||||||
export * from './dbtypes';
|
export * from './dbtypes';
|
||||||
export * from './fileformat';
|
export * from './extensions';
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ export function useUploadsZone() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('FILE', file);
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append('data', file);
|
formData.append('data', file);
|
||||||
|
|
||||||
@@ -42,6 +43,15 @@ export function useUploadsZone() {
|
|||||||
const resp = await fetch(`${apiBase}/uploads/upload`, fetchOptions);
|
const resp = await fetch(`${apiBase}/uploads/upload`, fetchOptions);
|
||||||
const fileData = await resp.json();
|
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) {
|
if (uploadListener) {
|
||||||
uploadListener(fileData);
|
uploadListener(fileData);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -56,6 +56,7 @@ const jsonlFormat = {
|
|||||||
writerFunc: 'jsonLinesWriter',
|
writerFunc: 'jsonLinesWriter',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** @returns {import('dbgate-types').FileFormatDefinition[]} */
|
||||||
export function buildFileFormats(plugins) {
|
export function buildFileFormats(plugins) {
|
||||||
const res = [excelFormat, jsonlFormat];
|
const res = [excelFormat, jsonlFormat];
|
||||||
for (const { content } of plugins) {
|
for (const { content } of plugins) {
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ export function ExtensionsProvider({ children }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function buildExtensions(plugins) {
|
export function buildExtensions(plugins) {
|
||||||
|
/** @type {import('dbgate-types').ExtensionsDirectory} */
|
||||||
const extensions = {
|
const extensions = {
|
||||||
plugins,
|
plugins,
|
||||||
fileFormats: buildFileFormats(plugins),
|
fileFormats: buildFileFormats(plugins),
|
||||||
@@ -18,6 +19,7 @@ export function buildExtensions(plugins) {
|
|||||||
return extensions;
|
return extensions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @returns {import('dbgate-types').ExtensionsDirectory} */
|
||||||
export default function useExtensions() {
|
export default function useExtensions() {
|
||||||
return React.useContext(ExtensionsContext);
|
return React.useContext(ExtensionsContext);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user