1
0
mirror of https://github.com/sasjs/server.git synced 2026-01-09 07:20:05 +00:00

fix(upload): added query param as well for filepath

This commit is contained in:
Saad Jutt
2022-02-28 22:34:18 +05:00
parent 8c1941a87b
commit feeec4eb14
5 changed files with 56 additions and 13 deletions

View File

@@ -3,7 +3,12 @@ import { deleteFile } from '@sasjs/utils'
import { multerSingle } from '../../middlewares/multer'
import { DriveController } from '../../controllers/'
import { getFileDriveValidation, updateFileDriveValidation } from '../../utils'
import {
getFileDriveValidation,
updateFileDriveValidation,
uploadFileBodyValidation,
uploadFileParamValidation
} from '../../utils'
const controller = new DriveController()
@@ -42,16 +47,22 @@ driveRouter.post(
'/file',
(...arg) => multerSingle('file', arg),
async (req, res) => {
const { error, value: body } = updateFileDriveValidation(req.body)
if (error) {
const { error: errQ, value: query } = uploadFileParamValidation(req.query)
const { error: errB, value: body } = uploadFileBodyValidation(req.body)
if (errQ && errB) {
if (req.file) await deleteFile(req.file.path)
return res.status(400).send(error.details[0].message)
return res.status(400).send(errB.details[0].message)
}
if (!req.file) return res.status(400).send('"file" is not present.')
try {
const response = await controller.saveFile(body.filePath, req.file)
const response = await controller.saveFile(
req.file,
query._filePath,
body.filePath
)
res.send(response)
} catch (err: any) {
await deleteFile(req.file.path)

View File

@@ -172,7 +172,7 @@ describe('files', () => {
describe('file', () => {
describe('create', () => {
it('should create a SAS file on drive', async () => {
it('should create a SAS file on drive having filePath as form field', async () => {
const res = await request(app)
.post('/SASjsApi/drive/file')
.auth(accessToken, { type: 'bearer' })
@@ -185,6 +185,19 @@ describe('files', () => {
})
})
it('should create a SAS file on drive having _filePath as query param', async () => {
const res = await request(app)
.post('/SASjsApi/drive/file')
.auth(accessToken, { type: 'bearer' })
.query({ _filePath: '/my/path/code1.sas' })
.attach('file', path.join(__dirname, 'files', 'sample.sas'))
expect(res.statusCode).toEqual(200)
expect(res.body).toEqual({
status: 'success'
})
})
it('should respond with Unauthorized if access token is not present', async () => {
const res = await request(app)
.post('/SASjsApi/drive/file')