Server side - Implement uploading
This commit is contained in:
60
index.js
60
index.js
@@ -1,8 +1,10 @@
|
|||||||
|
|
||||||
const express = require('express')
|
const express = require('express')
|
||||||
const bodyParser = require('body-parser')
|
const bodyParser = require('body-parser')
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const processing = require('./processing')
|
const processing = require('./processing')
|
||||||
|
const busboy = require('connect-busboy')
|
||||||
|
|
||||||
// Import settings
|
// Import settings
|
||||||
let settings = JSON.parse(fs.readFileSync("settings.json"))
|
let settings = JSON.parse(fs.readFileSync("settings.json"))
|
||||||
@@ -10,10 +12,7 @@ let settings = JSON.parse(fs.readFileSync("settings.json"))
|
|||||||
app = express()
|
app = express()
|
||||||
app.use(bodyParser.urlencoded({extended:false}))
|
app.use(bodyParser.urlencoded({extended:false}))
|
||||||
app.use(bodyParser.json())
|
app.use(bodyParser.json())
|
||||||
|
app.use(busboy())
|
||||||
// Check if a given directory is within the main defined directory or not
|
|
||||||
let inDir = (dircheck,dirmain) => !path.relative(path.normalize(dircheck), dirmain).startsWith('..')
|
|
||||||
|
|
||||||
|
|
||||||
// Download file
|
// Download file
|
||||||
//loc
|
//loc
|
||||||
@@ -22,7 +21,7 @@ app.get('/files/cat',(req,res,next)=>{
|
|||||||
const location = processing.mergedir(req.query.loc,settings)
|
const location = processing.mergedir(req.query.loc,settings)
|
||||||
//const nloc = path.normalize(req.body.loc);
|
//const nloc = path.normalize(req.body.loc);
|
||||||
const nloc = path.normalize(path.relative(settings.dirname,location))
|
const nloc = path.normalize(path.relative(settings.dirname,location))
|
||||||
if(inDir(settings.dirname,location)){
|
if(processing.inDir(settings.dirname,location)){
|
||||||
res.download(location,err=>{if(err) next(err)} )
|
res.download(location,err=>{if(err) next(err)} )
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -36,7 +35,7 @@ app.post('/files/ls',(req,res,next)=>{
|
|||||||
const nloc = path.normalize(path.relative(settings.dirname,location))
|
const nloc = path.normalize(path.relative(settings.dirname,location))
|
||||||
//console.log([loc,nloc])
|
//console.log([loc,nloc])
|
||||||
//Make sure not escaping the given path; insecure
|
//Make sure not escaping the given path; insecure
|
||||||
if(inDir(settings.dirname,location)){
|
if(processing.inDir(settings.dirname,location)){
|
||||||
fs.readdir(location,{withFileTypes:true},(err,files)=>{
|
fs.readdir(location,{withFileTypes:true},(err,files)=>{
|
||||||
if(err){
|
if(err){
|
||||||
next(err)
|
next(err)
|
||||||
@@ -44,7 +43,7 @@ app.post('/files/ls',(req,res,next)=>{
|
|||||||
else{
|
else{
|
||||||
res.json({
|
res.json({
|
||||||
"loc": nloc ,
|
"loc": nloc ,
|
||||||
"back": inDir(settings.dirname, path.normalize(path.join(location,'..')) )?path.normalize(path.join(nloc,'..')):null,
|
"back": processing.inDir(settings.dirname, path.normalize(path.join(location,'..')) )?path.normalize(path.join(nloc,'..')):null,
|
||||||
"contents":processing.dirprocess(files,location,settings)
|
"contents":processing.dirprocess(files,location,settings)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -65,9 +64,10 @@ app.post('/files/mv',(req,res,next)=>{
|
|||||||
}
|
}
|
||||||
const loc2 = processing.mergedir(req.body.nloc,settings)
|
const loc2 = processing.mergedir(req.body.nloc,settings)
|
||||||
//log([loc1,loc2])
|
//log([loc1,loc2])
|
||||||
if(inDir(settings.dirname,loc1)&&inDir(settings.dirname,loc2)){
|
if(processing.inDir(settings.dirname,loc1)&&processing.inDir(settings.dirname,loc2)){
|
||||||
fs.rename(loc1,loc2,err=>{
|
fs.rename(loc1,loc2,err=>{
|
||||||
if(err){
|
if(err){
|
||||||
|
// Enable to watch errors
|
||||||
//console.log(err)
|
//console.log(err)
|
||||||
next(err)
|
next(err)
|
||||||
}
|
}
|
||||||
@@ -78,10 +78,46 @@ app.post('/files/mv',(req,res,next)=>{
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// Attempt to upload a file - Placeholder - needs busboy
|
// Attempt to upload a file
|
||||||
app.put('/files/upload',(req,res)=>{
|
// Note : loc takes in directory, and not file
|
||||||
console.log("Upload attempted")
|
app.post('/files/upload',(req,res,next)=>{
|
||||||
res.json({'error':500})
|
//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{
|
||||||
|
//console.log(nloc)
|
||||||
|
// 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))
|
||||||
|
res.json({"done":"Uploaded"})
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
throw new Error("Not in directory")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch(e){
|
||||||
|
// Enable to watch errors
|
||||||
|
//console.log(e)
|
||||||
|
next(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
const path = require('path')
|
const path = require('path')
|
||||||
|
const os = require('os')
|
||||||
module.exports.mergedir = (dirname,settings)=>{
|
module.exports.mergedir = (dirname,settings)=>{
|
||||||
return path.normalize(path.join(settings.dirname,dirname));
|
return path.normalize(path.join(settings.dirname,dirname));
|
||||||
}
|
}
|
||||||
@@ -20,3 +21,8 @@ module.exports.dirprocess = (dirstream,location,settings)=>{
|
|||||||
return contents
|
return contents
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// settings.dirname, whatever
|
||||||
|
module.exports.inDir = (dircheck,dirmain) => !path.relative(path.normalize(dircheck), dirmain).startsWith('..')
|
||||||
|
|
||||||
|
module.exports.getTmpDir = (location) => path.join(os.tmpdir(), path.basename(location))
|
Reference in New Issue
Block a user