From e9cd71b770423c460bbac23f3374b4fb3e8bec2c Mon Sep 17 00:00:00 2001 From: Christopher Rose Date: Wed, 22 May 2019 20:22:59 +0530 Subject: [PATCH] Implement traversal of folders --- index.js | 79 ++++++++++++++++++++++++++++++-------------------- processing.js | 31 ++++++++++---------- static/base.js | 17 ++++++++--- 3 files changed, 76 insertions(+), 51 deletions(-) diff --git a/index.js b/index.js index d82626f..67230dd 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,22 @@ +/* +POST request contains a JSON object +{ + "loc": +} +Reply format +{ + "loc": , + "back": + "contents":[ + { + "name": , + "path": + "isDir": + } + ] +} +*/ const express = require('express') @@ -16,19 +34,35 @@ const DIR=settings.dirname; app.use(bodyParser.urlencoded({extended:false})) app.use(bodyParser.json()) -//Get the status of the folder and things related -app.get('/files',(req,res,next)=>{ - fs.readdir(DIR,{withFileTypes:true},(err,stream)=>{ - if(err){ - next(err) - } - else{ - //res.json(stream[0].isDirectory()) - res.json({"filename":`${DIR}`,"loc":processing.dirprocess(stream,settings)}); - } - }) -}) +let inDir = (dircheck,dirmain) => !path.relative(path.normalize(dircheck), dirmain).startsWith('..') +//Get folder details + +app.post('/files/ls',(req,res,next)=>{ + const location = processing.mergedir(req.body.loc,settings) + //console.log(path.relative( path.normalize(settings.dirname) ,location)) + + //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({ + "location": location , + "back": inDir(settings.dirname, path.normalize(path.join(location,'..')) )?path.normalize(path.join(location,'..')):null, + "contents":processing.dirprocess(files,location,settings) + }) + } + }) + } + else{ + res.json({"error":"Access denied","loc":'/'}) + } + + //next() +}) //Attempt to upload a file - Placeholder app.put('/files/upload',(req,res)=>{ @@ -36,27 +70,10 @@ app.put('/files/upload',(req,res)=>{ res.json({'error':500}) }) - -//Get folder details -app.post('/files/ls',(req,res,next)=>{ - const location = processing.mergedir(req.body.loc,settings) - fs.readdir(location,{withFileTypes:true},(err,files)=>{ - if(err){ - next(err) - } - else{ - res.json({ - "location": location , - "contents":processing.dirprocess(files,settings) - }) - } - }) - - //next() -}) -//console.log(path.join(__dirname ,'node_modules','jquery','dist')) +// Use jquery app.use('/jquery', express.static( path.join(__dirname ,'node_modules','jquery','dist') ) ) +// Use the statics app.get( '/*', express.static( path.join(__dirname,'static') ) ) diff --git a/processing.js b/processing.js index c5dffd3..e0d72e0 100644 --- a/processing.js +++ b/processing.js @@ -1,23 +1,22 @@ -const paths = require('path') +const path = require('path') module.exports.mergedir = (dirname,settings)=>{ - return paths.normalize(paths.join(settings.dirname,dirname)); + return path.normalize(path.join(settings.dirname,dirname)); } -module.exports.dirprocess = (dirstream,settings)=>{ +//produces the contents array +module.exports.dirprocess = (dirstream,location,settings)=>{ + let contents = [] dirstream.forEach(element => { - element.type = element.isDirectory() + console.log(element) + if(!(element.name.startsWith('.')&&!settings.showHidden) ) + { + contents.push({ + "name":element.name, + "path":path.normalize(path.join(location,element.name)) , + "isDir": element.isDirectory() + }) + } }); - dirstream.push({'name':'..','type':true}) - //dirstream.contents.push({'name':'..','type':true}) - if(!settings.showHidden){ - let fdirstream = dirstream.filter((ele)=>{ - //ele.type=ele.isDirectory - return ele.name[0]!='.'||ele.name=='..' - }) - return fdirstream - } - else{ - return dirstream - } + return contents } diff --git a/static/base.js b/static/base.js index 5e74865..c4825e5 100644 --- a/static/base.js +++ b/static/base.js @@ -5,7 +5,7 @@ let currDir = {'loc':'','contents':null}; function doUpdate(ele){ console.log(ele.attr('data-choice')); if(ele.hasClass('file-isDir')){ - currDir.loc = currDir.loc+ "/"+ ele.attr('data-choice'); + currDir.loc = ele.attr('data-choice'); populateContents(); } //$() @@ -13,15 +13,24 @@ function doUpdate(ele){ } function updateContents(contents){ - //console.log(contents) + console.log(contents) + + // Change top header contents $('#files-location').html(currDir.loc) - if(contents===null) $('#files-table').append(`null`) + + // if empty, return null, this shouldnt execute if the server is responding properly but ok + if(contents===null) { + $('#files-table').append(`null`) + } else { $('#files-table').empty(); contents.contents.forEach(element => { - $('#files-table').append(`${element.name}`) + $('#files-table').append(`${element.name}`) }); + if(contents.back!=null){ + $('#files-table').prepend(`..`) + } } }