-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaltlocalwithtemp.js
161 lines (122 loc) · 4.58 KB
/
altlocalwithtemp.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
const http = require("http");
const { exec } = require('node:child_process');
const execSync = require('child_process').execSync;
const fs = require('fs').promises;
const host = '0.0.0.0';
const port = 8002;
const shelljs = require('shelljs');
var serverip = "localhost:8000";
const requestListener = function (req, res) {
//res.writeHead(200);
console.log("connection to local server got!");
console.log(req.url);
rawanted=req.url;
wanted=rawanted.slice(1);
oldwanted=rawanted.slice(1);
if(wanted.charAt(wanted.length-1)=="/"){
wanted=wanted+"index.html";
}
wantedlist=wanted.split("/");
/*
try{
//mkdirerr = execSync("mkdir /home/"+process.env.USER+"/localcache/"+arguments[0]);
directory = "/home/"+process.env.USER+"/localcache/"+wanted;
createcommand= ("mkdir -p \"$(dirname \"" + directory + "\")\" && touch \"" + directory + "\"");
console.log(createcommand);
execSync(createcommand);
}catch(error){
console.log("cant create directory");
}
*/
//rmerr = execSync("rm -rf /home/"+process.env.USER+"/localcache/"+wanted);
//mkdirerr = execSync("mkdir /home/"+process.env.USER+"/localcache/"+wanted);
console.log("checker a");
try{
console.log("checker b");
filePath="/home/"+process.env.USER+"/localcache/"+wanted;
console.log(filePath);
var teststuff=execSync("cat "+filePath+ " > /dev/null");
fs.readFile(filePath).then(contents => {
console.log("sending: "+filePath);
res.writeHead(200);
res.end(contents);
})
}catch(error){
try{
//mkdirerr = execSync("mkdir /home/"+process.env.USER+"/localcache/"+arguments[0]);
directory = "/home/"+process.env.USER+"/localcache/"+wanted;
createcommand= ("mkdir -p \"$(dirname \"" + directory + "\")\" && touch \"" + directory + "\"");
console.log(createcommand);
execSync(createcommand);
}catch(error){
console.log("cant create directory");
}
console.log("checker c");
try{
console.log("checker d");
var command=("wget -O /home/"+process.env.USER+"/localcache/"+wanted+".7z "+serverip+"/"+oldwanted);
console.log(command);
wgeterr = execSync(command);
console.log("checker x");
decommand=("7z x /home/"+process.env.USER+"/localcache/"+wanted+".7z -o"+"/home/"+process.env.USER+"/localtemp/"+wanted);
console.log(decommand);
decompress=execSync(decommand);
//move from temp folder to cache
movecommand=shelljs.mv("/home/"+process.env.USER+"/localtemp/"+wanted+'/'+wantedlist[wantedlist.length-1], "/home/"+process.env.USER+"/localcache/"+wanted);
console.log("movecommand error: "+movecommand);
console.log("checker y");
filePath="/home/"+process.env.USER+"/localcache/"+wanted;
prevpath="";
for(let i=0;i<wantedlist.length-1;i++){
prevpath=prevpath+wantedlist[i]+"/"
}
try{
console.log("trying to fix the / for the second time geez");
//secondreplace=shelljs.sed('-i', 'href="','href='+prevpath, filePath);
}catch(sus){
console.log(secondreplace);
}
console.log("the program progressed after / replacement");
//console.log(secondreplace);
console.log(filePath);
try{
console.log("Trying to cat the file to see if it actually exists lol");
//caterror=execSync("cat "+filePath);
}catch (e){
console.log("amogus");
filePath="/home/"+process.env.USER+"/localcache/"+wanted;
}
try{
console.log("woohoo trying to send full send it yeah (I am not affiliated)");
fs.readFile(filePath).then(contents => {
console.log("sending: "+filePath);
res.writeHead(200);
res.end(contents);
console.log("checker z");
})
}catch(thingbad){
console.log("cant send very bad");
res.writeHead(404);
res.end("Resource not found");
}
}catch(err){
console.log("checker e");
res.writeHead(404);
}
}
//sziperror = execSync("7z a /home/"+process.env.USER+"/localcache/"+wanted+".7z /home/"+process.env.USER+"/localcache/"+wanted );
//ilePath="/home/"+process.env.USER+"/localcache/"+wanted+".7z"
//console.log(filePath);
/*
fs.readFile(filePath, function(error, content) {
console.log("A");
res.writeHead(200, { 'Content-Type': "application/x-7z-compressed"});
console.log(filePath);
res.end(content, 'utf-8');
})
*/
};
const server = http.createServer(requestListener);
server.listen(port, host, () => {
console.log(`Server is running on http://${host}:${port}`);
});