1
0

Better error messages. Now you will not loose your uploaded file if I somehow fuck the template up.

This commit is contained in:
Alexander Andreev 2022-03-29 18:58:35 +04:00
parent b49f863abf
commit f74576e3ce
Signed by: Arav
GPG Key ID: 1327FE8A374CC86F

View File

@ -111,7 +111,7 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
if err := r.ParseMultipartForm(fMaxSizeBytes); err != nil { if err := r.ParseMultipartForm(fMaxSizeBytes); err != nil {
h.logErr.Println("failed to parse form:", err) h.logErr.Println("failed to parse form:", err)
http.Error(w, "request too big", http.StatusExpectationFailed) http.Error(w, err.Error(), http.StatusExpectationFailed)
return return
} }
@ -119,6 +119,7 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
h.logErr.Println("failed to open incoming file:", err) h.logErr.Println("failed to open incoming file:", err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
http.Error(w, "cannot read incoming file", http.StatusInternalServerError)
return return
} }
defer func() { defer func() {
@ -134,15 +135,15 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
if err := compiledTemplates["nospace"].Execute(w, &NotFoundData{ if err := compiledTemplates["nospace"].Execute(w, &NotFoundData{
MainSite: utils.MainSite(r.Host), MainSite: utils.MainSite(r.Host),
}); err != nil { }); err != nil {
w.WriteHeader(http.StatusInternalServerError)
h.logErr.Fatalln("failed to execute NoSpace template:", err) h.logErr.Fatalln("failed to execute NoSpace template:", err)
http.Error(w, "cannot execute a NoSpace template. But error was that there's no space for uploads left", http.StatusInternalServerError)
} }
} }
s256 := sha256.New() s256 := sha256.New()
if _, err := io.Copy(s256, f); err != nil { if _, err := io.Copy(s256, f); err != nil {
h.logErr.Println("failed to compute SHA-256 hash:", err) h.logErr.Println("failed to compute SHA-256 hash:", err)
w.WriteHeader(http.StatusInternalServerError) http.Error(w, "cannot compute hash for a file", http.StatusInternalServerError)
return return
} }
@ -159,7 +160,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 {
h.logErr.Println("failed to open file for writing", err) h.logErr.Println("failed to open file for writing", err)
w.WriteHeader(http.StatusInternalServerError) http.Error(w, "cannot create your file", http.StatusInternalServerError)
return return
} }
defer fDst.Close() defer fDst.Close()
@ -175,7 +176,7 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
_, err = io.Copy(fDst, f) _, err = io.Copy(fDst, f)
if err != nil { if err != nil {
h.logErr.Println("failed to copy uploaded file to destination:", err) h.logErr.Println("failed to copy uploaded file to destination:", err)
w.WriteHeader(http.StatusInternalServerError) http.Error(w, "cannot copy file's content", http.StatusInternalServerError)
return return
} }
@ -195,8 +196,7 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.UserAgent(), "curl") { if strings.Contains(r.UserAgent(), "curl") {
_, scheme := utils.NetworkType(r.Host) _, scheme := utils.NetworkType(r.Host)
downloadURL = fmt.Sprint(scheme, "://", r.Host, downloadURLParsed, "\r\n") w.Write([]byte(scheme + "://" + r.Host + downloadURLParsed.String() + "\r\n"))
w.Write([]byte(downloadURL))
return return
} }
@ -205,8 +205,8 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
DownloadURL: downloadURLParsed.String(), DownloadURL: downloadURLParsed.String(),
KeepForHours: h.conf.Uploads.Limits.KeepForHours, KeepForHours: h.conf.Uploads.Limits.KeepForHours,
}); err != nil { }); err != nil {
w.WriteHeader(http.StatusInternalServerError)
h.logErr.Fatalln("failed to execute Uploaded template:", err) h.logErr.Fatalln("failed to execute Uploaded template:", err)
http.Error(w, "cannot execute Uploaded template, but here is your download link: "+downloadURLParsed.String(), http.StatusInternalServerError)
} }
} }