diff --git a/api/src/controllers/internal/Execution.ts b/api/src/controllers/internal/Execution.ts index b246a20..50620c1 100644 --- a/api/src/controllers/internal/Execution.ts +++ b/api/src/controllers/internal/Execution.ts @@ -14,6 +14,7 @@ import { PreProgramVars, Session, TreeNode } from '../../types' import { extractHeaders, generateFileUploadSasCode, + generateFileUploadJSCode, getFilesFolder, getMacrosFolder, HTTPHeaders, @@ -116,8 +117,10 @@ export class ExecutionController { program, preProgramVariables, vars, + session, weboutPath, - tokenFile + tokenFile, + otherArgs ) const codePath = path.join(session.path, 'code.js') @@ -280,8 +283,10 @@ ${program}` program: string, preProgramVariables: PreProgramVars, vars: ExecutionVars, + session: Session, weboutPath: string, - tokenFile: string + tokenFile: string, + otherArgs?: any ) { const varStatments = Object.keys(vars).reduce( (computed: string, key: string) => @@ -300,10 +305,9 @@ const _metauser = _sasjs_username; const sasjsprocessmode = 'Stored Program'; ` - program = ` -/*require module for writing webout file*/ -const fs = require('fs-extra') + const requiredModules = `const fs = require('fs-extra')` + program = ` /* runtime vars */ ${varStatments} @@ -316,21 +320,19 @@ ${program} /* write webout file*/ fs.promises.writeFile(weboutPath, _webout) ` - - // todo: modify this commented block for js runtime // if no files are uploaded filesNamesMap will be undefined - // if (otherArgs?.filesNamesMap) { - // const uploadSasCode = await generateFileUploadSasCode( - // otherArgs.filesNamesMap, - // session.path - // ) + if (otherArgs?.filesNamesMap) { + const uploadJSCode = await generateFileUploadJSCode( + otherArgs.filesNamesMap, + session.path + ) - // //If sas code for the file is generated it will be appended to the top of sasCode - // if (uploadSasCode.length > 0) { - // program = `${uploadSasCode}` + program - // } - // } - return program + //If js code for the file is generated it will be appended to the top of jsCode + if (uploadJSCode.length > 0) { + program = `${uploadJSCode}\n` + program + } + } + return requiredModules + program } buildDirectoryTree() { diff --git a/api/src/utils/upload.ts b/api/src/utils/upload.ts index 640464c..cc9a47f 100644 --- a/api/src/utils/upload.ts +++ b/api/src/utils/upload.ts @@ -1,5 +1,6 @@ +import path from 'path' import { MulterFile } from '../types/Upload' -import { listFilesInFolder } from '@sasjs/utils' +import { listFilesInFolder, readFileBinary } from '@sasjs/utils' interface FilenameMapSingle { fieldName: string @@ -98,3 +99,34 @@ export const generateFileUploadSasCode = async ( return uploadSasCode } + +/** + * Generates the js code that references uploaded files in the concurrent request + * @param filesNamesMap object that maps hashed file names and original file names + * @param sessionFolder name of the folder that is created for the purpose of files in concurrent request + * @returns generated js code + */ +export const generateFileUploadJSCode = async ( + filesNamesMap: FilenamesMap, + sessionFolder: string +) => { + let uploadCode = '' + let fileCount = 0 + + const sessionFolderList: string[] = await listFilesInFolder(sessionFolder) + sessionFolderList.forEach(async (fileName) => { + if (fileName.includes('req_file')) { + fileCount++ + const filePath = path.join(sessionFolder, fileName) + uploadCode += `\nconst _WEBIN_FILEREF${fileCount} = fs.readFileSync('${filePath}')` + uploadCode += `\nconst _WEBIN_FILENAME${fileCount} = '${filesNamesMap[fileName].originalName}'` + uploadCode += `\nconst _WEBIN_NAME${fileCount} = '${filesNamesMap[fileName].fieldName}'` + } + }) + + if (fileCount) { + uploadCode = `\nconst _WEBIN_FILE_COUNT = ${fileCount}` + uploadCode + } + + return uploadCode +}