From ed7d088bbc287a570c09e7732e4e5fdd68b91b69 Mon Sep 17 00:00:00 2001 From: smruthi2187 <34555664+smruthi2187@users.noreply.github.com> Date: Fri, 30 Mar 2018 01:33:06 -0700 Subject: [PATCH] Check if the requested file already exists in fetcher and skip fetch (#584) --- environments/fetcher/fetcher.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/environments/fetcher/fetcher.go b/environments/fetcher/fetcher.go index d39817a3..f0b4d932 100644 --- a/environments/fetcher/fetcher.go +++ b/environments/fetcher/fetcher.go @@ -217,6 +217,19 @@ func (fetcher *Fetcher) FetchHandler(w http.ResponseWriter, r *http.Request) { // Fetch takes FetchRequest and makes the fetch call // It returns the HTTP code and error if any func (fetcher *Fetcher) Fetch(req FetchRequest) (int, error) { + // check that the requested filename is not an empty string and error out if so + if len(req.Filename) == 0 { + e := fmt.Sprintf("Fetch request received for an empty file name, request: %v", req) + log.Printf(e) + return 400, errors.New(e) + } + + // verify first if the file already exists. + if _, err := os.Stat(filepath.Join(fetcher.sharedVolumePath, req.Filename)); err == nil { + log.Printf("Requested file: %s already exists at %s. Skipping fetch", req.Filename, fetcher.sharedVolumePath) + return 200, nil + } + tmpFile := req.Filename + ".tmp" tmpPath := filepath.Join(fetcher.sharedVolumePath, tmpFile)