From 030196cf51ab99f23b346a19bb3f339ab8a1b442 Mon Sep 17 00:00:00 2001 From: jabuxas Date: Tue, 15 Oct 2024 15:14:22 -0300 Subject: [PATCH] fix: drop over-reliance on app.lastUploadedFile It won't be good for future code maintainance if filepath for the actual file is dependant on a variable that gets updated when something is hashed --- handlers.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/handlers.go b/handlers.go index 6657188..14af44b 100644 --- a/handlers.go +++ b/handlers.go @@ -201,7 +201,7 @@ func (app *Application) formHandler(w http.ResponseWriter, r *http.Request) { } defer file.Close() - url := app.publicURL(file, ".txt") + filename := app.publicURL(file, ".txt") // reopening file because hash consumes it file, err = os.Open("/tmp/file.txt") @@ -215,7 +215,7 @@ func (app *Application) formHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Error parsing file: %s", err.Error()) } - fmt.Fprintf(w, "%s", url) + fmt.Fprintf(w, "%s", fmt.Sprintf("http://%s/%s\n", app.url, filename)) } func (app *Application) curlHandler(w http.ResponseWriter, r *http.Request) { @@ -237,7 +237,7 @@ func (app *Application) curlHandler(w http.ResponseWriter, r *http.Request) { } defer file.Close() - url := app.publicURL(file, filepath.Ext(handler.Filename)) + filename := 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") @@ -252,7 +252,7 @@ func (app *Application) curlHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Error parsing file: %s", err.Error()) } - fmt.Fprintf(w, "%s", url) + fmt.Fprintf(w, "%s", fmt.Sprintf("http://%s/%s\n", app.url, filename)) } func (app *Application) publicURL(file io.Reader, extension string) string { @@ -262,5 +262,5 @@ func (app *Application) publicURL(file io.Reader, extension string) string { app.lastUploadedFile = filepath - return fmt.Sprintf("http://%s/%s\n", app.url, filename) + return filename }