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 = ''
|
let rootRedirectURL = ''
|
||||||
|
@ -141,11 +146,6 @@ function GetApp(): ModuleType {
|
||||||
logger.Log(`Using public dir: ${pdir}`)
|
logger.Log(`Using public dir: ${pdir}`)
|
||||||
app.use(express.static(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 {
|
function setup(data: SubmoduleData): void {
|
||||||
const { app /* userDB, url, publicdirs, moduleSpecificData */ } = data
|
const { app /* userDB, url, publicdirs, moduleSpecificData */ } = data
|
||||||
|
|
||||||
app.post('/postfeedbackfile', function(req: Request, res: any) {
|
app.post('/postfeedbackfile', function (req: Request, res: any) {
|
||||||
utils.uploadFile(req, uloadFiles).then(() => {
|
utils
|
||||||
|
.uploadFile(req, uloadFiles)
|
||||||
|
.then(() => {
|
||||||
res.json({ success: true })
|
res.json({ success: true })
|
||||||
})
|
})
|
||||||
|
.catch(() => {
|
||||||
|
res.json({ success: false, msg: 'error during uploading' })
|
||||||
|
return
|
||||||
|
})
|
||||||
|
|
||||||
logger.LogReq(req)
|
logger.LogReq(req)
|
||||||
logger.Log('New feedback file', logger.GetColor('bluebg'))
|
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)
|
logger.LogReq(req)
|
||||||
if (req.body.fromLogin) {
|
if (req.body.fromLogin) {
|
||||||
logger.Log(
|
logger.Log(
|
||||||
|
@ -51,7 +57,7 @@ function setup(data: SubmoduleData): void {
|
||||||
res.json({ success: true })
|
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 }) => {
|
utils.uploadFile(req, uloadFiles).then(({ fileName }) => {
|
||||||
res.redirect('/f/' + fileName)
|
res.redirect('/f/' + fileName)
|
||||||
})
|
})
|
||||||
|
|
|
@ -239,7 +239,7 @@ function setup(data: SubmoduleData): void {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.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 {
|
function writeFileAsync(content: string, path: string): void {
|
||||||
CreatePath(path)
|
CreatePath(path)
|
||||||
fs.writeFile(path, content, function(err) {
|
fs.writeFile(path, content, function (err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
logger.Log(
|
logger.Log(
|
||||||
'Error writing file: ' + path + ' (sync)',
|
'Error writing file: ' + path + ' (sync)',
|
||||||
|
@ -184,6 +184,13 @@ function deleteFile(fname: string): Boolean {
|
||||||
function uploadFile(req: Request, path: string): Promise<any> {
|
function uploadFile(req: Request, path: string): Promise<any> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
|
if (!req.files) {
|
||||||
|
logger.Log(
|
||||||
|
`Unable to upload file, req.files is undefined`,
|
||||||
|
logger.GetColor('redbg')
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
const file = req.files.file
|
const file = req.files.file
|
||||||
// FIXME: Object.keys(req.files).forEach((file) => { saveFile() })
|
// FIXME: Object.keys(req.files).forEach((file) => { saveFile() })
|
||||||
logger.Log('Uploading: ' + file.name, logger.GetColor('blue'))
|
logger.Log('Uploading: ' + file.name, logger.GetColor('blue'))
|
||||||
|
@ -217,7 +224,10 @@ function uploadFile(req: Request, path: string): Promise<any> {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
} catch (err) {
|
} 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)
|
console.error(err)
|
||||||
reject(err)
|
reject(err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue