From c884fe8225eb32bf4b10204cb60f9422826cd100 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Tue, 8 Feb 2022 19:02:37 +0400 Subject: [PATCH] Let's check if client is curl and return just a link. And NetworkType() returns a scheme as well now. --- internal/handlers/handlers.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index 7c1b8e9..443f7d7 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -16,6 +16,7 @@ import ( "net/http" "os" "path" + "strings" "time" "github.com/Joker/jade" @@ -161,7 +162,9 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) { return } - h.logUpload.Printf("| %s | %s | %s | SHA256 %s | %s | %d", r.RemoteAddr, utils.NetworkType(r.Host), + netTyp, _ := utils.NetworkType(r.Host) + + h.logUpload.Printf("| %s | %s | %s | SHA256 %s | %s | %d", r.RemoteAddr, netTyp, fHandler.Filename, fSha256, fSaltedHash, fHandler.Size) w.WriteHeader(http.StatusCreated) @@ -170,13 +173,14 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusFound) } - scheme := r.Header.Get("X-Forwarded-Proto") - if scheme == "" { - scheme = "http" - } - downloadURL := path.Join("/f", fSaltedHash, fHandler.Filename) + if strings.Contains(r.UserAgent(), "curl") { + _, scheme := utils.NetworkType(r.Host) + w.Write([]byte(path.Join(scheme, "://", r.Host, downloadURL) + "\r\n")) + return + } + if err := compiledTemplates["uploaded"].Execute(w, &UploadedData{ MainSite: utils.MainSite(r.Host), DownloadURL: downloadURL, @@ -209,7 +213,9 @@ func (h *UploadHandlers) Download(w http.ResponseWriter, r *http.Request) { } defer fd.Close() - h.logDownload.Printf("| %s | %s | %s | %s", r.RemoteAddr, utils.NetworkType(r.Host), name, saltedHash) + netTyp, _ := utils.NetworkType(r.Host) + + h.logDownload.Printf("| %s | %s | %s | %s", r.RemoteAddr, netTyp, name, saltedHash) http.ServeContent(w, r, path, stat.ModTime(), fd) }