uploads - moved logic to FE because of plugins

This commit is contained in:
Jan Prochazka 2020-11-22 09:14:19 +01:00
parent 7d1c0c5c18
commit 3cdba4339f
7 changed files with 68 additions and 45 deletions

View File

@ -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
View 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[];
}

View File

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

View File

@ -39,4 +39,4 @@ export * from './query';
export * from './dialect';
export * from './dumper';
export * from './dbtypes';
export * from './fileformat';
export * from './extensions';

View File

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

View File

@ -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) {

View File

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