Skip to content

Commit d6bc7ce

Browse files
authored
Merge pull request #16 from J-P-S-O/master
Master
2 parents 02adf0c + 34debec commit d6bc7ce

4 files changed

Lines changed: 87 additions & 58 deletions

File tree

package-lock.json

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"dependencies":{"-":"^0.0.1","chalk":"^5.0.0","g":"^2.0.1","hyperspace":"^3.19.0","istextorbinary":"^6.0.0","prompt-sync":"^4.2.0"}}
1+
{"dependencies":{"-":"^0.0.1","chalk":"^5.0.0","g":"^2.0.1","hyperspace":"^3.19.0","istextorbinary":"^6.0.0","mime-types":"^2.1.34","prompt-sync":"^4.2.0"}}

run.js

Lines changed: 43 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,32 @@ With love,
66
*/
77

88

9-
9+
let mime = require('mime-types')
1010
let prompt = require("prompt-sync")()
1111
let fs = require("fs")
1212
let log = console.log;
1313
let http = require("http")
1414
let crypto = require("crypto")
15-
const { isText } = require('istextorbinary')
16-
15+
let path = require("path")
1716

1817

1918

20-
if (fs.existsSync("data")) {
21-
console.log("\x1b[31m WARNING: Folder \"data\" will be erased and rebuilt if you proceed \x1b[37m")
19+
console.log("\x1b[31m WARNING: All files in /static/ matching \x1b[34m *.upload.* \x1b[31m will be removed \x1b[37m")
20+
2221
const answer = prompt("Would you like to proceed? (S/N)")
2322
if (answer.toUpperCase() =="S"){
24-
log("removing")
25-
fs.rmdirSync("data",{recursive: true})
23+
//log("removing")
24+
if(fs.existsSync("static")){
25+
let filess = fs.readdirSync("./static")
26+
for (file in filess){
27+
28+
//console.log(String(filess[file]))
29+
if (String(filess[file]).match(/\.*\.upload\.*/)){
30+
console.log("Removing "+"\x1b[32m"+ String(filess[file]) + "\x1b[37m")
31+
fs.unlinkSync(path.join(__dirname,"static", filess[file]))
32+
}
33+
}
34+
}
2635
console.log("Done!")
2736
console.log("Now we can start the HTTP server lol")
2837

@@ -33,10 +42,8 @@ if (fs.existsSync("data")) {
3342
console.log("Not a valid answer. Exiting...")
3443
process.exit(1)
3544
}
36-
}
45+
3746

38-
fs.mkdirSync("./data");
39-
fs.mkdirSync("./data/keys")
4047

4148
const requestListener = function (req, res) {
4249
let intcode = "" + crypto.randomInt(999999);
@@ -86,34 +93,17 @@ const requestListener = function (req, res) {
8693
req.on("end",()=>{
8794
res.writeHead(200, { "Content-Type": "text/plain" });
8895
res.end("Success: your code is "+ intcode);
89-
90-
91-
intcode = "./data/keys/" + intcode
96+
//console.log(req.headers)
97+
let type = req.headers["content-type"]
98+
let ext = mime.extension(type)
99+
let code = intcode
100+
intcode = "./static/" + intcode+".upload."+ext
92101
fs.writeFileSync(intcode,body)
93-
94-
95-
/*fs.readFile(intcode, 'utf8', function(err, data){
96-
97-
if (err){ throw err; }
98-
var lines = data.split('\n')
99-
var type = lines[2]
100-
type = type.replace("Content-Type: ","")
101-
//log(type)
102-
103-
lines = lines.slice(3).join('\n');
104-
lines = lines.split("\n")
105-
let i = 0
106-
while (i < 6){
102+
107103

108-
lines.splice(-1)
109-
i++
110-
}
111-
lines = lines.join('\n')
112-
//console.log(lines)
113-
/* fs.writeFileSync(intcode, lines); */
114-
//fs.writeFileSync(intcode+".type",type)
104+
115105

116-
console.log("File uploaded to "+"\x1b[32m"+intcode.replace("./data/keys/","")+"\x1b[37m" +" Type: " /*+ type*/)
106+
console.log("File uploaded to "+"\x1b[32m"+code+"\x1b[37m" +" Type: " + type)
117107

118108
/*})*/;
119109

@@ -132,36 +122,33 @@ console.log("File uploaded to "+"\x1b[32m"+intcode.replace("./data/keys/","")+"\
132122
});
133123

134124
} else if(String(req.url).startsWith("/download/") ){
135-
//log(String(req.url).replace("/download/",""))
136-
let pathh = (String(req.url).replace("/download/",""))
137-
pathh = "./data/keys/" + pathh + ".type"
138-
fs.readFile("./data/keys/" + String(req.url).replace("/download/",""), function (err,data) {
125+
let number = String(req.url).replace("/download/","")
126+
let filepath = new RegExp(number+"\.upload\."+"*")
127+
let defpath = "undefined"
128+
let files = fs.readdirSync("static")
129+
console.log(filepath)
130+
for (file in files){
131+
console.log(files[file])
132+
133+
if (String(files[file]).match(filepath)){
134+
defpath = String(files[file])
135+
console.log(defpath)
136+
}
137+
}
138+
fs.readFile(defpath, function(err,data) {
139139
if (err) {
140-
141-
if(err.code=="ENOENT"){
142-
143-
fs.readFile("templates/404.html", function (err,data) {
144-
if (err) {
145-
res.writeHead(203);
146-
res.end("<html><body><b>internal error</b></body></html>"+JSON.stringify(err));
147-
return;
148-
}
149-
140+
if(err.code=="ENOENT"){
150141
res.writeHead(404);
151142
res.end(data);
152143
return;
153-
})
154-
return;
155-
}
156-
144+
}
157145
res.writeHead(203);
158146
res.end(JSON.stringify(err));
159147
return;
160148
}
161-
res.setHeader("Content-Type", pathh)
149+
res.setHeader("Content-Type", "text/plain")
162150
res.writeHead(200);
163-
164-
res.end(data);
151+
res.end(defpath);
165152
console.log("File downloaded from "+"\x1b[32m"+String(req.url).replace("/download/","")+"\x1b[37m")
166153
});
167154
}else{

static/start.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,16 @@ upbutton.onchange = function(e){
3434

3535
}
3636
dbutton.onclick =function(){
37-
window.location = "/download/"+codeplace.value
37+
let xhr = new XMLHttpRequest()
38+
xhr.open('GET', "/download/"+codeplace.value)
39+
40+
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
41+
xhr.send()
42+
xhr.onload = () => {
43+
window.location = xhr.responseText;
44+
45+
}
46+
3847
}
3948

4049

0 commit comments

Comments
 (0)