From e87382bc777a4fa87b71599ffcb32b7ac2cb6320 Mon Sep 17 00:00:00 2001 From: jabuxas Date: Tue, 15 Oct 2024 13:33:18 -0300 Subject: [PATCH] wip: add upload box --- abyss.go | 2 ++ handlers.go | 22 ++++++++++++++ templates/upload.html | 69 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 templates/upload.html diff --git a/abyss.go b/abyss.go index bb89e6a..163037a 100644 --- a/abyss.go +++ b/abyss.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "log" "log/slog" "net/http" @@ -65,6 +66,7 @@ func main() { ), ) mux.HandleFunc("/last", app.lastHandler) + mux.HandleFunc("/upload", app.basicAuth(app.uploadButtonHandler)) srv := &http.Server{ Addr: app.port, diff --git a/handlers.go b/handlers.go index 9bd687d..eac682f 100644 --- a/handlers.go +++ b/handlers.go @@ -149,10 +149,32 @@ func (app *Application) uploadCurlHandler(w http.ResponseWriter, r *http.Request app.uploadHandler(w, r) } +func (app *Application) uploadButtonHandler(w http.ResponseWriter, r *http.Request) { + + if r.Method == http.MethodPost { + app.uploadHandler(w, r) + return + } + + tmpl := template.Must(template.ParseFiles("templates/upload.html")) + if err := tmpl.Execute(w, app.uploadHandler); err != nil { + slog.Warn(error.Error(err)) + } +} + func (app *Application) uploadHandler(w http.ResponseWriter, r *http.Request) { + + // if contentType := r.Header.Get("Content-Type"); contentType == "application/x-www-form-urlencoded" { + // content := r.FormValue("content") + // } else if contentType == "multipart/form-data" { + // } +} + +func (app *Application) formFileHandler(w http.ResponseWriter, r *http.Request) { file, handler, err := r.FormFile("file") if err != nil { http.Error(w, "Error retrieving the file", http.StatusBadRequest) + slog.Warn(error.Error(err)) return } defer file.Close() diff --git a/templates/upload.html b/templates/upload.html new file mode 100644 index 0000000..0cc9036 --- /dev/null +++ b/templates/upload.html @@ -0,0 +1,69 @@ + + + + + + + dir listing + + + + +
+

+ +
+ + +