Changed handlers.Error() signature to be like http.Error()'s one.
This commit is contained in:
parent
6106e817cf
commit
4048390bfb
@ -64,7 +64,7 @@ func main() {
|
|||||||
r := httpr.New()
|
r := httpr.New()
|
||||||
|
|
||||||
r.NotFoundHandler = func(w http.ResponseWriter, r *http.Request) {
|
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)
|
r.Handler(http.MethodGet, "/", hand.Index)
|
||||||
|
@ -62,14 +62,14 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
if err := r.ParseMultipartForm(fMaxSizeBytes); err != nil {
|
if err := r.ParseMultipartForm(fMaxSizeBytes); err != nil {
|
||||||
log.Println("failed to parse upload form:", err)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
f, fHandler, err := r.FormFile("file")
|
f, fHandler, err := r.FormFile("file")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("failed to open incoming file:", err)
|
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
|
return
|
||||||
}
|
}
|
||||||
defer os.Remove(fHandler.Filename)
|
defer os.Remove(fHandler.Filename)
|
||||||
@ -79,14 +79,14 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
if leftSpace < fHandler.Size {
|
if leftSpace < fHandler.Size {
|
||||||
log.Println("not enough space left in storage, only", leftSpace>>20, "MiB left")
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
s256 := sha256.New()
|
s256 := sha256.New()
|
||||||
if _, err := io.Copy(s256, f); err != nil {
|
if _, err := io.Copy(s256, f); err != nil {
|
||||||
log.Println("failed to compute a SHA-256 hash:", err)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,7 +103,7 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
|
|||||||
fDst, err := os.Create(fPath)
|
fDst, err := os.Create(fPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("failed to open file for writing", err)
|
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
|
return
|
||||||
}
|
}
|
||||||
defer fDst.Close()
|
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 {
|
if _, err = io.Copy(fDst, f); err != nil {
|
||||||
log.Println("failed to copy uploaded file to destination:", err)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -157,7 +157,7 @@ func (h *UploadHandlers) Download(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
stat, err := os.Stat(path)
|
stat, err := os.Stat(path)
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
Error(w, r, http.StatusNotFound, "")
|
Error(w, r, "", http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -168,7 +168,7 @@ func (h *UploadHandlers) Download(w http.ResponseWriter, r *http.Request) {
|
|||||||
fd, err := os.Open(path)
|
fd, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("failed to open file to read:", err)
|
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
|
return
|
||||||
}
|
}
|
||||||
defer fd.Close()
|
defer fd.Close()
|
||||||
@ -197,13 +197,13 @@ func (h *UploadHandlers) Delete(w http.ResponseWriter, r *http.Request) {
|
|||||||
path := path.Join(h.uploadDir, saltedHash)
|
path := path.Join(h.uploadDir, saltedHash)
|
||||||
|
|
||||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||||
Error(w, r, http.StatusNotFound, "")
|
Error(w, r, "", http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := os.Remove(path); err != nil {
|
if err := os.Remove(path); err != nil {
|
||||||
log.Println("failed to remove a file:", err)
|
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
|
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") {
|
if strings.Contains(r.UserAgent(), "curl") || strings.Contains(r.UserAgent(), "Wget") {
|
||||||
http.Error(w, reason, code)
|
http.Error(w, reason, code)
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user