-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathutils.js
146 lines (140 loc) · 5.38 KB
/
utils.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
String.prototype.htmlEscape = function() {
return this.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'")
}
module.exports = {
humanFileSize: function(bytes, si=false, dp=1) {
if (! bytes) {
return ''
}
//from https://stackoverflow.com/questions/10420352/converting-file-size-in-bytes-to-human-readable-string/10420404
const thresh = si ? 1000 : 1024;
if (Math.abs(bytes) < thresh) {
return bytes + ' B';
}
const units = si
? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
let u = -1;
const r = 10**dp;
do {
bytes /= thresh;
++u;
} while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1);
return bytes.toFixed(dp) + ' ' + units[u];
},
lastModified: function(modificationTime) {
if (! modificationTime) {
return 0
}
var lastModifiedMonth = modificationTime.getMonth() + 1
var lastModifiedDay = modificationTime.getDate()
var lastModifiedYear = modificationTime.getFullYear().toString().substring(2, 4)
var lastModifiedHours = modificationTime.getHours()
var lastModifiedMinutes = modificationTime.getMinutes()
var lastModifiedSeconds = modificationTime.getSeconds()
var lastModified = lastModifiedMonth+
lastModifiedDay+
lastModifiedYear+
lastModifiedHours+
lastModifiedMinutes+
lastModifiedSeconds
return lastModified
},
lastModifiedStr: function(modificationTime) {
if (! modificationTime) {
return ''
}
var lastModifiedMonth = modificationTime.getMonth() + 1
var lastModifiedDay = modificationTime.getDate()
var lastModifiedYear = modificationTime.getFullYear().toString().substring(2, 4)
var lastModifiedHours = modificationTime.getHours()
var lastModifiedMinutes = modificationTime.getMinutes()
var lastModifiedSeconds = modificationTime.getSeconds()
if (lastModifiedSeconds.toString().length != 2) {
var lastModifiedSeconds = '0' + lastModifiedSeconds
}
if (lastModifiedMinutes.toString().length != 2) {
var lastModifiedMinutes = '0' + lastModifiedMinutes
}
if (lastModifiedDay.toString().length != 2) {
var lastModifiedDay = '0' + lastModifiedDay
}
if (lastModifiedHours >= 12) {
var lastModifiedAmPm = 'PM'
if (lastModifiedHours > 12) {
var lastModifiedHours = lastModifiedHours - 12
}
} else {
var lastModifiedAmPm = 'AM'
}
var lastModifiedStr = lastModifiedMonth+'/'+
lastModifiedDay+'/'+
lastModifiedYear+', '+
lastModifiedHours+':'+
lastModifiedMinutes+':'+
lastModifiedSeconds +' '+
lastModifiedAmPm
return lastModifiedStr
},
htaccessFileRequested: function(filerequested, index) {
if (index) {
if (filerequested == 'index.html' ||
filerequested == 'index.htm' ||
filerequested == 'index' ||
filerequested == 'index.xhtm' ||
filerequested == 'index.xhtml' ||
filerequested == '') {
return 'index'
} else {
return filerequested
}
} else {
if (filerequested == 'index.html' ||
filerequested == 'index.htm' ||
filerequested == 'index' ||
filerequested == 'index.xhtm' ||
filerequested == 'index.xhtml') {
return 'index'
} else {
return filerequested
}
}
},
relativePath: function(reqPath, curPath) {
var endWSlash = false
if (reqPath.endsWith('/')) {
var endWSlash = true
}
var split1 = curPath.split('/')
var split2 = reqPath.split('/')
for (var w=0; w<split2.length; w++) {
if (split2[w] == '' || split2[w] == '.') {
// . means current directory. Leave this here for spacing
} else if (split2[w] == '..') {
if (split1.length > 0) {
var split1 = WSC.utils.stripOffFile(split1.join('/')).split('/')
}
} else {
split1.push(split2[w])
}
}
var newPath = split1.join('/').replace(/\/\//g, '/')
if (! newPath.startsWith('/')) {
var newPath = '/' + newPath
}
if (endWSlash && ! newPath.endsWith('/')) {
var newPath = newPath + '/'
}
return newPath
},
stripOffFile: function(origpath) {
var fullrequestpath = origpath
var finpath = fullrequestpath.split('/').pop()
var finalpath = fullrequestpath.substring(0, fullrequestpath.length - finpath.length)
if (origpath == '/') {
return '/'
} else {
return finalpath
}
}
}