From 86ef72e654dd2763f12946929f41e8e631ad4213 Mon Sep 17 00:00:00 2001 From: Pablo Chacin Date: Thu, 5 Sep 2024 08:27:24 +0200 Subject: [PATCH] fix download url Signed-off-by: Pablo Chacin --- pkg/cache/server/server.go | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/pkg/cache/server/server.go b/pkg/cache/server/server.go index 3006e92..3cc2851 100644 --- a/pkg/cache/server/server.go +++ b/pkg/cache/server/server.go @@ -90,13 +90,7 @@ func (s *CacheServer) Get(w http.ResponseWriter, r *http.Request) { return } - // overwrite URL with own - baseURL := s.baseURL - if baseURL == "" { - baseURL = fmt.Sprintf("http://%s%s", r.Host, r.RequestURI) - } - downloadURL := fmt.Sprintf("%s/%s/download", baseURL, id) - + downloadURL := getDownloadURL(s.baseURL, r) resp.Object = cache.Object{ ID: id, Checksum: object.Checksum, @@ -135,13 +129,7 @@ func (s *CacheServer) Store(w http.ResponseWriter, r *http.Request) { return } - // overwrite URL with own - baseURL := s.baseURL - if baseURL == "" { - baseURL = fmt.Sprintf("http://%s%s", r.Host, r.RequestURI) - } - downloadURL := fmt.Sprintf("%s/%s/download", baseURL, id) - + downloadURL := getDownloadURL(s.baseURL, r) resp.Object = cache.Object{ ID: id, Checksum: object.Checksum, @@ -152,6 +140,14 @@ func (s *CacheServer) Store(w http.ResponseWriter, r *http.Request) { _ = json.NewEncoder(w).Encode(resp) //nolint:errchkjson } +func getDownloadURL(baseURL string, r *http.Request) string { + if baseURL != "" { + return fmt.Sprintf("%s/%s/download", baseURL, r.PathValue("id")) + } + + return fmt.Sprintf("http://%s%s/download", r.Host, r.RequestURI) +} + // Download returns an object's content given its id func (s *CacheServer) Download(w http.ResponseWriter, r *http.Request) { id := r.PathValue("id")