diff --git a/cmd/dwelling-upload/main.go b/cmd/dwelling-upload/main.go index fe58a60..c481748 100644 --- a/cmd/dwelling-upload/main.go +++ b/cmd/dwelling-upload/main.go @@ -64,7 +64,7 @@ func main() { r := httpr.New() r.NotFoundHandler = func(w http.ResponseWriter, r *http.Request) { - duihttp.Error(w, r, http.StatusNotFound, "") + duihttp.Error(w, r, "", http.StatusNotFound) } r.Handler(http.MethodGet, "/", hand.Index) diff --git a/internal/http/handlers.go b/internal/http/handlers.go index e8a4b3d..d8fa9eb 100644 --- a/internal/http/handlers.go +++ b/internal/http/handlers.go @@ -62,14 +62,14 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) { if err := r.ParseMultipartForm(fMaxSizeBytes); err != nil { log.Println("failed to parse upload form:", err) - Error(w, r, http.StatusExpectationFailed, "Failed to parse upload form.") + Error(w, r, "Failed to parse upload form.", http.StatusExpectationFailed) return } f, fHandler, err := r.FormFile("file") if err != nil { log.Println("failed to open incoming file:", err) - Error(w, r, http.StatusInternalServerError, "Error reading an incoming file.") + Error(w, r, "Error reading an incoming file.", http.StatusInternalServerError) return } defer os.Remove(fHandler.Filename) @@ -79,14 +79,14 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) { if leftSpace < fHandler.Size { log.Println("not enough space left in storage, only", leftSpace>>20, "MiB left") - Error(w, r, http.StatusInternalServerError, "Not enough space left, sorry.") + Error(w, r, "Not enough space left, sorry.", http.StatusInternalServerError) return } s256 := sha256.New() if _, err := io.Copy(s256, f); err != nil { log.Println("failed to compute a SHA-256 hash:", err) - Error(w, r, http.StatusInternalServerError, "A hash for the file cannot be computed.") + Error(w, r, "A hash for the file cannot be computed.", http.StatusInternalServerError) return } @@ -103,7 +103,7 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) { fDst, err := os.Create(fPath) if err != nil { log.Println("failed to open file for writing", err) - Error(w, r, http.StatusInternalServerError, "File cannot be written.") + Error(w, r, "File cannot be written.", http.StatusInternalServerError) return } defer fDst.Close() @@ -118,7 +118,7 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) { if _, err = io.Copy(fDst, f); err != nil { log.Println("failed to copy uploaded file to destination:", err) - Error(w, r, http.StatusInternalServerError, "Failed to copy uploaded file to the storage.") + Error(w, r, "Failed to copy uploaded file to the storage.", http.StatusInternalServerError) return } @@ -157,7 +157,7 @@ func (h *UploadHandlers) Download(w http.ResponseWriter, r *http.Request) { stat, err := os.Stat(path) if os.IsNotExist(err) { - Error(w, r, http.StatusNotFound, "") + Error(w, r, "", http.StatusNotFound) return } @@ -168,7 +168,7 @@ func (h *UploadHandlers) Download(w http.ResponseWriter, r *http.Request) { fd, err := os.Open(path) if err != nil { log.Println("failed to open file to read:", err) - Error(w, r, http.StatusInternalServerError, "Failed to open file to read.") + Error(w, r, "Failed to open file to read.", http.StatusInternalServerError) return } defer fd.Close() @@ -197,13 +197,13 @@ func (h *UploadHandlers) Delete(w http.ResponseWriter, r *http.Request) { path := path.Join(h.uploadDir, saltedHash) if _, err := os.Stat(path); os.IsNotExist(err) { - Error(w, r, http.StatusNotFound, "") + Error(w, r, "", http.StatusNotFound) return } if err := os.Remove(path); err != nil { log.Println("failed to remove a file:", err) - Error(w, r, http.StatusInternalServerError, "Failed to remove a file.") + Error(w, r, "Failed to remove a file.", http.StatusInternalServerError) return } @@ -223,7 +223,7 @@ func (h *UploadHandlers) Delete(w http.ResponseWriter, r *http.Request) { } } -func Error(w http.ResponseWriter, r *http.Request, code int, reason string) { +func Error(w http.ResponseWriter, r *http.Request, reason string, code int) { if strings.Contains(r.UserAgent(), "curl") || strings.Contains(r.UserAgent(), "Wget") { http.Error(w, reason, code) return