From 59a60989f09af60c831a9e755c10481a2ab17b6e Mon Sep 17 00:00:00 2001 From: Amit Singh Date: Thu, 7 Sep 2023 18:48:36 +0530 Subject: [PATCH] sets uri sha as the downloaded artifact filename and removes checks for http(s) uri Signed-off-by: Amit Singh --- dependency_cache.go | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/dependency_cache.go b/dependency_cache.go index aecb588..03f94fb 100644 --- a/dependency_cache.go +++ b/dependency_cache.go @@ -167,7 +167,9 @@ func (d *DependencyCache) Artifact(dependency BuildpackDependency, mods ...Reque actual BuildpackDependency artifact string file string + hasher = sha256.New() uri = dependency.URI + uriSha string ) for d, u := range d.Mappings { @@ -177,26 +179,17 @@ func (d *DependencyCache) Artifact(dependency BuildpackDependency, mods ...Reque } } + // downloaded artifact will have the uri sha as its filename + hasher.Write([]byte(uri)) + uriSha = hex.EncodeToString(hasher.Sum(nil)) + if dependency.SHA256 == "" { d.Logger.Headerf("%s Dependency has no SHA256. Skipping cache.", color.New(color.FgYellow, color.Bold).Sprint("Warning:")) d.Logger.Bodyf("%s from %s", color.YellowString("Downloading"), uri) - // ensure query parameters are not included in the downloaded file name if the uri is http type - downloadUri, err := url.Parse(uri) - if err != nil { - return nil, fmt.Errorf("unable to parse the download uri %s\n%w", uri, err) - } - - if(downloadUri.Scheme == "http" || downloadUri.Scheme == "https") { - hasher := sha256.New() - hasher.Write([]byte(uri)) - - artifact = filepath.Join(d.DownloadPath, hex.EncodeToString(hasher.Sum(nil))) - } else { - artifact = filepath.Join(d.DownloadPath, filepath.Base(uri)) - } + artifact = filepath.Join(d.DownloadPath, uriSha) if err := d.download(uri, artifact, mods...); err != nil { return nil, fmt.Errorf("unable to download %s\n%w", uri, err) @@ -234,7 +227,7 @@ func (d *DependencyCache) Artifact(dependency BuildpackDependency, mods ...Reque } d.Logger.Bodyf("%s from %s", color.YellowString("Downloading"), uri) - artifact = filepath.Join(d.DownloadPath, dependency.SHA256, filepath.Base(uri)) + artifact = filepath.Join(d.DownloadPath, dependency.SHA256, uriSha) if err := d.download(uri, artifact, mods...); err != nil { return nil, fmt.Errorf("unable to download %s\n%w", uri, err) }