1
0
Fork 0

A little refactoring in handlers.go.

This commit is contained in:
Alexander Andreev 2023-08-05 04:46:25 +04:00
parent df3c5e0678
commit c2278fdd2e
Signed by: Arav
GPG Key ID: D22A817D95815393
1 changed files with 9 additions and 10 deletions

View File

@ -116,8 +116,7 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
fDst.Write([]byte{0})
fDst.Seek(0, io.SeekStart)
_, err = io.Copy(fDst, f)
if err != nil {
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.")
return
@ -125,8 +124,8 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
typ, _ := utils.NetworkType(r.Host)
h.logFile.Printf("| up | %s | %s | %s | SHA256 %s | %s | %d | %s", r.Header.Get("X-Real-IP"), typ,
fHandler.Filename, fHash, fSaltedHash, fHandler.Size, r.UserAgent())
h.logFile.Printf("| up | %s | %s | %s | SHA256 %s | %s | %d | %s",
r.Header.Get("X-Real-IP"), typ, fHandler.Filename, fHash, fSaltedHash, fHandler.Size, r.UserAgent())
w.WriteHeader(http.StatusCreated)
} else {
@ -172,7 +171,8 @@ func (h *UploadHandlers) Download(w http.ResponseWriter, r *http.Request) {
netTyp, _ := utils.NetworkType(r.Host)
h.logFile.Printf("| dw | %s | %s | %s | %s | %s", r.Header.Get("X-Real-IP"), netTyp, name, saltedHash, r.UserAgent())
h.logFile.Printf("| dw | %s | %s | %s | %s | %s",
r.Header.Get("X-Real-IP"), netTyp, name, saltedHash, r.UserAgent())
http.ServeContent(w, r, path, stat.ModTime(), fd)
}
@ -188,14 +188,12 @@ func (h *UploadHandlers) Delete(w http.ResponseWriter, r *http.Request) {
path := path.Join(h.uploadDir, saltedHash)
_, err := os.Stat(path)
if os.IsNotExist(err) {
if _, err := os.Stat(path); os.IsNotExist(err) {
Error(w, r, http.StatusNotFound, "")
return
}
err = os.Remove(path)
if err != nil {
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.")
return
@ -203,7 +201,8 @@ func (h *UploadHandlers) Delete(w http.ResponseWriter, r *http.Request) {
netTyp, _ := utils.NetworkType(r.Host)
h.logFile.Printf("| dt | %s | %s | %s | %s", r.Header.Get("X-Real-IP"), netTyp, saltedHash, r.UserAgent())
h.logFile.Printf("| dt | %s | %s | %s | %s",
r.Header.Get("X-Real-IP"), netTyp, saltedHash, r.UserAgent())
if strings.Contains(r.UserAgent(), "curl") || strings.Contains(r.UserAgent(), "Wget") {
fmt.Fprintln(w, "File was successfully deleted.")