mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
File upload fix
This commit is contained in:
parent
0da1dab95d
commit
8730318741
4 changed files with 30 additions and 14 deletions
|
@ -83,6 +83,11 @@ function GetApp(): ModuleType {
|
|||
],
|
||||
})
|
||||
)
|
||||
app.use(
|
||||
fileUpload({
|
||||
limits: { fileSize: 50 * 1024 * 1024 },
|
||||
})
|
||||
)
|
||||
// -------------------------------------------------------------------------------------------
|
||||
|
||||
let rootRedirectURL = ''
|
||||
|
@ -141,11 +146,6 @@ function GetApp(): ModuleType {
|
|||
logger.Log(`Using public dir: ${pdir}`)
|
||||
app.use(express.static(pdir))
|
||||
})
|
||||
app.use(
|
||||
fileUpload({
|
||||
limits: { fileSize: 50 * 1024 * 1024 },
|
||||
})
|
||||
)
|
||||
|
||||
// -------------------------------------------------------------------------------------------
|
||||
|
||||
|
|
|
@ -8,16 +8,22 @@ const uloadFiles = 'data/f'
|
|||
function setup(data: SubmoduleData): void {
|
||||
const { app /* userDB, url, publicdirs, moduleSpecificData */ } = data
|
||||
|
||||
app.post('/postfeedbackfile', function(req: Request, res: any) {
|
||||
utils.uploadFile(req, uloadFiles).then(() => {
|
||||
app.post('/postfeedbackfile', function (req: Request, res: any) {
|
||||
utils
|
||||
.uploadFile(req, uloadFiles)
|
||||
.then(() => {
|
||||
res.json({ success: true })
|
||||
})
|
||||
.catch(() => {
|
||||
res.json({ success: false, msg: 'error during uploading' })
|
||||
return
|
||||
})
|
||||
|
||||
logger.LogReq(req)
|
||||
logger.Log('New feedback file', logger.GetColor('bluebg'))
|
||||
})
|
||||
|
||||
app.post('/postfeedback', function(req: Request, res: any) {
|
||||
app.post('/postfeedback', function (req: Request, res: any) {
|
||||
logger.LogReq(req)
|
||||
if (req.body.fromLogin) {
|
||||
logger.Log(
|
||||
|
@ -51,7 +57,7 @@ function setup(data: SubmoduleData): void {
|
|||
res.json({ success: true })
|
||||
})
|
||||
|
||||
app.route('/fosuploader').post(function(req: Request, res: any) {
|
||||
app.route('/fosuploader').post(function (req: Request, res: any) {
|
||||
utils.uploadFile(req, uloadFiles).then(({ fileName }) => {
|
||||
res.redirect('/f/' + fileName)
|
||||
})
|
||||
|
|
|
@ -239,7 +239,7 @@ function setup(data: SubmoduleData): void {
|
|||
})
|
||||
})
|
||||
.catch(() => {
|
||||
res.end('something bad happened :s')
|
||||
res.json({ success: false, msg: 'something bad happened :s' })
|
||||
})
|
||||
})
|
||||
|
||||
|
|
|
@ -149,7 +149,7 @@ function WriteFile(content: string, path: string): void {
|
|||
|
||||
function writeFileAsync(content: string, path: string): void {
|
||||
CreatePath(path)
|
||||
fs.writeFile(path, content, function(err) {
|
||||
fs.writeFile(path, content, function (err) {
|
||||
if (err) {
|
||||
logger.Log(
|
||||
'Error writing file: ' + path + ' (sync)',
|
||||
|
@ -184,6 +184,13 @@ function deleteFile(fname: string): Boolean {
|
|||
function uploadFile(req: Request, path: string): Promise<any> {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
if (!req.files) {
|
||||
logger.Log(
|
||||
`Unable to upload file, req.files is undefined`,
|
||||
logger.GetColor('redbg')
|
||||
)
|
||||
return
|
||||
}
|
||||
const file = req.files.file
|
||||
// FIXME: Object.keys(req.files).forEach((file) => { saveFile() })
|
||||
logger.Log('Uploading: ' + file.name, logger.GetColor('blue'))
|
||||
|
@ -217,7 +224,10 @@ function uploadFile(req: Request, path: string): Promise<any> {
|
|||
}
|
||||
})
|
||||
} catch (err) {
|
||||
logger.Log(`Unable to upload file!`, logger.GetColor('redbg'))
|
||||
logger.Log(
|
||||
`Unable to upload file, error on stderr`,
|
||||
logger.GetColor('redbg')
|
||||
)
|
||||
console.error(err)
|
||||
reject(err)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue