2019-05-19 12:21:40 +05:30
|
|
|
const express = require('express')
|
|
|
|
const bodyParser = require('body-parser')
|
2019-05-21 10:38:54 +05:30
|
|
|
const fs = require('fs')
|
2019-05-19 18:48:50 +05:30
|
|
|
const path = require('path')
|
2019-05-21 10:38:54 +05:30
|
|
|
const processing = require('./processing')
|
2019-05-19 18:48:50 +05:30
|
|
|
|
2019-05-19 12:21:40 +05:30
|
|
|
const port = 8080;
|
2019-05-24 12:04:21 +05:30
|
|
|
|
2019-05-19 12:21:40 +05:30
|
|
|
app = express()
|
|
|
|
|
2019-05-21 10:38:54 +05:30
|
|
|
let settings = JSON.parse(fs.readFileSync("settings.json"))
|
|
|
|
|
|
|
|
const DIR=settings.dirname;
|
2019-05-19 12:21:40 +05:30
|
|
|
app.use(bodyParser.urlencoded({extended:false}))
|
|
|
|
app.use(bodyParser.json())
|
|
|
|
|
2019-05-24 10:45:06 +05:30
|
|
|
// Check if a given directory is within the main defined directory or not
|
2019-05-22 20:22:59 +05:30
|
|
|
let inDir = (dircheck,dirmain) => !path.relative(path.normalize(dircheck), dirmain).startsWith('..')
|
|
|
|
|
2019-05-24 10:45:06 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
2019-05-22 20:22:59 +05:30
|
|
|
//Get folder details
|
2019-05-24 11:43:48 +05:30
|
|
|
app.post('/files/cat',(req,res,next)=>{
|
|
|
|
const location = processing.mergedir(req.body.loc,settings)
|
|
|
|
//const nloc = path.normalize(req.body.loc);
|
|
|
|
const nloc = path.normalize(path.relative(settings.dirname,location))
|
|
|
|
if(inDir(settings.dirname,location)){
|
|
|
|
res.download(location,err=>{if(err) next(err)} )
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-05-21 10:38:54 +05:30
|
|
|
|
2019-05-22 20:22:59 +05:30
|
|
|
app.post('/files/ls',(req,res,next)=>{
|
|
|
|
const location = processing.mergedir(req.body.loc,settings)
|
2019-05-24 11:25:10 +05:30
|
|
|
//const nloc = path.normalize(req.body.loc);
|
|
|
|
const nloc = path.normalize(path.relative(settings.dirname,location))
|
2019-05-22 20:22:59 +05:30
|
|
|
//Make sure not escaping the given path; insecure
|
|
|
|
if(inDir(settings.dirname,location)){
|
|
|
|
fs.readdir(location,{withFileTypes:true},(err,files)=>{
|
|
|
|
if(err){
|
|
|
|
next(err)
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
res.json({
|
2019-05-24 11:04:51 +05:30
|
|
|
"loc": nloc ,
|
|
|
|
"back": inDir(settings.dirname, path.normalize(path.join(location,'..')) )?path.normalize(path.join(nloc,'..')):null,
|
2019-05-22 20:22:59 +05:30
|
|
|
"contents":processing.dirprocess(files,location,settings)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
else{
|
2019-05-24 08:48:01 +05:30
|
|
|
res.status(404).json({"error":"Access denied","loc":'/'})
|
2019-05-22 20:22:59 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
//next()
|
|
|
|
})
|
2019-05-21 10:38:54 +05:30
|
|
|
|
2019-05-24 10:45:06 +05:30
|
|
|
app.post('/files/ls',(res,rep,next)=>{
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2019-05-21 19:37:11 +05:30
|
|
|
//Attempt to upload a file - Placeholder
|
2019-05-19 13:08:23 +05:30
|
|
|
app.put('/files/upload',(req,res)=>{
|
|
|
|
console.log("Upload attempted")
|
2019-05-21 19:37:11 +05:30
|
|
|
res.json({'error':500})
|
2019-05-19 13:08:23 +05:30
|
|
|
})
|
|
|
|
|
2019-05-22 20:22:59 +05:30
|
|
|
// Use jquery
|
2019-05-21 19:37:11 +05:30
|
|
|
app.use('/jquery', express.static( path.join(__dirname ,'node_modules','jquery','dist') ) )
|
2019-05-19 12:21:40 +05:30
|
|
|
|
2019-05-22 20:22:59 +05:30
|
|
|
// Use the statics
|
2019-05-21 19:37:11 +05:30
|
|
|
app.get( '/*', express.static( path.join(__dirname,'static') ) )
|
2019-05-21 10:38:54 +05:30
|
|
|
|
2019-05-19 12:21:40 +05:30
|
|
|
|
2019-05-21 19:37:11 +05:30
|
|
|
//All non-matched end up in this route
|
2019-05-19 12:21:40 +05:30
|
|
|
app.all('*',(req,res)=>{
|
|
|
|
res.status(404).json({'error':404});
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
app.listen(port,()=>{
|
2019-05-19 18:48:50 +05:30
|
|
|
console.log(`Listening : ${port}`)
|
2019-05-19 12:21:40 +05:30
|
|
|
})
|
2019-05-24 08:48:01 +05:30
|
|
|
|
2019-05-24 10:45:06 +05:30
|
|
|
app.use((err,req,res,next)=>{
|
2019-05-24 11:43:48 +05:30
|
|
|
res.status(500).json({error:`Internal error.Try again.`})
|
2019-05-24 10:45:06 +05:30
|
|
|
})
|
|
|
|
|
2019-05-24 08:48:01 +05:30
|
|
|
module.exports = app;
|