mirror of
https://github.com/sasjs/server.git
synced 2025-12-10 11:24:35 +00:00
style: lint
This commit is contained in:
@@ -3,34 +3,34 @@ 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()
|
||||
}
|
||||
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, '')}`)
|
||||
}
|
||||
})
|
||||
|
||||
public getMulterUploadObject() {
|
||||
return this.upload
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
export * from './deploy'
|
||||
export * from './Session'
|
||||
export * from './Execution'
|
||||
export * from './FileUploadController'
|
||||
export * from './FileUploadController'
|
||||
|
||||
@@ -85,39 +85,50 @@ router.get('/SASjsExecutor/do', async (req, res) => {
|
||||
}
|
||||
})
|
||||
|
||||
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)
|
||||
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 += addExtensionIfNotFound(sasCodePath, 'sas')
|
||||
// If no extension provided, add .sas extension
|
||||
sasCodePath += addExtensionIfNotFound(sasCodePath, 'sas')
|
||||
|
||||
let filesNamesMap = null
|
||||
let filesNamesMap = null
|
||||
|
||||
if (req.files && req.files.length > 0) {
|
||||
filesNamesMap = makeFilesNamesMap(req.files)
|
||||
}
|
||||
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 })
|
||||
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`
|
||||
})
|
||||
} else {
|
||||
res.status(400).send({
|
||||
status: 'failure',
|
||||
message: `Please provide the location of SAS code`
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
export default router
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
export interface MulterFile {
|
||||
fieldname: string
|
||||
originalname: string
|
||||
encoding: string
|
||||
mimetype: string
|
||||
destination: string
|
||||
filename: string
|
||||
path: string
|
||||
size: number
|
||||
}
|
||||
fieldname: string
|
||||
originalname: string
|
||||
encoding: string
|
||||
mimetype: string
|
||||
destination: string
|
||||
filename: string
|
||||
path: string
|
||||
size: number
|
||||
}
|
||||
|
||||
@@ -27,4 +27,4 @@ export const generateUniqueFileName = (fileName: string, extension = '') =>
|
||||
|
||||
export const addExtensionIfNotFound = (value: string, extension: string) => {
|
||||
return !value.includes('.') ? `.${extension}` : ''
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,9 +10,9 @@ import { listFilesInFolder } from '@sasjs/utils'
|
||||
* @returns object
|
||||
*/
|
||||
export const makeFilesNamesMap = (files: MulterFile[]) => {
|
||||
if (!files) return null
|
||||
if (!files) return null
|
||||
|
||||
const filesNamesMap: {[key: string]: string} = {}
|
||||
const filesNamesMap: { [key: string]: string } = {}
|
||||
|
||||
for (let file of files) {
|
||||
filesNamesMap[file.filename] = file.fieldname
|
||||
@@ -40,7 +40,6 @@ export const generateFileUploadSasCode = (
|
||||
count: number
|
||||
}[] = []
|
||||
|
||||
|
||||
fs.readdirSync(sasSessionFolder).forEach((fileName) => {
|
||||
let fileCountString = fileCount < 100 ? '0' + fileCount : fileCount
|
||||
fileCountString = fileCount < 10 ? '00' + fileCount : fileCount
|
||||
|
||||
Reference in New Issue
Block a user