mirror of
https://github.com/sasjs/server.git
synced 2026-01-07 06:30:06 +00:00
fix(webin): closes #99
This commit is contained in:
@@ -1,9 +1,21 @@
|
|||||||
import path from 'path'
|
|
||||||
import fs from 'fs'
|
|
||||||
import { getTmpSessionsFolderPath } from '.'
|
|
||||||
import { MulterFile } from '../types/Upload'
|
import { MulterFile } from '../types/Upload'
|
||||||
import { listFilesInFolder } from '@sasjs/utils'
|
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
|
* It will create an object that maps hashed file names to the original names
|
||||||
* @param files array of files to be mapped
|
* @param files array of files to be mapped
|
||||||
@@ -12,10 +24,13 @@ import { listFilesInFolder } from '@sasjs/utils'
|
|||||||
export const makeFilesNamesMap = (files: MulterFile[]) => {
|
export const makeFilesNamesMap = (files: MulterFile[]) => {
|
||||||
if (!files) return null
|
if (!files) return null
|
||||||
|
|
||||||
const filesNamesMap: { [key: string]: string } = {}
|
const filesNamesMap: FilenamesMap = {}
|
||||||
|
|
||||||
for (let file of files) {
|
for (let file of files) {
|
||||||
filesNamesMap[file.filename] = file.originalname
|
filesNamesMap[file.filename] = {
|
||||||
|
fieldName: file.fieldname,
|
||||||
|
originalName: file.originalname
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return filesNamesMap
|
return filesNamesMap
|
||||||
@@ -28,17 +43,12 @@ export const makeFilesNamesMap = (files: MulterFile[]) => {
|
|||||||
* @returns generated sas code
|
* @returns generated sas code
|
||||||
*/
|
*/
|
||||||
export const generateFileUploadSasCode = async (
|
export const generateFileUploadSasCode = async (
|
||||||
filesNamesMap: any,
|
filesNamesMap: FilenamesMap,
|
||||||
sasSessionFolder: string
|
sasSessionFolder: string
|
||||||
): Promise<string> => {
|
): Promise<string> => {
|
||||||
let uploadSasCode = ''
|
let uploadSasCode = ''
|
||||||
let fileCount = 0
|
let fileCount = 0
|
||||||
let uploadedFilesMap: {
|
const uploadedFiles: UploadedFiles[] = []
|
||||||
fileref: string
|
|
||||||
filepath: string
|
|
||||||
filename: string
|
|
||||||
count: number
|
|
||||||
}[] = []
|
|
||||||
|
|
||||||
const sasSessionFolderList: string[] = await listFilesInFolder(
|
const sasSessionFolderList: string[] = await listFilesInFolder(
|
||||||
sasSessionFolder
|
sasSessionFolder
|
||||||
@@ -50,31 +60,32 @@ export const generateFileUploadSasCode = async (
|
|||||||
if (fileName.includes('req_file')) {
|
if (fileName.includes('req_file')) {
|
||||||
fileCount++
|
fileCount++
|
||||||
|
|
||||||
uploadedFilesMap.push({
|
uploadedFiles.push({
|
||||||
fileref: `_sjs${fileCountString}`,
|
fileref: `_sjs${fileCountString}`,
|
||||||
filepath: `${sasSessionFolder}/${fileName}`,
|
filepath: `${sasSessionFolder}/${fileName}`,
|
||||||
filename: filesNamesMap[fileName],
|
originalName: filesNamesMap[fileName].originalName,
|
||||||
|
fieldName: filesNamesMap[fileName].fieldName,
|
||||||
count: fileCount
|
count: fileCount
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
for (let uploadedMap of uploadedFilesMap) {
|
for (const uploadedFile of uploadedFiles) {
|
||||||
uploadSasCode += `\nfilename ${uploadedMap.fileref} "${uploadedMap.filepath}";`
|
uploadSasCode += `\nfilename ${uploadedFile.fileref} "${uploadedFile.filepath}";`
|
||||||
}
|
}
|
||||||
|
|
||||||
uploadSasCode += `\n%let _WEBIN_FILE_COUNT=${fileCount};`
|
uploadSasCode += `\n%let _WEBIN_FILE_COUNT=${fileCount};`
|
||||||
|
|
||||||
for (let uploadedMap of uploadedFilesMap) {
|
for (const uploadedFile of uploadedFiles) {
|
||||||
uploadSasCode += `\n%let _WEBIN_FILENAME${uploadedMap.count}=${uploadedMap.filename};`
|
uploadSasCode += `\n%let _WEBIN_FILENAME${uploadedFile.count}=${uploadedFile.originalName};`
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let uploadedMap of uploadedFilesMap) {
|
for (const uploadedFile of uploadedFiles) {
|
||||||
uploadSasCode += `\n%let _WEBIN_FILEREF${uploadedMap.count}=${uploadedMap.fileref};`
|
uploadSasCode += `\n%let _WEBIN_FILEREF${uploadedFile.count}=${uploadedFile.fileref};`
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let uploadedMap of uploadedFilesMap) {
|
for (const uploadedFile of uploadedFiles) {
|
||||||
uploadSasCode += `\n%let _WEBIN_NAME${uploadedMap.count}=${uploadedMap.filepath};`
|
uploadSasCode += `\n%let _WEBIN_NAME${uploadedFile.count}=${uploadedFile.fieldName};`
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fileCount > 0) {
|
if (fileCount > 0) {
|
||||||
|
|||||||
@@ -1,16 +1,14 @@
|
|||||||
|
|
||||||
|
|
||||||
### testing upload file example
|
### testing upload file example
|
||||||
POST http://localhost:5000/SASjsApi/stp/execute/?_program=/Public/app/viya/services/editors/loadfile&table=DCCONFIG.MPE_X_TEST
|
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
|
Content-Type: multipart/form-data; boundary=----WebKitFormBoundarynkYOqevUMKZrXeAy
|
||||||
|
|
||||||
------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
|
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
|
||||||
|
|
||||||
|
|
||||||
------WebKitFormBoundarynkYOqevUMKZrXeAy
|
------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
|
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
|
_____DELETE__THIS__RECORD_____,PRIMARY_KEY_FIELD,SOME_CHAR,SOME_DROPDOWN,SOME_NUM,SOME_DATE,SOME_DATETIME,SOME_TIME,SOME_SHORTNUM,SOME_BESTNUM
|
||||||
|
|||||||
Reference in New Issue
Block a user