From 38ab27c1ed9cf3f87d76773e78203357acf156c2 Mon Sep 17 00:00:00 2001 From: Mihajlo Medjedovic Date: Fri, 15 Oct 2021 12:57:26 +0000 Subject: [PATCH] style: lint --- src/controllers/FileUploadController.ts | 60 +++++++++++------------ src/controllers/index.ts | 2 +- src/routes/index.ts | 65 +++++++++++++++---------- src/types/Upload.ts | 18 +++---- src/utils/file.ts | 2 +- src/utils/upload.ts | 5 +- 6 files changed, 81 insertions(+), 71 deletions(-) diff --git a/src/controllers/FileUploadController.ts b/src/controllers/FileUploadController.ts index 0bd015c..3a55fce 100644 --- a/src/controllers/FileUploadController.ts +++ b/src/controllers/FileUploadController.ts @@ -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 - } -} \ No newline at end of file + 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 + } +} diff --git a/src/controllers/index.ts b/src/controllers/index.ts index 60c5d59..86939a2 100644 --- a/src/controllers/index.ts +++ b/src/controllers/index.ts @@ -1,4 +1,4 @@ export * from './deploy' export * from './Session' export * from './Execution' -export * from './FileUploadController' \ No newline at end of file +export * from './FileUploadController' diff --git a/src/routes/index.ts b/src/routes/index.ts index 1ed3d4b..7a9302a 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -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 diff --git a/src/types/Upload.ts b/src/types/Upload.ts index d025a4e..74628f9 100644 --- a/src/types/Upload.ts +++ b/src/types/Upload.ts @@ -1,10 +1,10 @@ export interface MulterFile { - fieldname: string - originalname: string - encoding: string - mimetype: string - destination: string - filename: string - path: string - size: number -} \ No newline at end of file + fieldname: string + originalname: string + encoding: string + mimetype: string + destination: string + filename: string + path: string + size: number +} diff --git a/src/utils/file.ts b/src/utils/file.ts index 3dfe57f..cb29f48 100644 --- a/src/utils/file.ts +++ b/src/utils/file.ts @@ -27,4 +27,4 @@ export const generateUniqueFileName = (fileName: string, extension = '') => export const addExtensionIfNotFound = (value: string, extension: string) => { return !value.includes('.') ? `.${extension}` : '' -} \ No newline at end of file +} diff --git a/src/utils/upload.ts b/src/utils/upload.ts index 2c685f4..e0ce494 100644 --- a/src/utils/upload.ts +++ b/src/utils/upload.ts @@ -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