From 1bd55061bfb935e02d6ed2b862ae8aa0ae7f827c Mon Sep 17 00:00:00 2001 From: jabuxas Date: Tue, 15 Oct 2024 15:08:27 -0300 Subject: [PATCH] feat: add form for uploading --- handlers.go | 139 +++++++++++++++++++++++++++++----------------------- helpers.go | 14 ++++++ 2 files changed, 93 insertions(+), 60 deletions(-) diff --git a/handlers.go b/handlers.go index 2584e7f..6657188 100644 --- a/handlers.go +++ b/handlers.go @@ -69,7 +69,7 @@ func (app *Application) fileListingHandler(w http.ResponseWriter, r *http.Reques URL: app.url, } if err := tmpl.Execute(w, templateData); err != nil { - slog.Warn(error.Error(err)) + slog.Warn(err.Error()) } } @@ -142,7 +142,7 @@ func (app *Application) uploadHandler(w http.ResponseWriter, r *http.Request) { tmpl := template.Must(template.ParseFiles("templates/upload.html")) if err := tmpl.Execute(w, app.uploadHandler); err != nil { - slog.Warn(error.Error(err)) + slog.Warn(err.Error()) } } @@ -161,64 +161,6 @@ func (app *Application) parserHandler(w http.ResponseWriter, r *http.Request) { } } -func (app *Application) formHandler(w http.ResponseWriter, r *http.Request) { - // content := r.FormValue("content") - // - // app.saveFile(w, r, file, ".txt", "content") -} - -func (app *Application) curlHandler(w http.ResponseWriter, r *http.Request) { - if r.URL.Path != "/" { - http.Error(w, "Method not allowed", http.StatusUnauthorized) - return - } - - if !CheckAuth(r, app.key) { - http.Error(w, "You're not authorized.", http.StatusBadRequest) - return - } - - 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() - - url := app.publicURL(file, filepath.Ext(handler.Filename)) - - // reopen the file for copying, as the hash process consumed the file reader - file, _, err = r.FormFile("file") - if err != nil { - http.Error(w, "Error retrieving the file", http.StatusBadRequest) - return - } - defer file.Close() - - dst, err := os.Create(app.lastUploadedFile) - if err != nil { - http.Error(w, "Error creating file\n", http.StatusInternalServerError) - } - defer dst.Close() - - if _, err := io.Copy(dst, file); err != nil { - http.Error(w, "Error copying the file", http.StatusInternalServerError) - } - - fmt.Fprintf(w, "%s", url) -} - -func (app *Application) publicURL(file io.Reader, extension string) string { - filename, _ := HashFile(file, extension) - - filepath := filepath.Join(app.filesDir, filename) - - app.lastUploadedFile = filepath - - return fmt.Sprintf("http://%s/%s\n", app.url, filename) -} - func (app *Application) basicAuth(next http.HandlerFunc) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { username, password, ok := r.BasicAuth() @@ -245,3 +187,80 @@ func (app *Application) basicAuth(next http.HandlerFunc) http.HandlerFunc { http.Error(w, "Unauthorized", http.StatusUnauthorized) }) } + +func (app *Application) formHandler(w http.ResponseWriter, r *http.Request) { + content := r.FormValue("content") + + if err := os.WriteFile("/tmp/file.txt", []byte(content), 0666); err != nil { + http.Error(w, "Couldn't parse content body", http.StatusNoContent) + } + + file, err := os.Open("/tmp/file.txt") + if err != nil { + http.Error(w, "Couldn't find file", http.StatusNotFound) + } + defer file.Close() + + url := app.publicURL(file, ".txt") + + // reopening file because hash consumes it + file, err = os.Open("/tmp/file.txt") + if err != nil { + http.Error(w, "Couldn't find file", http.StatusNotFound) + } + defer file.Close() + + err = SaveFile(app.lastUploadedFile, file) + if err != nil { + fmt.Fprintf(w, "Error parsing file: %s", err.Error()) + } + + fmt.Fprintf(w, "%s", url) +} + +func (app *Application) curlHandler(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/" { + http.Error(w, "Method not allowed", http.StatusUnauthorized) + return + } + + if !CheckAuth(r, app.key) { + http.Error(w, "You're not authorized.", http.StatusBadRequest) + return + } + + file, handler, err := r.FormFile("file") + if err != nil { + http.Error(w, "Error retrieving the file", http.StatusBadRequest) + slog.Warn(err.Error()) + return + } + defer file.Close() + + url := app.publicURL(file, filepath.Ext(handler.Filename)) + + // reopen the file for copying, as the hash process consumed the file reader + file, _, err = r.FormFile("file") + if err != nil { + http.Error(w, "Error retrieving the file", http.StatusBadRequest) + return + } + defer file.Close() + + err = SaveFile(app.lastUploadedFile, file) + if err != nil { + fmt.Fprintf(w, "Error parsing file: %s", err.Error()) + } + + fmt.Fprintf(w, "%s", url) +} + +func (app *Application) publicURL(file io.Reader, extension string) string { + filename, _ := HashFile(file, extension) + + filepath := filepath.Join(app.filesDir, filename) + + app.lastUploadedFile = filepath + + return fmt.Sprintf("http://%s/%s\n", app.url, filename) +} diff --git a/helpers.go b/helpers.go index 8169b35..0cccdb2 100644 --- a/helpers.go +++ b/helpers.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "net/http" + "os" ) func CheckAuth(r *http.Request, key string) bool { @@ -35,3 +36,16 @@ func HashFile(file io.Reader, extension string) (string, error) { return filename, nil } + +func SaveFile(name string, file io.Reader) error { + dst, err := os.Create(name) + if err != nil { + return err + } + defer dst.Close() + + if _, err := io.Copy(dst, file); err != nil { + return err + } + return nil +}