mirror of
https://github.com/sasjs/server.git
synced 2026-01-12 08:40:04 +00:00
fix: improvement in flow of uploading
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import path from 'path'
|
import path from 'path'
|
||||||
import express from 'express'
|
import express, { ErrorRequestHandler } from 'express'
|
||||||
import morgan from 'morgan'
|
import morgan from 'morgan'
|
||||||
import dotenv from 'dotenv'
|
import dotenv from 'dotenv'
|
||||||
import cors from 'cors'
|
import cors from 'cors'
|
||||||
@@ -26,12 +26,19 @@ app.use(morgan('tiny'))
|
|||||||
app.use(express.static(path.join(__dirname, '../public')))
|
app.use(express.static(path.join(__dirname, '../public')))
|
||||||
app.use(express.static(getWebBuildFolderPath()))
|
app.use(express.static(getWebBuildFolderPath()))
|
||||||
|
|
||||||
|
const onError: ErrorRequestHandler = (err, req, res, next) => {
|
||||||
|
console.error(err.stack)
|
||||||
|
res.status(500).send('Something broke!')
|
||||||
|
}
|
||||||
|
|
||||||
export default setProcessVariables().then(async () => {
|
export default setProcessVariables().then(async () => {
|
||||||
// loading these modules after setting up variables due to
|
// loading these modules after setting up variables due to
|
||||||
// multer's usage of process var process.driveLoc
|
// multer's usage of process var process.driveLoc
|
||||||
const { setupRoutes } = await import('./routes/setupRoutes')
|
const { setupRoutes } = await import('./routes/setupRoutes')
|
||||||
setupRoutes(app)
|
setupRoutes(app)
|
||||||
|
|
||||||
|
app.use(onError)
|
||||||
|
|
||||||
await connectDB()
|
await connectDB()
|
||||||
return app
|
return app
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -214,18 +214,15 @@ const saveFile = async (
|
|||||||
.replace(new RegExp('/', 'g'), path.sep)
|
.replace(new RegExp('/', 'g'), path.sep)
|
||||||
|
|
||||||
if (!filePathFull.includes(driveFilesPath)) {
|
if (!filePathFull.includes(driveFilesPath)) {
|
||||||
await deleteFile(multerFile.path)
|
|
||||||
throw new Error('Cannot put file outside drive.')
|
throw new Error('Cannot put file outside drive.')
|
||||||
}
|
}
|
||||||
|
|
||||||
if (await fileExists(filePathFull)) {
|
if (await fileExists(filePathFull)) {
|
||||||
await deleteFile(multerFile.path)
|
|
||||||
throw new Error('File already exists.')
|
throw new Error('File already exists.')
|
||||||
}
|
}
|
||||||
|
|
||||||
const folderPath = path.dirname(filePathFull)
|
const folderPath = path.dirname(filePathFull)
|
||||||
await createFolder(folderPath)
|
await createFolder(folderPath)
|
||||||
|
|
||||||
await moveFile(multerFile.path, filePathFull)
|
await moveFile(multerFile.path, filePathFull)
|
||||||
|
|
||||||
return { status: 'success' }
|
return { status: 'success' }
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import express from 'express'
|
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 { DriveController } from '../../controllers/'
|
||||||
import { getFileDriveValidation, updateFileDriveValidation } from '../../utils'
|
import { getFileDriveValidation, updateFileDriveValidation } from '../../utils'
|
||||||
|
|
||||||
@@ -41,7 +43,10 @@ driveRouter.post(
|
|||||||
(...arg) => multerSingle('file', arg),
|
(...arg) => multerSingle('file', arg),
|
||||||
async (req, res) => {
|
async (req, res) => {
|
||||||
const { error, value: body } = updateFileDriveValidation(req.body)
|
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.')
|
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)
|
const response = await controller.saveFile(body.filePath, req.file)
|
||||||
res.send(response)
|
res.send(response)
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
|
await deleteFile(req.file.path)
|
||||||
res.status(403).send(err.toString())
|
res.status(403).send(err.toString())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user