A little refactoring in handlers.go.
This commit is contained in:
parent
df3c5e0678
commit
c2278fdd2e
@ -116,8 +116,7 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
|
|||||||
fDst.Write([]byte{0})
|
fDst.Write([]byte{0})
|
||||||
fDst.Seek(0, io.SeekStart)
|
fDst.Seek(0, io.SeekStart)
|
||||||
|
|
||||||
_, err = io.Copy(fDst, f)
|
if _, err = io.Copy(fDst, f); err != nil {
|
||||||
if 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, http.StatusInternalServerError, "Failed to copy uploaded file to the storage.")
|
||||||
return
|
return
|
||||||
@ -125,8 +124,8 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
typ, _ := utils.NetworkType(r.Host)
|
typ, _ := utils.NetworkType(r.Host)
|
||||||
|
|
||||||
h.logFile.Printf("| up | %s | %s | %s | SHA256 %s | %s | %d | %s", r.Header.Get("X-Real-IP"), typ,
|
h.logFile.Printf("| up | %s | %s | %s | SHA256 %s | %s | %d | %s",
|
||||||
fHandler.Filename, fHash, fSaltedHash, fHandler.Size, r.UserAgent())
|
r.Header.Get("X-Real-IP"), typ, fHandler.Filename, fHash, fSaltedHash, fHandler.Size, r.UserAgent())
|
||||||
|
|
||||||
w.WriteHeader(http.StatusCreated)
|
w.WriteHeader(http.StatusCreated)
|
||||||
} else {
|
} else {
|
||||||
@ -172,7 +171,8 @@ func (h *UploadHandlers) Download(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
netTyp, _ := utils.NetworkType(r.Host)
|
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)
|
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)
|
path := path.Join(h.uploadDir, saltedHash)
|
||||||
|
|
||||||
_, err := os.Stat(path)
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||||
if os.IsNotExist(err) {
|
|
||||||
Error(w, r, http.StatusNotFound, "")
|
Error(w, r, http.StatusNotFound, "")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = os.Remove(path)
|
if err := os.Remove(path); err != nil {
|
||||||
if 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, http.StatusInternalServerError, "Failed to remove a file.")
|
||||||
return
|
return
|
||||||
@ -203,7 +201,8 @@ func (h *UploadHandlers) Delete(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
netTyp, _ := utils.NetworkType(r.Host)
|
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") {
|
if strings.Contains(r.UserAgent(), "curl") || strings.Contains(r.UserAgent(), "Wget") {
|
||||||
fmt.Fprintln(w, "File was successfully deleted.")
|
fmt.Fprintln(w, "File was successfully deleted.")
|
||||||
|
Loading…
Reference in New Issue
Block a user