forked from vectorgraphics/asymptoteWebApplication
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverUtil.js
107 lines (92 loc) · 3.16 KB
/
serverUtil.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
const fs = require('fs');
const SHA1 = require("crypto-js/sha1");
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% usrDirMgr
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
const usrDirMgr = function(req, serverDir){
const usrDirName = SHA1(req.connection.remoteAddress);
return {
usrDirName: usrDirName + "",
usrDirPath: '/' + usrDirName,
usrRelDirPath: '/clients/' + usrDirName,
usrAbsDirPath: serverDir + '/clients/' + usrDirName,
}
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% writePing
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
const writePing = function(dir) {
const pingFilePath = dir + "/ping";
fs.writeFileSync(pingFilePath, "", (err) => {
if(err) {
console.log("error in writing ping file");
}
});
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% makeDir
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
const makeDir = function(dir) {
fs.mkdirSync(dir);
writePing(dir);
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% dirCheck
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
const dirCheck = function(req, dirname) {
const dest = usrDirMgr(req, dirname);
if(!fs.existsSync(dest.usrAbsDirPath)) {
makeDir(dest.usrAbsDirPath);
}
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% dateTime
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
const dateTime = function(){
const dateObject = new Date();
const year = dateObject.getFullYear();
const month = dateObject.getMonth() + 1;
const day = dateObject.getDate();
const hour = dateObject.getHours();
const minute = dateObject.getMinutes();
const second = dateObject.getSeconds();
const date = year + "/" + month + "/" + day;
const time = hour + ":" + minute + ":" + second;
return {
date: date,
time: time
}
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% removeDir
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
const removeDir = function(path){
if (fs.existsSync(path)){
const files = fs.readdirSync(path)
if (files.length > 0){
files.forEach(function(filename){
if (fs.statSync(path + "/" + filename).isDirectory()){
removeDir(path + "/" + filename)
} else {
fs.unlinkSync(path + "/" + filename)
}
})
fs.rmdirSync(path)
}else{
fs.rmdirSync(path)
}
} else {
console.log("Directory path not found.")
}
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% encode
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
const encode = function (r) {
const esc = function (s) {
return (s+"").toString().replace(/:/g,":|")+"::";
}
return Object.values(r).map(esc).reduce((sum, x) => sum+x);
}
module.exports = {
usrDirMgr: usrDirMgr,
dirCheck: dirCheck,
removeDir: removeDir,
makeDir: makeDir,
writePing: writePing,
dateTime: dateTime,
encode:encode
}