Backend - implement moving, move port to settings

This commit is contained in:
2019-05-26 21:40:24 +05:30
parent e56cc5a348
commit d50f0bc4a0
3 changed files with 30 additions and 17 deletions

View File

@@ -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.`})
})

View File

@@ -1,5 +1,6 @@
{
"dirname":"./sandbox/",
"dbpath":null,
"showHidden":false
"showHidden":false,
"port":8080
}

View File

@@ -14,7 +14,7 @@
<div class="window-button done-rename"><span class="far fa-check-circle"></span></div>
<div class="window-button close-rename"><span class="far fa-times-circle"></span></div>
<form>
<input type='value' placeholder="New file name"/>
<input type='value' placeholder="New location (Rel.)"/>
</form>
</div>
<div class="upload-window window box-shadow-2 hidden">