Files
nodejs-fm/index.js

151 lines
4.4 KiB
JavaScript
Raw Permalink Normal View History

2019-05-27 14:21:51 +05:30
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-27 14:21:51 +05:30
const busboy = require('connect-busboy')
2020-12-05 10:43:11 +05:30
const morgan = require('morgan');
// Import settings
2019-09-15 13:15:21 +05:30
const settings = JSON.parse(fs.readFileSync("settings.json"))
2019-05-21 10:38:54 +05:30
app = express()
2019-05-19 12:21:40 +05:30
app.use(bodyParser.urlencoded({extended:false}))
app.use(bodyParser.json())
2020-12-05 10:43:11 +05:30
app.use(morgan('tiny'))
2019-05-27 14:21:51 +05:30
app.use(busboy())
2019-05-24 10:45:06 +05:30
2019-09-15 13:18:54 +05:30
///Make the directory if it doesnt exist
if(!fs.existsSync(settings.dirname)){
fs.mkdirSync(settings.dirname)
}
// Download file
//loc
app.get('/files/cat',(req,res,next)=>{
//console.log(req.body)
const location = processing.mergedir(req.query.loc,settings)
//const nloc = path.normalize(req.body.loc);
const nloc = path.normalize(path.relative(settings.dirname,location))
2019-05-27 14:21:51 +05:30
if(processing.inDir(settings.dirname,location)){
res.download(location,err=>{if(err) next(err)} )
}
})
//Get folder details
//loc
2019-05-22 20:22:59 +05:30
app.post('/files/ls',(req,res,next)=>{
const location = processing.mergedir(req.body.loc,settings)
// nloc - Path to show the user
2019-05-24 11:25:10 +05:30
const nloc = path.normalize(path.relative(settings.dirname,location))
2019-05-27 10:51:05 +05:30
//console.log([loc,nloc])
2019-05-22 20:22:59 +05:30
//Make sure not escaping the given path; insecure
2019-05-27 14:21:51 +05:30
if(processing.inDir(settings.dirname,location)){
2019-05-22 20:22:59 +05:30
fs.readdir(location,{withFileTypes:true},(err,files)=>{
if(err){
next(err)
}
else{
res.json({
2019-05-24 11:04:51 +05:30
"loc": nloc ,
2019-05-27 14:21:51 +05:30
"back": processing.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
}
})
2019-05-21 10:38:54 +05:30
// Rename / move files
//loc,nloc
app.post('/files/mv',(req,res,next)=>{
2019-05-27 10:51:05 +05:30
//console.log(req.body)
const loc1 = processing.mergedir(req.body.loc,settings)
2019-05-27 10:51:05 +05:30
if(path.normalize(loc1).startsWith('.')) {
next(new Error("Cannot find dir"))
}
const loc2 = processing.mergedir(req.body.nloc,settings)
2019-05-27 10:51:05 +05:30
//log([loc1,loc2])
2019-05-27 14:21:51 +05:30
if(processing.inDir(settings.dirname,loc1)&&processing.inDir(settings.dirname,loc2)){
fs.rename(loc1,loc2,err=>{
if(err){
next(err)
}
2019-05-27 10:51:05 +05:30
else{
res.json({'loc':req.body.nloc})
}
})
}
2019-05-24 10:45:06 +05:30
})
2019-05-27 14:21:51 +05:30
// 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{
// 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))
2019-05-27 15:46:27 +05:30
res.json({"loc":nloc.path})
2019-05-27 14:21:51 +05:30
}
else{
throw new Error("Not in directory")
}
}
catch(e){
// Enable to watch errors
//console.log(e)
next(e)
}
})
2019-05-19 13:08:23 +05:30
})
2019-05-24 14:41:30 +05:30
// Use font-awesome
app.use('/fa',express.static(path.join(__dirname,'node_modules','@fortawesome','fontawesome-free')))
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(settings.port,()=>{
console.log(`Listening : ${settings.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-09-12 21:24:31 +05:30
console.log("E:"+JSON.stringify(err))
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;