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 path = require('path')
const processing = require('./processing') const processing = require('./processing')
const port = 8080; // Import settings
app = express()
let settings = JSON.parse(fs.readFileSync("settings.json")) let settings = JSON.parse(fs.readFileSync("settings.json"))
const DIR=settings.dirname; app = express()
app.use(bodyParser.urlencoded({extended:false})) app.use(bodyParser.urlencoded({extended:false}))
app.use(bodyParser.json()) app.use(bodyParser.json())
@@ -18,7 +15,8 @@ app.use(bodyParser.json())
let inDir = (dircheck,dirmain) => !path.relative(path.normalize(dircheck), dirmain).startsWith('..') let inDir = (dircheck,dirmain) => !path.relative(path.normalize(dircheck), dirmain).startsWith('..')
//Get folder details // Download file
//loc
app.get('/files/cat',(req,res,next)=>{ app.get('/files/cat',(req,res,next)=>{
//console.log(req.body) //console.log(req.body)
const location = processing.mergedir(req.query.loc,settings) 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)=>{ app.post('/files/ls',(req,res,next)=>{
const location = processing.mergedir(req.body.loc,settings) const location = processing.mergedir(req.body.loc,settings)
//const nloc = path.normalize(req.body.loc); //const nloc = path.normalize(req.body.loc);
// nloc - Path to show the user
const nloc = path.normalize(path.relative(settings.dirname,location)) const nloc = path.normalize(path.relative(settings.dirname,location))
//Make sure not escaping the given path; insecure //Make sure not escaping the given path; insecure
if(inDir(settings.dirname,location)){ if(inDir(settings.dirname,location)){
@@ -51,20 +52,31 @@ app.post('/files/ls',(req,res,next)=>{
else{ else{
res.status(404).json({"error":"Access denied","loc":'/'}) 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 - needs busboy
//Attempt to upload a file - Placeholder
app.put('/files/upload',(req,res)=>{ app.put('/files/upload',(req,res)=>{
console.log("Upload attempted") console.log("Upload attempted")
res.json({'error':500}) res.json({'error':500})
}) })
// Use font-awesome // Use font-awesome
app.use('/fa',express.static(path.join(__dirname,'node_modules','@fortawesome','fontawesome-free'))) app.use('/fa',express.static(path.join(__dirname,'node_modules','@fortawesome','fontawesome-free')))
@@ -81,12 +93,12 @@ app.all('*',(req,res)=>{
}) })
app.listen(port,()=>{ app.listen(settings.port,()=>{
console.log(`Listening : ${port}`) console.log(`Listening : ${settings.port}`)
}) })
app.use((err,req,res,next)=>{ app.use((err,req,res,next)=>{
console.log(err) console.log(err.code)
res.status(500).json({error:`Internal error.Try again.`}) res.status(500).json({error:`Internal error.Try again.`})
}) })

View File

@@ -1,5 +1,6 @@
{ {
"dirname":"./sandbox/", "dirname":"./sandbox/",
"dbpath":null, "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 done-rename"><span class="far fa-check-circle"></span></div>
<div class="window-button close-rename"><span class="far fa-times-circle"></span></div> <div class="window-button close-rename"><span class="far fa-times-circle"></span></div>
<form> <form>
<input type='value' placeholder="New file name"/> <input type='value' placeholder="New location (Rel.)"/>
</form> </form>
</div> </div>
<div class="upload-window window box-shadow-2 hidden"> <div class="upload-window window box-shadow-2 hidden">