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

chore: merging with sessions - sas request with file upload

This commit is contained in:
Mihajlo Medjedovic
2021-10-14 15:44:28 +00:00
parent 3f39cab357
commit 17a7a26fc3
8 changed files with 355 additions and 27 deletions

View File

@@ -1,12 +1,41 @@
import express from 'express'
import { createFileTree, getTreeExample } from '../controllers'
import { createFileTree, getSessionController, getTreeExample } from '../controllers'
import { ExecutionResult, isRequestQuery, isFileTree } from '../types'
import path from 'path'
import { getTmpFilesFolderPath } from '../utils'
import { getTmpFilesFolderPath, getTmpFolderPath, makeFilesNamesMap } from '../utils'
import { ExecutionController } from '../controllers'
import { uuidv4 } from '@sasjs/utils'
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()
}
router.get('/', async (_, res) => {
res.status(200).send('Welcome to @sasjs/server API')
})
@@ -46,9 +75,12 @@ router.get('/SASjsExecutor', async (req, res) => {
router.get('/SASjsExecutor/do', async (req, res) => {
if (isRequestQuery(req.query)) {
const sasCodePath = path
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' : ''
await new ExecutionController()
.execute(sasCodePath, undefined, undefined, { ...req.query })
@@ -70,4 +102,39 @@ router.get('/SASjsExecutor/do', async (req, res) => {
}
})
router.post('/SASjsExecutor/do', preuploadMiddleware, upload.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' : ''
let filesNamesMap = null
if (req.files && req.files.length > 0) {
filesNamesMap = makeFilesNamesMap(req.files)
}
await new ExecutionController()
.execute(sasCodePath, undefined, req.sasSession, { ...req.query }, { filesNamesMap: filesNamesMap })
.then((result: {}) => {
res.status(200).send(result)
})
.catch((err: {} | string) => {
res.status(400).send({
status: 'failure',
message: 'Job execution failed.',
...(typeof err === 'object' ? err : { details: err })
})
})
} else {
res.status(400).send({
status: 'failure',
message: `Please provide the location of SAS code`
})
}
})
export default router