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
This commit is contained in:
jabuxas 2024-10-15 15:14:22 -03:00
parent 1bd55061bf
commit 030196cf51

View File

@ -201,7 +201,7 @@ func (app *Application) formHandler(w http.ResponseWriter, r *http.Request) {
} }
defer file.Close() defer file.Close()
url := app.publicURL(file, ".txt") filename := app.publicURL(file, ".txt")
// reopening file because hash consumes it // reopening file because hash consumes it
file, err = os.Open("/tmp/file.txt") 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, "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) { 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() 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 // reopen the file for copying, as the hash process consumed the file reader
file, _, err = r.FormFile("file") 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, "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 { 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 app.lastUploadedFile = filepath
return fmt.Sprintf("http://%s/%s\n", app.url, filename) return filename
} }