Skip to content

Commit 0147bcb

Browse files
committed
fix(webin): closes #99
1 parent bf53ad3 commit 0147bcb

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

api/src/utils/upload.ts

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1-
import path from 'path'
2-
import fs from 'fs'
3-
import { getTmpSessionsFolderPath } from '.'
41
import { MulterFile } from '../types/Upload'
52
import { listFilesInFolder } from '@sasjs/utils'
63

4+
interface FilenameMapSingle {
5+
fieldName: string
6+
originalName: string
7+
}
8+
9+
interface FilenamesMap {
10+
[key: string]: FilenameMapSingle
11+
}
12+
13+
interface UploadedFiles extends FilenameMapSingle {
14+
fileref: string
15+
filepath: string
16+
count: number
17+
}
18+
719
/**
820
* It will create an object that maps hashed file names to the original names
921
* @param files array of files to be mapped
@@ -12,10 +24,13 @@ import { listFilesInFolder } from '@sasjs/utils'
1224
export const makeFilesNamesMap = (files: MulterFile[]) => {
1325
if (!files) return null
1426

15-
const filesNamesMap: { [key: string]: string } = {}
27+
const filesNamesMap: FilenamesMap = {}
1628

1729
for (let file of files) {
18-
filesNamesMap[file.filename] = file.originalname
30+
filesNamesMap[file.filename] = {
31+
fieldName: file.fieldname,
32+
originalName: file.originalname
33+
}
1934
}
2035

2136
return filesNamesMap
@@ -28,17 +43,12 @@ export const makeFilesNamesMap = (files: MulterFile[]) => {
2843
* @returns generated sas code
2944
*/
3045
export const generateFileUploadSasCode = async (
31-
filesNamesMap: any,
46+
filesNamesMap: FilenamesMap,
3247
sasSessionFolder: string
3348
): Promise<string> => {
3449
let uploadSasCode = ''
3550
let fileCount = 0
36-
let uploadedFilesMap: {
37-
fileref: string
38-
filepath: string
39-
filename: string
40-
count: number
41-
}[] = []
51+
const uploadedFiles: UploadedFiles[] = []
4252

4353
const sasSessionFolderList: string[] = await listFilesInFolder(
4454
sasSessionFolder
@@ -50,31 +60,32 @@ export const generateFileUploadSasCode = async (
5060
if (fileName.includes('req_file')) {
5161
fileCount++
5262

53-
uploadedFilesMap.push({
63+
uploadedFiles.push({
5464
fileref: `_sjs${fileCountString}`,
5565
filepath: `${sasSessionFolder}/${fileName}`,
56-
filename: filesNamesMap[fileName],
66+
originalName: filesNamesMap[fileName].originalName,
67+
fieldName: filesNamesMap[fileName].fieldName,
5768
count: fileCount
5869
})
5970
}
6071
})
6172

62-
for (let uploadedMap of uploadedFilesMap) {
63-
uploadSasCode += `\nfilename ${uploadedMap.fileref} "${uploadedMap.filepath}";`
73+
for (const uploadedFile of uploadedFiles) {
74+
uploadSasCode += `\nfilename ${uploadedFile.fileref} "${uploadedFile.filepath}";`
6475
}
6576

6677
uploadSasCode += `\n%let _WEBIN_FILE_COUNT=${fileCount};`
6778

68-
for (let uploadedMap of uploadedFilesMap) {
69-
uploadSasCode += `\n%let _WEBIN_FILENAME${uploadedMap.count}=${uploadedMap.filename};`
79+
for (const uploadedFile of uploadedFiles) {
80+
uploadSasCode += `\n%let _WEBIN_FILENAME${uploadedFile.count}=${uploadedFile.originalName};`
7081
}
7182

72-
for (let uploadedMap of uploadedFilesMap) {
73-
uploadSasCode += `\n%let _WEBIN_FILEREF${uploadedMap.count}=${uploadedMap.fileref};`
83+
for (const uploadedFile of uploadedFiles) {
84+
uploadSasCode += `\n%let _WEBIN_FILEREF${uploadedFile.count}=${uploadedFile.fileref};`
7485
}
7586

76-
for (let uploadedMap of uploadedFilesMap) {
77-
uploadSasCode += `\n%let _WEBIN_NAME${uploadedMap.count}=${uploadedMap.filepath};`
87+
for (const uploadedFile of uploadedFiles) {
88+
uploadSasCode += `\n%let _WEBIN_NAME${uploadedFile.count}=${uploadedFile.fieldName};`
7889
}
7990

8091
if (fileCount > 0) {

restClient/stp.rest

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
2-
31
### testing upload file example
42
POST http://localhost:5000/SASjsApi/stp/execute/?_program=/Public/app/viya/services/editors/loadfile&table=DCCONFIG.MPE_X_TEST
53
Content-Type: multipart/form-data; boundary=----WebKitFormBoundarynkYOqevUMKZrXeAy
64

75
------WebKitFormBoundarynkYOqevUMKZrXeAy
8-
Content-Disposition: form-data; name="file"; filename="DCCONFIG.MPE_X_TEST.xlsx"
6+
Content-Disposition: form-data; name="fileSome11"; filename="DCCONFIG.MPE_X_TEST.xlsx"
97
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
108

119

1210
------WebKitFormBoundarynkYOqevUMKZrXeAy
13-
Content-Disposition: form-data; name="file"; filename="DCCONFIG.MPE_X_TEST.xlsx.csv"
11+
Content-Disposition: form-data; name="fileSome22"; filename="DCCONFIG.MPE_X_TEST.xlsx.csv"
1412
Content-Type: application/csv
1513

1614
_____DELETE__THIS__RECORD_____,PRIMARY_KEY_FIELD,SOME_CHAR,SOME_DROPDOWN,SOME_NUM,SOME_DATE,SOME_DATETIME,SOME_TIME,SOME_SHORTNUM,SOME_BESTNUM

0 commit comments

Comments
 (0)