diff --git a/api/src/utils/upload.ts b/api/src/utils/upload.ts index 3ad4ad3..640464c 100644 --- a/api/src/utils/upload.ts +++ b/api/src/utils/upload.ts @@ -1,9 +1,21 @@ -import path from 'path' -import fs from 'fs' -import { getTmpSessionsFolderPath } from '.' import { MulterFile } from '../types/Upload' import { listFilesInFolder } from '@sasjs/utils' +interface FilenameMapSingle { + fieldName: string + originalName: string +} + +interface FilenamesMap { + [key: string]: FilenameMapSingle +} + +interface UploadedFiles extends FilenameMapSingle { + fileref: string + filepath: string + count: number +} + /** * It will create an object that maps hashed file names to the original names * @param files array of files to be mapped @@ -12,10 +24,13 @@ import { listFilesInFolder } from '@sasjs/utils' export const makeFilesNamesMap = (files: MulterFile[]) => { if (!files) return null - const filesNamesMap: { [key: string]: string } = {} + const filesNamesMap: FilenamesMap = {} for (let file of files) { - filesNamesMap[file.filename] = file.originalname + filesNamesMap[file.filename] = { + fieldName: file.fieldname, + originalName: file.originalname + } } return filesNamesMap @@ -28,17 +43,12 @@ export const makeFilesNamesMap = (files: MulterFile[]) => { * @returns generated sas code */ export const generateFileUploadSasCode = async ( - filesNamesMap: any, + filesNamesMap: FilenamesMap, sasSessionFolder: string ): Promise => { let uploadSasCode = '' let fileCount = 0 - let uploadedFilesMap: { - fileref: string - filepath: string - filename: string - count: number - }[] = [] + const uploadedFiles: UploadedFiles[] = [] const sasSessionFolderList: string[] = await listFilesInFolder( sasSessionFolder @@ -50,31 +60,32 @@ export const generateFileUploadSasCode = async ( if (fileName.includes('req_file')) { fileCount++ - uploadedFilesMap.push({ + uploadedFiles.push({ fileref: `_sjs${fileCountString}`, filepath: `${sasSessionFolder}/${fileName}`, - filename: filesNamesMap[fileName], + originalName: filesNamesMap[fileName].originalName, + fieldName: filesNamesMap[fileName].fieldName, count: fileCount }) } }) - for (let uploadedMap of uploadedFilesMap) { - uploadSasCode += `\nfilename ${uploadedMap.fileref} "${uploadedMap.filepath}";` + for (const uploadedFile of uploadedFiles) { + uploadSasCode += `\nfilename ${uploadedFile.fileref} "${uploadedFile.filepath}";` } uploadSasCode += `\n%let _WEBIN_FILE_COUNT=${fileCount};` - for (let uploadedMap of uploadedFilesMap) { - uploadSasCode += `\n%let _WEBIN_FILENAME${uploadedMap.count}=${uploadedMap.filename};` + for (const uploadedFile of uploadedFiles) { + uploadSasCode += `\n%let _WEBIN_FILENAME${uploadedFile.count}=${uploadedFile.originalName};` } - for (let uploadedMap of uploadedFilesMap) { - uploadSasCode += `\n%let _WEBIN_FILEREF${uploadedMap.count}=${uploadedMap.fileref};` + for (const uploadedFile of uploadedFiles) { + uploadSasCode += `\n%let _WEBIN_FILEREF${uploadedFile.count}=${uploadedFile.fileref};` } - for (let uploadedMap of uploadedFilesMap) { - uploadSasCode += `\n%let _WEBIN_NAME${uploadedMap.count}=${uploadedMap.filepath};` + for (const uploadedFile of uploadedFiles) { + uploadSasCode += `\n%let _WEBIN_NAME${uploadedFile.count}=${uploadedFile.fieldName};` } if (fileCount > 0) { diff --git a/restClient/stp.rest b/restClient/stp.rest index b19f958..7cc3c04 100644 --- a/restClient/stp.rest +++ b/restClient/stp.rest @@ -1,16 +1,14 @@ - - ### testing upload file example POST http://localhost:5000/SASjsApi/stp/execute/?_program=/Public/app/viya/services/editors/loadfile&table=DCCONFIG.MPE_X_TEST Content-Type: multipart/form-data; boundary=----WebKitFormBoundarynkYOqevUMKZrXeAy ------WebKitFormBoundarynkYOqevUMKZrXeAy -Content-Disposition: form-data; name="file"; filename="DCCONFIG.MPE_X_TEST.xlsx" +Content-Disposition: form-data; name="fileSome11"; filename="DCCONFIG.MPE_X_TEST.xlsx" Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet ------WebKitFormBoundarynkYOqevUMKZrXeAy -Content-Disposition: form-data; name="file"; filename="DCCONFIG.MPE_X_TEST.xlsx.csv" +Content-Disposition: form-data; name="fileSome22"; filename="DCCONFIG.MPE_X_TEST.xlsx.csv" Content-Type: application/csv _____DELETE__THIS__RECORD_____,PRIMARY_KEY_FIELD,SOME_CHAR,SOME_DROPDOWN,SOME_NUM,SOME_DATE,SOME_DATETIME,SOME_TIME,SOME_SHORTNUM,SOME_BESTNUM