Skip to content

Commit 56dd22d

Browse files
committed
Add file removal, change data dir
1 parent 699f953 commit 56dd22d

3 files changed

Lines changed: 59 additions & 32 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: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,25 @@ 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: Folder \"data\" will be erased and rebuilt if you proceed \x1b[37m"
20+
+ "\n~Same will happen for all files in /static/ matching \x1b[34m *.upload.* \x1b[37m")
21+
2222
const answer = prompt("Would you like to proceed? (S/N)")
2323
if (answer.toUpperCase() =="S"){
24-
log("removing")
24+
//log("removing")
25+
if(fs.existsSync("data")){
2526
fs.rmdirSync("data",{recursive: true})
27+
}
2628
console.log("Done!")
2729
console.log("Now we can start the HTTP server lol")
2830

@@ -33,6 +35,15 @@ if (fs.existsSync("data")) {
3335
console.log("Not a valid answer. Exiting...")
3436
process.exit(1)
3537
}
38+
39+
let filess = fs.readdirSync("./static")
40+
for (file in filess){
41+
42+
//console.log(String(filess[file]))
43+
if (String(filess[file]).match(/\.*\.upload\.*/)){
44+
console.log("Removing "+"\x1b[32m"+ String(filess[file]) + "\x1b[37m")
45+
fs.unlinkSync(path.join(__dirname,"static", filess[file]))
46+
}
3647
}
3748

3849
fs.mkdirSync("./data");
@@ -86,34 +97,17 @@ const requestListener = function (req, res) {
8697
req.on("end",()=>{
8798
res.writeHead(200, { "Content-Type": "text/plain" });
8899
res.end("Success: your code is "+ intcode);
89-
90-
91-
intcode = "./data/keys/" + intcode
100+
//console.log(req.headers)
101+
let type = req.headers["content-type"]
102+
let ext = mime.extension(type)
103+
let code = intcode
104+
intcode = "./static/" + intcode+".upload."+ext
92105
fs.writeFileSync(intcode,body)
93-
106+
94107

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){
107-
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)
108+
115109

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

118112
/*})*/;
119113

0 commit comments

Comments
 (0)