diff --git a/commands/helpers/cache_extractor.go b/commands/helpers/cache_extractor.go index 0ad01d4f0077610900b645482a2aac4d27858976..ad7fcf0f8720758a07da88def79849e7998fa505 100644 --- a/commands/helpers/cache_extractor.go +++ b/commands/helpers/cache_extractor.go @@ -35,16 +35,15 @@ func (c *CacheExtractorCommand) getClient() *CacheClient { return c.client } +func checkIfUpToDate(path string, resp *http.Response) (bool, time.Time) { + fi, _ := os.Lstat(path) + date, _ := time.Parse(http.TimeFormat, resp.Header.Get("Last-Modified")) + return fi != nil && !date.After(fi.ModTime()), date +} + func (c *CacheExtractorCommand) download() (bool, error) { os.MkdirAll(filepath.Dir(c.File), 0700) - file, err := ioutil.TempFile(filepath.Dir(c.File), "cache") - if err != nil { - return false, err - } - defer file.Close() - defer os.Remove(file.Name()) - resp, err := c.getClient().Get(c.URL) if err != nil { return true, err @@ -59,13 +58,19 @@ func (c *CacheExtractorCommand) download() (bool, error) { return retry, fmt.Errorf("Received: %s", resp.Status) } - fi, _ := os.Lstat(c.File) - date, _ := time.Parse(http.TimeFormat, resp.Header.Get("Last-Modified")) - if fi != nil && !date.After(fi.ModTime()) { + upToDate, date := checkIfUpToDate(c.File, resp) + if upToDate { logrus.Infoln(filepath.Base(c.File), "is up to date") return false, nil } + file, err := ioutil.TempFile(filepath.Dir(c.File), "cache") + if err != nil { + return false, err + } + defer os.Remove(file.Name()) + defer file.Close() + logrus.Infoln("Downloading", filepath.Base(c.File), "from", url_helpers.CleanURL(c.URL)) _, err = io.Copy(file, resp.Body) if err != nil {