download refactor

This commit is contained in:
Jan Prochazka
2020-11-29 21:20:27 +01:00
parent 6d7e7f97c7
commit 7e3555d84a
5 changed files with 26 additions and 41 deletions

View File

@@ -38,16 +38,7 @@ const dbgateApi = require(process.env.DBGATE_API);
${requirePluginsTemplate(extractShellApiPlugins(functionName, props))} ${requirePluginsTemplate(extractShellApiPlugins(functionName, props))}
require=null; require=null;
async function run() { async function run() {
${ const reader=await ${extractShellApiFunctionName(functionName)}(${JSON.stringify(props)});
props.downloadUrl
? `
const downloaded=await dbgateApi.download(${JSON.stringify(props.downloadUrl)});
const reader=await ${extractShellApiFunctionName(functionName)}(Object.assign(${JSON.stringify(
_.omit(props, ['downloadUrl'])
)}, { fileName: downloaded }));
`
: `const reader=await ${extractShellApiFunctionName(functionName)}(${JSON.stringify(props)});`
}
const writer=await dbgateApi.collectorWriter({runid: '${runid}'}); const writer=await dbgateApi.collectorWriter({runid: '${runid}'});
await dbgateApi.copyStream(reader, writer); await dbgateApi.copyStream(reader, writer);
} }

View File

@@ -4,9 +4,12 @@ const { uploadsdir } = require('../utility/directories');
const { downloadFile } = require('../utility/downloader'); const { downloadFile } = require('../utility/downloader');
async function download(url) { async function download(url) {
const tmpFile = path.join(uploadsdir(), uuidv1()); if (url && url.match(/(^http:\/\/)|(^https:\/\/)/)) {
await downloadFile(url, tmpFile); const tmpFile = path.join(uploadsdir(), uuidv1());
return tmpFile; await downloadFile(url, tmpFile);
return tmpFile;
}
return url;
} }
module.exports = download; module.exports = download;

View File

@@ -1,4 +1,4 @@
import { EngineDriver } from "./engines"; import { EngineDriver } from './engines';
export interface FileFormatDefinition { export interface FileFormatDefinition {
storageType: string; storageType: string;
@@ -7,9 +7,10 @@ export interface FileFormatDefinition {
readerFunc?: string; readerFunc?: string;
writerFunc?: string; writerFunc?: string;
args?: any[]; args?: any[];
addFilesToSourceList?: ( addFileToSourceList?: (
file: { file: {
full: string; fileName: string;
shortName: string;
}, },
newSources: string[], newSources: string[],
newValues: { newValues: {

View File

@@ -109,12 +109,11 @@ function getFileFilters(extensions, storageType) {
return res; return res;
} }
async function addFilesToSourceListDefault(file, newSources, newValues) { async function addFileToSourceListDefault({ fileName, shortName }, newSources, newValues) {
const sourceName = file.name; const sourceName = shortName;
newSources.push(sourceName); newSources.push(sourceName);
newValues[`sourceFile_${sourceName}`] = { newValues[`sourceFile_${sourceName}`] = {
fileName: file.full, fileName,
downloadUrl: file.url,
}; };
} }
@@ -125,7 +124,7 @@ async function addFilesToSourceList(extensions, files, values, setValues, prefer
for (const file of getAsArray(files)) { for (const file of getAsArray(files)) {
const format = findFileFormat(extensions, storage); const format = findFileFormat(extensions, storage);
if (format) { if (format) {
await (format.addFilesToSourceList || addFilesToSourceListDefault)(file, newSources, newValues); await (format.addFileToSourceList || addFileToSourceListDefault)(file, newSources, newValues);
} }
} }
newValues['sourceList'] = [...(values.sourceList || []).filter((x) => !newSources.includes(x)), ...newSources]; newValues['sourceList'] = [...(values.sourceList || []).filter((x) => !newSources.includes(x)), ...newSources];
@@ -159,8 +158,8 @@ function ElectronFilesInput() {
await addFilesToSourceList( await addFilesToSourceList(
extensions, extensions,
files.map((full) => ({ files.map((full) => ({
full, fileName: full,
...path.parse(full), shortName: path.parse(full).name,
})), })),
values, values,
setValues setValues
@@ -202,8 +201,9 @@ function FilesInput({ setPreviewSource = undefined }) {
extensions, extensions,
[ [
{ {
url, fileName: url,
name: extractUrlName(url), shortName: extractUrlName(url),
isDownload: true,
}, },
], ],
values, values,
@@ -385,11 +385,11 @@ function SourceName({ name }) {
); );
}; };
const doChangeUrl = (url) => { const doChangeUrl = (url) => {
setFieldValue(`sourceFile_${name}`, { downloadUrl: url }); setFieldValue(`sourceFile_${name}`, { fileName: url });
}; };
const handleChangeUrl = () => { const handleChangeUrl = () => {
showModal((modalState) => ( showModal((modalState) => (
<ChangeDownloadUrlModal modalState={modalState} url={obj.downloadUrl} onConfirm={doChangeUrl} /> <ChangeDownloadUrlModal modalState={modalState} url={obj.fileName} onConfirm={doChangeUrl} />
)); ));
}; };
@@ -397,7 +397,7 @@ function SourceName({ name }) {
<SourceNameWrapper> <SourceNameWrapper>
<div>{name}</div> <div>{name}</div>
<SourceNameButtons> <SourceNameButtons>
{obj && !!obj.downloadUrl && ( {obj && !!obj.isDownload && (
<IconButtonWrapper onClick={handleChangeUrl} theme={theme}> <IconButtonWrapper onClick={handleChangeUrl} theme={theme}>
<FontIcon icon="icon web" /> <FontIcon icon="icon web" />
</IconButtonWrapper> </IconButtonWrapper>
@@ -427,8 +427,8 @@ export default function ImportExportConfigurator({ uploadedFile = undefined, onC
extensions, extensions,
[ [
{ {
full: file.filePath, fileName: file.filePath,
name: file.shortName, shortName: file.shortName,
}, },
], ],
values, values,

View File

@@ -20,17 +20,7 @@ export default class ScriptWriter {
} }
assign(variableName, functionName, props) { assign(variableName, functionName, props) {
if (props.downloadUrl) { this.put(`const ${variableName} = await ${extractShellApiFunctionName(functionName)}(${JSON.stringify(props)});`);
const fileNameVar = this.allocVariable();
this.put(`const ${fileNameVar} = await dbgateApi.download(${JSON.stringify(props.downloadUrl)});`);
this.put(
`const ${variableName} = await ${extractShellApiFunctionName(functionName)}(Object.assign(${JSON.stringify(
_.omit(props, ['downloadUrl'])
)}, { fileName: ${fileNameVar} }));`
);
} else {
this.put(`const ${variableName} = await ${extractShellApiFunctionName(functionName)}(${JSON.stringify(props)});`);
}
this.packageNames.push(...extractShellApiPlugins(functionName, props)); this.packageNames.push(...extractShellApiPlugins(functionName, props));
} }