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))}
require=null;
async function run() {
${
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 reader=await ${extractShellApiFunctionName(functionName)}(${JSON.stringify(props)});
const writer=await dbgateApi.collectorWriter({runid: '${runid}'});
await dbgateApi.copyStream(reader, writer);
}

View File

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

View File

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

View File

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

View File

@@ -20,17 +20,7 @@ export default class ScriptWriter {
}
assign(variableName, functionName, props) {
if (props.downloadUrl) {
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.put(`const ${variableName} = await ${extractShellApiFunctionName(functionName)}(${JSON.stringify(props)});`);
this.packageNames.push(...extractShellApiPlugins(functionName, props));
}