Process file type and send it over

This commit is contained in:
2019-05-21 20:31:41 +05:30
parent 4f01ee8cb8
commit 51775c76f2
5 changed files with 47 additions and 11 deletions

View File

@@ -18,12 +18,13 @@ app.use(bodyParser.json())
//Get the status of the folder and things related //Get the status of the folder and things related
app.get('/files',(req,res,next)=>{ app.get('/files',(req,res,next)=>{
fs.readdir(DIR,(err,stream)=>{ fs.readdir(DIR,{withFileTypes:true},(err,stream)=>{
if(err){ if(err){
next(err) next(err)
} }
else{ else{
res.json({"filename":`${DIR}`,"rootdir":processing.dirprocess(stream,settings)}); //res.json(stream[0].isDirectory())
res.json({"filename":`${DIR}`,"loc":processing.dirprocess(stream,settings)});
} }
}) })
}) })
@@ -39,14 +40,14 @@ app.put('/files/upload',(req,res)=>{
//Get folder details //Get folder details
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)
fs.readdir(location,(err,files)=>{ fs.readdir(location,{withFileTypes:true},(err,files)=>{
if(err){ if(err){
next(err) next(err)
} }
else{ else{
res.json({ res.json({
"location": location , "location": location ,
"contents":files "contents":processing.dirprocess(files,settings)
}) })
} }
}) })

View File

@@ -4,9 +4,13 @@ module.exports.mergedir = (dirname,settings)=>{
} }
module.exports.dirprocess = (dirstream,settings)=>{ module.exports.dirprocess = (dirstream,settings)=>{
dirstream.forEach(element => {
element.type = element.isDirectory()
});
if(!settings.showHidden){ if(!settings.showHidden){
let fdirstream = dirstream.filter((ele)=>{ let fdirstream = dirstream.filter((ele)=>{
return ele[0]!='.' //ele.type=ele.isDirectory
return ele.name[0]!='.'
}) })
return fdirstream return fdirstream
} }

View File

@@ -98,6 +98,12 @@ table td{
tbody tr:last-child{ tbody tr:last-child{
border:0; border:0;
} }
tbody td.file-isDir{
font-style: italic;
}
tbody td.file-isDir::after{
content: '/';
}
.box-shadow-1{ .box-shadow-1{

View File

@@ -1,8 +1,33 @@
let currDir = '/'; let currDir = {'loc':'','contents':null};
function updateContents(contents){
console.log(contents)
$('#files-location').html(contents.loc)
if(contents===null) $('#files-table').append(`<tr><td>null</td><td class="file-handlers"></td></tr>`)
else
contents.contents.forEach(element => {
$('#files-table').append(`<tr><td class="file-name ${(element.type?'file-isDir':'file-isFile')}">${element.name}</td><td class="file-handlers"></td></tr>`)
});
}
//set table details //set table details
function setList(){ function populateContents(){
$.ajax('/files/ls'+currDir.loc,{
method:'post',
data: currDir,
success:(msg)=>{
updateContents(msg)
}
})
return null;
} }
$(document).ready(()=>{
populateContents();
//updateContents();
})

View File

@@ -10,7 +10,7 @@
<body> <body>
<div class="navbar-container box-shadow-1"> <div class="navbar-container box-shadow-1">
<div class="navbar"> <div class="navbar">
<div class="navbar-text">TEXT</div> <div class="navbar-text">Location: /<span id="files-location"></span></div>
</div> </div>
</div> </div>
@@ -23,15 +23,15 @@
<td>Actions</td> <td>Actions</td>
</tr> </tr>
</thead> </thead>
<tbody> <tbody id="files-table">
<tr> <!--<tr>
<td>TEST1</td> <td>TEST1</td>
<td class="file-handlers">TEST2</td> <td class="file-handlers">TEST2</td>
</tr> </tr>
<tr> <tr>
<td>TEST3</td> <td>TEST3</td>
<td class="file-handlers">TEST4</td> <td class="file-handlers">TEST4</td>
</tr> </tr>-->
</tbody> </tbody>
</table> </table>
</div> </div>