From 0c428a2b3320d4be49d2d076cf58f98331ad5e41 Mon Sep 17 00:00:00 2001 From: Christopher Rose Date: Mon, 27 May 2019 14:21:51 +0530 Subject: [PATCH] Server side - Implement uploading --- index.js | 60 ++++++++++++++++++++++++++++++++++++++++----------- processing.js | 6 ++++++ 2 files changed, 54 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index d51932a..0749dae 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,10 @@ + const express = require('express') const bodyParser = require('body-parser') const fs = require('fs') const path = require('path') const processing = require('./processing') +const busboy = require('connect-busboy') // Import settings let settings = JSON.parse(fs.readFileSync("settings.json")) @@ -10,10 +12,7 @@ let settings = JSON.parse(fs.readFileSync("settings.json")) app = express() app.use(bodyParser.urlencoded({extended:false})) app.use(bodyParser.json()) - -// Check if a given directory is within the main defined directory or not -let inDir = (dircheck,dirmain) => !path.relative(path.normalize(dircheck), dirmain).startsWith('..') - +app.use(busboy()) // Download file //loc @@ -22,7 +21,7 @@ app.get('/files/cat',(req,res,next)=>{ const location = processing.mergedir(req.query.loc,settings) //const nloc = path.normalize(req.body.loc); const nloc = path.normalize(path.relative(settings.dirname,location)) - if(inDir(settings.dirname,location)){ + if(processing.inDir(settings.dirname,location)){ res.download(location,err=>{if(err) next(err)} ) } }) @@ -36,7 +35,7 @@ app.post('/files/ls',(req,res,next)=>{ const nloc = path.normalize(path.relative(settings.dirname,location)) //console.log([loc,nloc]) //Make sure not escaping the given path; insecure - if(inDir(settings.dirname,location)){ + if(processing.inDir(settings.dirname,location)){ fs.readdir(location,{withFileTypes:true},(err,files)=>{ if(err){ next(err) @@ -44,7 +43,7 @@ app.post('/files/ls',(req,res,next)=>{ else{ res.json({ "loc": nloc , - "back": inDir(settings.dirname, path.normalize(path.join(location,'..')) )?path.normalize(path.join(nloc,'..')):null, + "back": processing.inDir(settings.dirname, path.normalize(path.join(location,'..')) )?path.normalize(path.join(nloc,'..')):null, "contents":processing.dirprocess(files,location,settings) }) } @@ -65,9 +64,10 @@ app.post('/files/mv',(req,res,next)=>{ } const loc2 = processing.mergedir(req.body.nloc,settings) //log([loc1,loc2]) - if(inDir(settings.dirname,loc1)&&inDir(settings.dirname,loc2)){ + if(processing.inDir(settings.dirname,loc1)&&processing.inDir(settings.dirname,loc2)){ fs.rename(loc1,loc2,err=>{ if(err){ + // Enable to watch errors //console.log(err) next(err) } @@ -78,10 +78,46 @@ app.post('/files/mv',(req,res,next)=>{ } }) -// Attempt to upload a file - Placeholder - needs busboy -app.put('/files/upload',(req,res)=>{ - console.log("Upload attempted") - res.json({'error':500}) +// Attempt to upload a file +// Note : loc takes in directory, and not file +app.post('/files/upload',(req,res,next)=>{ + //console.log("Upload attempted") + let oloc=null + let nloc={path:null,fn:null} + req.pipe(req.busboy) + + req.busboy.on('field',(fieldname,val,fieldtrunc,valtruc,encoding,mimetype)=>{ + if(fieldname=='loc') nloc.path = val + }) + req.busboy.on("file",(fieldname, file, filename, encoding, mimetype)=>{ + oloc = processing.getTmpDir(filename) + nloc.fn = path.basename(filename) + //console.log(oloc) + file.pipe(fs.createWriteStream(oloc)) + }) + req.busboy.on('finish',()=>{ + try{ + //console.log(nloc) + // Read + if(nloc.path===null) { + throw Error("No path defined") + } + const loc = path.join(settings.dirname,nloc.path.trim(),nloc.fn) + //Make sure uploadable location is in required directory + if(processing.inDir(settings.dirname,loc)){ + fs.createReadStream(oloc).pipe(fs.createWriteStream(loc)) + res.json({"done":"Uploaded"}) + } + else{ + throw new Error("Not in directory") + } + } + catch(e){ + // Enable to watch errors + //console.log(e) + next(e) + } + }) }) diff --git a/processing.js b/processing.js index 88a1893..a1a00a7 100644 --- a/processing.js +++ b/processing.js @@ -1,4 +1,5 @@ const path = require('path') +const os = require('os') module.exports.mergedir = (dirname,settings)=>{ return path.normalize(path.join(settings.dirname,dirname)); } @@ -20,3 +21,8 @@ module.exports.dirprocess = (dirstream,location,settings)=>{ return contents } + +// settings.dirname, whatever +module.exports.inDir = (dircheck,dirmain) => !path.relative(path.normalize(dircheck), dirmain).startsWith('..') + +module.exports.getTmpDir = (location) => path.join(os.tmpdir(), path.basename(location)) \ No newline at end of file