-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser.js
130 lines (104 loc) · 3.7 KB
/
browser.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
var express = require('express');
var pug = require('pug');
var fs = require('fs');
const { join, extname, resolve } = require("path")
var moment = require("moment");
var cookieParser = require('cookie-parser')
function create_browser(config) {
this.router = express.Router();
// FIXME: make configurable:
this.targetlink = config.targetlink || "/";
this.extensions = config.extensions || {
'.ubdaq':"Raw UBDAQ files",
'.root':"Root files (Can read AnalysisTuple OR Larsoft OR Larlite files)"
};
this.default_path = config.default_path || __dirname;
this.allowed_paths = config.allowed_paths || ['/home','/Users','/pnfs','/uboone/data','/uboone/app'];
var self = this;
// console.log("create browser with config",config,self);
function human_size(size)
{
if (size > 1099511627776) // TiB: 1024 GiB
{
return (size / 1099511627776).toFixed(2) + " TB";
}
else if (size > 1073741824) // GiB: 1024 MiB
{
return (size / 1073741824).toFixed(2) + " GB";
}
else if (size > 1048576) // MiB: 1024 KiB
{
return (size / 1048576).toFixed(2) + " MB";
}
else if (size > 1024) // KiB: 1024 B
{
return (size / 1024).toFixed(0) + " kB";
}
else // bytes
{
return size + " bytes";
}
}
async function browser(path,req,res,next)
{
// console.log(self);
// Do we have a path? If not, then use cookie save.
if(path.length<2) {
path = ((req.cookies ||{})['file-browser-saved-path']) || self.default_path;
}
// Is the path we've been given legal?
// FIXME
// Does path exist?
if(!fs.existsSync(path)) return res.status(404).send("Specified path does not exist: "+path);
// FIXME
res.cookie('file-browser-saved-path',path);
// console.log("looking at ",path);
// look at path contents
var result = {
baseUrl: req.baseUrl,
path: path,
targetlink: self.targetlink,
extensions: self.extensions,
files: { dirs:[] }
};
for( e in self.extensions ) { result.files[e] = []; }
try {
let files = await fs.promises.readdir(path);
for(const file of files) {
if(file[0]=='.') continue;
var fullpath = resolve(path,file);
if(!fs.existsSync(fullpath)) continue;
var stats = await fs.promises.stat(fullpath);
var extension = extname(file);
// console.log(file,extension);
var entry = {
"filename": file,
"pathname": fullpath,
"modified" : moment(stats.mtime).format('MMM Do YYYY, h:mm:ss a'),
"ago" : moment(stats.mtime).fromNow(),
"size" : human_size(stats.size),
"sizedata" : stats.size,
"mdata" : stats.mtime.getTime(),
}
if(stats.isDirectory()) result.files.dirs.push(entry);
if(extension in self.extensions) {
result.files[extension].push(entry);
// console.log(entry);
}
}
} catch(err) {
console.error("what?",err);
}
// console.log(result);
res.send( pug.renderFile("pug/browser.pug",result) );
}
this.router.use(cookieParser());
this.router.get("/*",function(req,res,next){
browser(req.path,req,res,next);
});
this.router.get("/",function(req,res,next){
var path = req.query.path;
browser(path,req,res,next);
});
}
module.exports = create_browser;