Done
Anirudh Oppiliappan x@icyphox.sh
Mon, 07 Jun 2021 08:44:45 +0530
2 files changed,
37 insertions(+),
27 deletions(-)
M
index.html
→
index.html
@@ -9,11 +9,14 @@ </head>
<body> <form enctype="multipart/form-data" - action="http://localhost:9393/" + action="http://localhost:4040" method="post" > + <p>file:</p> <input type="file" name="file" /> <input type="submit" value="upload" /> + <p>key:</p> + <input type="password" name="key" /> </form> </body> </html>
M
main.go
→
main.go
@@ -30,35 +30,42 @@ return string(b)
} func (s *settings) uploadFile(w http.ResponseWriter, r *http.Request) { - key := r.FormValue("key") - fmt.Println(key) - if key != s.key { - fmt.Fprintf(w, "incorrect key") - log.Printf("incorrect key: %+v", key) - return - } - r.ParseMultipartForm(10 << 20) - file, handler, err := r.FormFile("file") - if err != nil { - log.Println(err) - return - } - defer file.Close() - log.Printf("file: %+v\t%+v bytes", handler.Filename, handler.Size) + switch r.Method { + case "POST": + key := r.FormValue("key") + fmt.Println(key) + if key != s.key { + fmt.Fprintf(w, "incorrect key") + log.Printf("incorrect key: %+v", key) + return + } + r.ParseMultipartForm(10 << 20) + file, handler, err := r.FormFile("file") + if err != nil { + log.Println(err) + return + } + defer file.Close() + log.Printf("file: %+v\t%+v bytes", handler.Filename, handler.Size) - ext := filepath.Ext(handler.Filename) - fileBytes, err := io.ReadAll(file) - if err != nil { - log.Println(err) - } + ext := filepath.Ext(handler.Filename) + fileBytes, err := io.ReadAll(file) + if err != nil { + log.Println(err) + } - newFile := randName(5) + ext - diskFile := filepath.Join(s.storepath, newFile) - os.WriteFile(diskFile, fileBytes, 0644) - log.Printf("wrote: %+v", diskFile) + newFile := randName(5) + ext + diskFile := filepath.Join(s.storepath, newFile) + os.WriteFile(diskFile, fileBytes, 0644) + log.Printf("wrote: %+v", diskFile) - fileUrl := s.url + "/" + newFile - fmt.Fprintf(w, "%v", fileUrl) + fileUrl := s.url + "/" + newFile + fmt.Fprintf(w, "%v", fileUrl) + case "GET": + http.ServeFile(w, r, "index.html") + default: + fmt.Fprintf(w, "unsupported method") + } } func (s *settings) readSettings() {