@@ -6,23 +6,25 @@ With love,
66*/
77
88
9-
9+ let mime = require ( 'mime-types' )
1010let prompt = require ( "prompt-sync" ) ( )
1111let fs = require ( "fs" )
1212let log = console . log ;
1313let http = require ( "http" )
1414let 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 ( / \. * \. u p l o a d \. * / ) ) {
44+ console . log ( "Removing " + "\x1b[32m" + String ( filess [ file ] ) + "\x1b[37m" )
45+ fs . unlinkSync ( path . join ( __dirname , "static" , filess [ file ] ) )
46+ }
3647}
3748
3849fs . 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