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

fix: improvement in flow of uploading

This commit is contained in:
Saad Jutt
2022-02-28 22:12:39 +05:00
parent 765969db11
commit 8c1941a87b
3 changed files with 16 additions and 6 deletions

View File

@@ -1,5 +1,7 @@
import express from 'express'
import multer, { multerSingle } from '../../middlewares/multer'
import { deleteFile } from '@sasjs/utils'
import { multerSingle } from '../../middlewares/multer'
import { DriveController } from '../../controllers/'
import { getFileDriveValidation, updateFileDriveValidation } from '../../utils'
@@ -41,7 +43,10 @@ driveRouter.post(
(...arg) => multerSingle('file', arg),
async (req, res) => {
const { error, value: body } = updateFileDriveValidation(req.body)
if (error) return res.status(400).send(error.details[0].message)
if (error) {
if (req.file) await deleteFile(req.file.path)
return res.status(400).send(error.details[0].message)
}
if (!req.file) return res.status(400).send('"file" is not present.')
@@ -49,6 +54,7 @@ driveRouter.post(
const response = await controller.saveFile(body.filePath, req.file)
res.send(response)
} catch (err: any) {
await deleteFile(req.file.path)
res.status(403).send(err.toString())
}
}