-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
98 lines (65 loc) · 2.08 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// LATEST USE THIS WORKING
var express = require('express'),
cors = require('cors')
ls = require('ls'), // modified library
async = require('async'),
path = require('path'),
fs = require('fs'),
filesize = require("filesize"),
moment = require('moment');
const app = express()
//onefifty
//twohundred
//onefifty
//onetwentyfife
//1,3,5,8,10,20,30,40,50,many
//source library to read large directory https://github.com/Zlitus/large-directory-readdir
var filesArray = []
// Request http://localhost:4000/?directory=new
app.get('/', async(req, res) => {
let directoryName = ls('./'+req.query.directory+'/*');
console.log(directoryName.length)
var directorySize= directoryName.length
try {
for (var file of directoryName) {
filesArray.push(getFileData(file));
if(directorySize == filesArray.length){
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept, Authorization'
);
res.setHeader('Access-Control-Allow-Methods', '*');
res.json(filesArray)
res.end();
//reset variables
directoryName =null;
filesArray = [ ];
}
}
}
catch (error){
console.log(error)
}
})
function getFileData(file){
var filename = file.name;
var fullpath = file.full;
var filetype =file.type;
var fileSizeInMb = filesize(file.size, {round: 0});
var fileextension = file.extension;
var date = moment(file.date).format('L');
// check permissions
var permissions ;
try {
fs.accessSync(fullpath, fs.constants.R_OK | fs.constants.W_OK);
permissions = "read/write"
} catch (err) {
console.error('no access!');
}
var myfile = {name:filename,path:fullpath,type:filetype,size:fileSizeInMb,extension:fileextension,created:date,permission:permissions};
return myfile;
}
app.use(cors());
app.listen(4000);
console.log('Running aAPI server at http://localhost:4000/');