diff --git a/index.js b/index.js index f5a5f47..e7e6084 100644 --- a/index.js +++ b/index.js @@ -4,13 +4,10 @@ const fs = require('fs') const path = require('path') const processing = require('./processing') -const port = 8080; - -app = express() - +// Import settings let settings = JSON.parse(fs.readFileSync("settings.json")) -const DIR=settings.dirname; +app = express() app.use(bodyParser.urlencoded({extended:false})) app.use(bodyParser.json()) @@ -18,7 +15,8 @@ app.use(bodyParser.json()) let inDir = (dircheck,dirmain) => !path.relative(path.normalize(dircheck), dirmain).startsWith('..') -//Get folder details +// Download file +//loc app.get('/files/cat',(req,res,next)=>{ //console.log(req.body) const location = processing.mergedir(req.query.loc,settings) @@ -29,9 +27,12 @@ app.get('/files/cat',(req,res,next)=>{ } }) +//Get folder details +//loc app.post('/files/ls',(req,res,next)=>{ const location = processing.mergedir(req.body.loc,settings) //const nloc = path.normalize(req.body.loc); + // nloc - Path to show the user const nloc = path.normalize(path.relative(settings.dirname,location)) //Make sure not escaping the given path; insecure if(inDir(settings.dirname,location)){ @@ -51,20 +52,31 @@ app.post('/files/ls',(req,res,next)=>{ else{ res.status(404).json({"error":"Access denied","loc":'/'}) } - - //next() }) -app.post('/files/ls',(res,rep,next)=>{ - +// Rename / move files +//loc,nloc +app.post('/files/mv',(req,res,next)=>{ + const loc1 = processing.mergedir(req.body.loc,settings) + const loc2 = processing.mergedir(req.body.nloc,settings) + if(inDir(settings.dirname,loc1)&&inDir(settings.dirname,loc2)){ + fs.rename(loc1,loc2,err=>{ + if(err){ + console.log(err) + next(err) + } + res.json({'loc':req.body.nloc}) + }) + } }) - -//Attempt to upload a file - Placeholder +// Attempt to upload a file - Placeholder - needs busboy app.put('/files/upload',(req,res)=>{ console.log("Upload attempted") res.json({'error':500}) }) + + // Use font-awesome app.use('/fa',express.static(path.join(__dirname,'node_modules','@fortawesome','fontawesome-free'))) @@ -81,12 +93,12 @@ app.all('*',(req,res)=>{ }) -app.listen(port,()=>{ - console.log(`Listening : ${port}`) +app.listen(settings.port,()=>{ + console.log(`Listening : ${settings.port}`) }) app.use((err,req,res,next)=>{ - console.log(err) + console.log(err.code) res.status(500).json({error:`Internal error.Try again.`}) }) diff --git a/settings.json b/settings.json index eeab79d..c119e50 100644 --- a/settings.json +++ b/settings.json @@ -1,5 +1,6 @@ { "dirname":"./sandbox/", "dbpath":null, - "showHidden":false + "showHidden":false, + "port":8080 } \ No newline at end of file diff --git a/static/index.html b/static/index.html index 5c82d34..8f6aa5d 100644 --- a/static/index.html +++ b/static/index.html @@ -14,7 +14,7 @@
- +