mirror of
https://github.com/sasjs/server.git
synced 2026-01-03 21:10:05 +00:00
feat: conver single session controller to two controller i.e. SASSessionController and JSSessionController
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
import path from 'path'
|
||||
import { Request, RequestHandler } from 'express'
|
||||
import multer from 'multer'
|
||||
import { uuidv4 } from '@sasjs/utils'
|
||||
import { getSessionController } from '.'
|
||||
import { uuidv4, fileExists, readFile } from '@sasjs/utils'
|
||||
import { getSASSessionController, getJSSessionController } from '.'
|
||||
import { getFilesFolder } from '../../utils'
|
||||
|
||||
export class FileUploadController {
|
||||
private storage = multer.diskStorage({
|
||||
destination: function (req: Request, file: any, cb: any) {
|
||||
//Sending the intercepted files to the sessions subfolder
|
||||
cb(null, req.sasSession?.path)
|
||||
cb(null, req.sasjsSession?.path)
|
||||
},
|
||||
filename: function (req: Request, file: any, cb: any) {
|
||||
//req_file prefix + unique hash added to sas request files
|
||||
@@ -20,15 +22,38 @@ export class FileUploadController {
|
||||
//It will intercept request and generate unique uuid to be used as a subfolder name
|
||||
//that will store the files uploaded
|
||||
public preUploadMiddleware: RequestHandler = async (req, res, next) => {
|
||||
let session
|
||||
if (process.runTimes.length === 0) {
|
||||
throw 'No runtime is specified in environment variables.'
|
||||
}
|
||||
|
||||
const sessionController = getSessionController()
|
||||
session = await sessionController.getSession()
|
||||
// marking consumed true, so that it's not available
|
||||
// as readySession for any other request
|
||||
session.consumed = true
|
||||
const programPath = req.query._program as string
|
||||
|
||||
req.sasSession = session
|
||||
for (const runTime of process.runTimes) {
|
||||
const codePath =
|
||||
path
|
||||
.join(getFilesFolder(), programPath)
|
||||
.replace(new RegExp('/', 'g'), path.sep) + runTime
|
||||
|
||||
if (await fileExists(programPath)) {
|
||||
const program = await readFile(codePath)
|
||||
|
||||
if (runTime === 'sas') {
|
||||
const sessionController = getSASSessionController()
|
||||
const session = await sessionController.getSession()
|
||||
// marking consumed true, so that it's not available
|
||||
// as readySession for any other request
|
||||
session.consumed = true
|
||||
|
||||
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.`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
next()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user