-
Notifications
You must be signed in to change notification settings - Fork 0
/
dir_list_json.js
206 lines (162 loc) · 5.98 KB
/
dir_list_json.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
* List files in a directory including various systems information.
* Saleem Bhatti, Sep 2018
*/
"use strict";
// https://nodejs.org/docs/latest-v8.x/api/fs.html
const fs = require('fs');
// https://nodejs.org/docs/latest-v8.x/api/os.html
const os = require('os');
// https://nodejs.org/docs/latest-v8.x/api/path.html
const path = require('path');
// https://nodejs.org/docs/latest-v8.x/api/errors.html#errors_class_error
// https://nodejs.org/docs/latest-v8.x/api/fs.html#fs_fs_statsync_path
// https://nodejs.org/docs/latest-v8.x/api/fs.html#fs_class_fs_stats
// https://nodejs.org/docs/latest-v8.x/api/fs.html#fs_fs_readdirsync_path_options
const fileInfo_keys = [
"filename", "type", "size",
"atime", "mtime", "ctime", "birthtime"
];
const dirInfo_keys = [
"server", "directoryname", "files"
];
function // This function turns a directory into a JSON object.
getDirInfo(dirPath, callback) { /* callback(error, result) */
if (!dirPath || !callback) { return null; }
let dirInfo = {};
dirInfo["server"] = os.hostname();
dirInfo["directoryname"] = dirPath;
dirInfo["files"] = {};
let error = null;
try {
let files = fs.readdirSync(dirPath);
if (files) {
for (let f = 0; f < files.length; ++f) {
let filename = files[f];
let filePath = path.join(dirPath, filename);
let fs_stats = fs.statSync(filePath);
let type = "unknown";
if (fs_stats.isFile()) { type = "file"; }
else if (fs_stats.isDirectory()) { type = "directory"; }
else if (fs_stats.isBlockDevice()) { type = "block"; }
else if (fs_stats.isCharacterDevice()) { type = "character"; }
else if (fs_stats.isFIFO()) { type = "fifo"; }
else if (fs_stats.isSocket()) { type = "socket"; }
/* The information we need */
let fileInfo = {};
fileInfo["filename"] = filename;
fileInfo["type"] = type;
for (let i = 0; i < fileInfo_keys.length; ++i) {
let k = fileInfo_keys[i];
if (k === "filename" || k === "type") { continue; }
let v = fs_stats[k];
if (k.includes("time")) {
// Date/Time is not handled so well in JavaScript.
// Safest option is to use:
// "time since 01 Jan 1970 in milliseconds"
// but convert that from the string version of
// the various time attributes if the file stats.
let t = Date.parse(v);
v = t;
}
fileInfo[k] = v;
}
dirInfo["files"][files[f]] = fileInfo;
} // for (f)
} // if (files)
} // try
catch (e) {
let e_s = null;
switch (e["code"]) {
case "ENOENT": e_s = "No such path: " + dirPath; break;
case "ENOTDIR": e_s = "Not a directory: " + dirPath; break;
default: e_s = JSON.stringify(error); break;
} // switch
console.log(e_s);
error = e;
} // catch
finally {
callback(error, dirInfo);
} // finally
} // function getDirFileInfo()
function
fileInfo2Markup(fileInfo) {
const fileInfo_keys = [
"filename", "type", "size",
"atime", "mtime", "ctime", "birthtime"
];
if (!fileInfo) { return null; }
let markup = "";
// leading spaces and "\n" for convenience only - not needed
//markup += "\n <fileinfo type=\"" + fileInfo["type"] + "\">\n";
markup += "\n <fileinfo type=\"" + fileInfo["type"] + "\"";
/**
* This 'if' is to add an onclick event to cells that represent a directory
*/
if (fileInfo["type"] == "directory") {
markup += "onclick=\"getDirectory(this.id)\"";
markup += "id=\"" + fileInfo["filename"] + "\"";
}
markup += ">\n";
for (let i = 0; i < fileInfo_keys.length; ++i) {
let k = fileInfo_keys[i];
let v = fileInfo[k];
let a = ""; // attributes for markup tag
if (k.includes("time")) {
// Keep the millisecond time as an attribute, but
// use a user-friendly string to display in markup.
// The millisecond time could be useful for ordering files.
let t = new Date();
t.setTime(v);
a += " " + k + "=\"" + v + "\"";
v = t.toLocaleString("en-GB");
}
markup += " <" + k + a + ">" + v + "</" + k + ">\n";
}
markup += " </fileinfo>\n";
return markup;
}
function
dirInfo2Markup(dirInfo) {
if (!dirInfo) { return null; }
let markup = "";
// leading spaces and "\n" for convenience only - not neededs
markup += "\n <dirinfo>\n\n";
markup += " <server>" + dirInfo["server"] + "</server>\n\n";
markup += "<hr\\>"
markup += " <directoryname>" + dirInfo["directoryname"] + "</directoryname>\n\n";
markup += " <button type=\"button\" onclick=\"back()\">Previous Directory</button> "
let files = dirInfo["files"];
let filenames = Object.keys(files);
markup += " <files>\n";
/**
* This markup creates headers headers for the html
*/
markup += "<headers>";
markup += "<filename class =\"header\" onclick=\"hide('filename')\">File Name</filename>";
markup += "<type class =\"header\" onclick=\"hide('type')\">Type</type>";
markup += "<size class =\"header\"onclick=\"hide('size')\">Size</size>";
markup += "<atime class =\"header\"onclick=\"hide('atime')\">Access Time</atime>";
markup += "<mtime class =\"header\"onclick=\"hide('mtime')\">Modification Time</mtime>";
markup += "<ctime class =\"header\"onclick=\"hide('ctime')\">Creation Time</ctime>";
markup += "<birthtime class =\"header\"onclick=\"hide('birthtime')\">Birth Time</birthtime>";
markup += "</headers>";
for (let f = 0; f < filenames.length; ++f) {
let fileInfo = files[filenames[f]];
markup += fileInfo2Markup(fileInfo);
}
markup += "\n </files>\n";
markup += "\n </dirinfo>\n";
return markup;
}
/* module.exports is only needed server-side for node.js */
if (typeof module !== "undefined" &&
typeof module.exports !== "undefined") {
module.exports = {
"fileInfo_keys": fileInfo_keys,
"dirInfo_keys": dirInfo_keys,
"getDirInfo": getDirInfo,
"fileInfo2Markup": fileInfo2Markup,
"dirInfo2Markup": dirInfo2Markup
};
}