mirror of
https://github.com/sasjs/server.git
synced 2025-12-11 19:44:35 +00:00
feat: create and inject code for uploaded files to code.js
This commit is contained in:
@@ -14,6 +14,7 @@ import { PreProgramVars, Session, TreeNode } from '../../types'
|
|||||||
import {
|
import {
|
||||||
extractHeaders,
|
extractHeaders,
|
||||||
generateFileUploadSasCode,
|
generateFileUploadSasCode,
|
||||||
|
generateFileUploadJSCode,
|
||||||
getFilesFolder,
|
getFilesFolder,
|
||||||
getMacrosFolder,
|
getMacrosFolder,
|
||||||
HTTPHeaders,
|
HTTPHeaders,
|
||||||
@@ -116,8 +117,10 @@ export class ExecutionController {
|
|||||||
program,
|
program,
|
||||||
preProgramVariables,
|
preProgramVariables,
|
||||||
vars,
|
vars,
|
||||||
|
session,
|
||||||
weboutPath,
|
weboutPath,
|
||||||
tokenFile
|
tokenFile,
|
||||||
|
otherArgs
|
||||||
)
|
)
|
||||||
|
|
||||||
const codePath = path.join(session.path, 'code.js')
|
const codePath = path.join(session.path, 'code.js')
|
||||||
@@ -280,8 +283,10 @@ ${program}`
|
|||||||
program: string,
|
program: string,
|
||||||
preProgramVariables: PreProgramVars,
|
preProgramVariables: PreProgramVars,
|
||||||
vars: ExecutionVars,
|
vars: ExecutionVars,
|
||||||
|
session: Session,
|
||||||
weboutPath: string,
|
weboutPath: string,
|
||||||
tokenFile: string
|
tokenFile: string,
|
||||||
|
otherArgs?: any
|
||||||
) {
|
) {
|
||||||
const varStatments = Object.keys(vars).reduce(
|
const varStatments = Object.keys(vars).reduce(
|
||||||
(computed: string, key: string) =>
|
(computed: string, key: string) =>
|
||||||
@@ -300,10 +305,9 @@ const _metauser = _sasjs_username;
|
|||||||
const sasjsprocessmode = 'Stored Program';
|
const sasjsprocessmode = 'Stored Program';
|
||||||
`
|
`
|
||||||
|
|
||||||
program = `
|
const requiredModules = `const fs = require('fs-extra')`
|
||||||
/*require module for writing webout file*/
|
|
||||||
const fs = require('fs-extra')
|
|
||||||
|
|
||||||
|
program = `
|
||||||
/* runtime vars */
|
/* runtime vars */
|
||||||
${varStatments}
|
${varStatments}
|
||||||
|
|
||||||
@@ -316,21 +320,19 @@ ${program}
|
|||||||
/* write webout file*/
|
/* write webout file*/
|
||||||
fs.promises.writeFile(weboutPath, _webout)
|
fs.promises.writeFile(weboutPath, _webout)
|
||||||
`
|
`
|
||||||
|
|
||||||
// todo: modify this commented block for js runtime
|
|
||||||
// if no files are uploaded filesNamesMap will be undefined
|
// if no files are uploaded filesNamesMap will be undefined
|
||||||
// if (otherArgs?.filesNamesMap) {
|
if (otherArgs?.filesNamesMap) {
|
||||||
// const uploadSasCode = await generateFileUploadSasCode(
|
const uploadJSCode = await generateFileUploadJSCode(
|
||||||
// otherArgs.filesNamesMap,
|
otherArgs.filesNamesMap,
|
||||||
// session.path
|
session.path
|
||||||
// )
|
)
|
||||||
|
|
||||||
// //If sas code for the file is generated it will be appended to the top of sasCode
|
//If js code for the file is generated it will be appended to the top of jsCode
|
||||||
// if (uploadSasCode.length > 0) {
|
if (uploadJSCode.length > 0) {
|
||||||
// program = `${uploadSasCode}` + program
|
program = `${uploadJSCode}\n` + program
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
return program
|
return requiredModules + program
|
||||||
}
|
}
|
||||||
|
|
||||||
buildDirectoryTree() {
|
buildDirectoryTree() {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
|
import path from 'path'
|
||||||
import { MulterFile } from '../types/Upload'
|
import { MulterFile } from '../types/Upload'
|
||||||
import { listFilesInFolder } from '@sasjs/utils'
|
import { listFilesInFolder, readFileBinary } from '@sasjs/utils'
|
||||||
|
|
||||||
interface FilenameMapSingle {
|
interface FilenameMapSingle {
|
||||||
fieldName: string
|
fieldName: string
|
||||||
@@ -98,3 +99,34 @@ export const generateFileUploadSasCode = async (
|
|||||||
|
|
||||||
return uploadSasCode
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user