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 {
h.logErr.Println("failed to parse form:", err)
http.Error(w, "request too big", http.StatusExpectationFailed)
http.Error(w, err.Error(), http.StatusExpectationFailed)
return
}
@ -119,6 +119,7 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
if err != nil {
h.logErr.Println("failed to open incoming file:", err)
w.WriteHeader(http.StatusInternalServerError)
http.Error(w, "cannot read incoming file", http.StatusInternalServerError)
return
}
defer func() {
@ -134,15 +135,15 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
if err := compiledTemplates["nospace"].Execute(w, &NotFoundData{
MainSite: utils.MainSite(r.Host),
}); err != nil {
w.WriteHeader(http.StatusInternalServerError)
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()
if _, err := io.Copy(s256, f); err != nil {
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
}
@ -159,7 +160,7 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
fDst, err := os.Create(fPath)
if err != nil {
h.logErr.Println("failed to open file for writing", err)
w.WriteHeader(http.StatusInternalServerError)
http.Error(w, "cannot create your file", http.StatusInternalServerError)
return
}
defer fDst.Close()
@ -175,7 +176,7 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
_, err = io.Copy(fDst, f)
if err != nil {
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
}
@ -195,8 +196,7 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.UserAgent(), "curl") {
_, scheme := utils.NetworkType(r.Host)
downloadURL = fmt.Sprint(scheme, "://", r.Host, downloadURLParsed, "\r\n")
w.Write([]byte(downloadURL))
w.Write([]byte(scheme + "://" + r.Host + downloadURLParsed.String() + "\r\n"))
return
}
@ -205,8 +205,8 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
DownloadURL: downloadURLParsed.String(),
KeepForHours: h.conf.Uploads.Limits.KeepForHours,
}); err != nil {
w.WriteHeader(http.StatusInternalServerError)
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)
}
}