1
0
mirror of https://github.com/sasjs/server.git synced 2026-01-07 06:30:06 +00:00

chore: comments addressing

This commit is contained in:
Mihajlo Medjedovic
2021-10-15 12:43:22 +00:00
parent 17a7a26fc3
commit c0a4e1aa14
12 changed files with 88 additions and 62 deletions

View File

@@ -60,7 +60,7 @@ ${program}`
}
}
let code = path.join(session.path, 'code.sas')
const code = path.join(session.path, 'code.sas')
if (!(await fileExists(code))) {
await createFile(code, program)
}
@@ -82,10 +82,6 @@ ${program}`
if (await fileExists(log)) log = await readFile(log)
else log = ''
// if (stderr) {
// return Promise.reject({ error: stderr, log: log })
// }
if (await fileExists(webout)) webout = await readFile(webout)
else webout = ''

View File

@@ -0,0 +1,36 @@
import { uuidv4 } from '@sasjs/utils'
import { getSessionController } from '.'
const multer = require('multer')
export class FileUploadController {
private storage = multer.diskStorage({
destination: function (req: any, file: any, cb: any) {
//Sending the intercepted files to the sessions subfolder
cb(null, req.sasSession.path)
},
filename: function (req: any, file: any, cb: any) {
//req_file prefix + unique hash added to sas request files
cb(null, `req_file_${uuidv4().replace(/-/gm, '')}`)
}
})
private upload = multer({ storage: this.storage })
//It will intercept request and generate uniqe uuid to be used as a subfolder name
//that will store the files uploaded
public preuploadMiddleware = async (req: any, res: any, next: any) => {
let session
const sessionController = getSessionController()
session = await sessionController.getSession()
session.inUse = true
req.sasSession = session
next()
}
public getMulterUploadObject() {
return this.upload
}
}

View File

@@ -70,7 +70,7 @@ export class SessionController {
this.scheduleSessionDestroy(session)
this.executionController.execute('', autoExec, session).catch((err) => {})
this.executionController.execute('', autoExec, session).catch(() => {})
this.sessions.push(session)

View File

@@ -1,3 +1,4 @@
export * from './deploy'
export * from './Session'
export * from './Execution'
export * from './FileUploadController'

View File

@@ -2,39 +2,12 @@ import express from 'express'
import { createFileTree, getSessionController, getTreeExample } from '../controllers'
import { ExecutionResult, isRequestQuery, isFileTree } from '../types'
import path from 'path'
import { getTmpFilesFolderPath, getTmpFolderPath, makeFilesNamesMap } from '../utils'
import { ExecutionController } from '../controllers'
import { uuidv4 } from '@sasjs/utils'
import { addExtensionIfNotFound, getTmpFilesFolderPath, getTmpFolderPath, makeFilesNamesMap } from '../utils'
import { ExecutionController, FileUploadController } from '../controllers'
const multer = require('multer')
const router = express.Router()
const storage = multer.diskStorage({
destination: function (req: any, file: any, cb: any) {
//Sending the intercepted files to the sessions subfolder
cb(null, req.sasSession.path)
},
filename: function (req: any, file: any, cb: any) {
//req_file prefix + unique hash added to sas request files
cb(null, `req_file_${uuidv4().replace(/-/gm, '')}`)
}
})
const upload = multer({ storage: storage })
//It will intercept request and generate uniqe uuid to be used as a subfolder name
//that will store the files uploaded
const preuploadMiddleware = async (req: any, res: any, next: any) => {
let session
const sessionController = getSessionController()
session = await sessionController.getSession()
session.inUse = true
req.sasSession = session
next()
}
const fileUploadController = new FileUploadController()
router.get('/', async (_, res) => {
res.status(200).send('Welcome to @sasjs/server API')
@@ -102,14 +75,14 @@ router.get('/SASjsExecutor/do', async (req, res) => {
}
})
router.post('/SASjsExecutor/do', preuploadMiddleware, upload.any(), async (req: any, res: any) => {
router.post('/SASjsExecutor/do', fileUploadController.preuploadMiddleware, fileUploadController.getMulterUploadObject().any(), async (req: any, res: any) => {
if (isRequestQuery(req.query)) {
let sasCodePath = path
.join(getTmpFilesFolderPath(), req.query._program)
.replace(new RegExp('/', 'g'), path.sep)
// If no extension provided, add .sas extension
sasCodePath += !sasCodePath.includes('.') ? '.sas' : ''
sasCodePath += addExtensionIfNotFound(sasCodePath, 'sas')
let filesNamesMap = null

10
src/types/Upload.ts Normal file
View File

@@ -0,0 +1,10 @@
export interface MulterFile {
fieldname: string
originalname: string
encoding: string
mimetype: string
destination: string
filename: string
path: string
size: number
}

View File

@@ -24,3 +24,7 @@ export const generateUniqueFileName = (fileName: string, extension = '') =>
new Date().getTime(),
extension
].join('')
export const addExtensionIfNotFound = (value: string, extension: string) => {
return !value.includes('.') ? `.${extension}` : ''
}

View File

@@ -1,16 +1,18 @@
import path from 'path'
import fs from 'fs'
import { getTmpSessionsFolderPath } from '.'
import { MulterFile } from '../types/Upload'
import { listFilesInFolder } from '@sasjs/utils'
/**
* It will create a 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
* @returns object
*/
export const makeFilesNamesMap = (files: any) => {
export const makeFilesNamesMap = (files: MulterFile[]) => {
if (!files) return null
const filesNamesMap: any = {}
const filesNamesMap: {[key: string]: string} = {}
for (let file of files) {
filesNamesMap[file.filename] = file.fieldname
@@ -20,7 +22,7 @@ export const makeFilesNamesMap = (files: any) => {
}
/**
* Generates the sas code that reference uploaded files in the concurrent request
* Generates the sas code that references uploaded files in the concurrent request
* @param filesNamesMap object that maps hashed file names and original file names
* @param sasUploadFolder name of the folder that is created for the purpose of files in concurrent request
* @returns generated sas code
@@ -29,8 +31,6 @@ export const makeFilesNamesMap = (files: any) => {
filesNamesMap: any,
sasSessionFolder: string
): string => {
const uploadFilesDirPath = sasSessionFolder
let uploadSasCode = ''
let fileCount = 0
let uploadedFilesMap: {
@@ -40,7 +40,8 @@ export const makeFilesNamesMap = (files: any) => {
count: number
}[] = []
fs.readdirSync(uploadFilesDirPath).forEach((fileName) => {
fs.readdirSync(sasSessionFolder).forEach((fileName) => {
let fileCountString = fileCount < 100 ? '0' + fileCount : fileCount
fileCountString = fileCount < 10 ? '00' + fileCount : fileCount
@@ -49,7 +50,7 @@ export const makeFilesNamesMap = (files: any) => {
uploadedFilesMap.push({
fileref: `_sjs${fileCountString}`,
filepath: `${uploadFilesDirPath}/${fileName}`,
filepath: `${sasSessionFolder}/${fileName}`,
filename: filesNamesMap[fileName],
count: fileCount
})