1
0
mirror of https://github.com/sasjs/server.git synced 2025-12-11 19:44:35 +00:00

fix: refactor code for session selection in preUploadMiddleware function

This commit is contained in:
2022-06-06 15:19:39 +05:00
parent e5a7674fa1
commit b4443819d4

View File

@@ -1,9 +1,9 @@
import path from 'path' import path from 'path'
import { Request, RequestHandler } from 'express' import { Request, RequestHandler } from 'express'
import multer from 'multer' import multer from 'multer'
import { uuidv4, fileExists, readFile } from '@sasjs/utils' import { uuidv4, fileExists } from '@sasjs/utils'
import { getSASSessionController, getJSSessionController } from '.' import { getSASSessionController, getJSSessionController } from '.'
import { getFilesFolder } from '../../utils' import { getFilesFolder, SASJSRunTimes } from '../../utils'
export class FileUploadController { export class FileUploadController {
private storage = multer.diskStorage({ private storage = multer.diskStorage({
@@ -22,22 +22,22 @@ export class FileUploadController {
//It will intercept request and generate unique uuid to be used as a subfolder name //It will intercept request and generate unique uuid to be used as a subfolder name
//that will store the files uploaded //that will store the files uploaded
public preUploadMiddleware: RequestHandler = async (req, res, next) => { public preUploadMiddleware: RequestHandler = async (req, res, next) => {
if (process.runTimes.length === 0) {
throw 'No runtime is specified in environment variables.'
}
const programPath = req.query._program as string const programPath = req.query._program as string
for (const runTime of process.runTimes) { for (const runTime of process.runTimes) {
const codePath = const codePath =
path path
.join(getFilesFolder(), programPath) .join(getFilesFolder(), programPath)
.replace(new RegExp('/', 'g'), path.sep) + runTime .replace(new RegExp('/', 'g'), path.sep) +
'.' +
runTime
if (await fileExists(programPath)) { if (await fileExists(codePath)) {
const program = await readFile(codePath) if (runTime === SASJSRunTimes.JS) {
const sessionController = getJSSessionController()
if (runTime === 'sas') { const session = await sessionController.getSession()
req.sasjsSession = session
} else {
const sessionController = getSASSessionController() const sessionController = getSASSessionController()
const session = await sessionController.getSession() const session = await sessionController.getSession()
// marking consumed true, so that it's not available // marking consumed true, so that it's not available
@@ -45,12 +45,6 @@ export class FileUploadController {
session.consumed = true session.consumed = true
req.sasjsSession = session req.sasjsSession = session
} else if (runTime === 'js') {
const sessionController = getJSSessionController()
const session = await sessionController.getSession()
req.sasjsSession = session
} else {
throw `${runTime} runtime is not implemented yet.`
} }
} }
} }