Skip to content

Commit 9323f10

Browse files
committed
fix download file cache hash
1 parent 8c0200f commit 9323f10

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

cmd/internal/install/archive.go

+24-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"archive/tar"
55
"archive/zip"
66
"compress/gzip"
7+
"crypto/sha1"
8+
"encoding/hex"
79
"fmt"
810
"io"
911
"net/http"
@@ -27,6 +29,17 @@ func getCacheDir() (string, error) {
2729
return cacheDir, nil
2830
}
2931

32+
// getFullExtension returns the full extension for a filename (e.g., ".tar.gz" for "file.tar.gz")
33+
func getFullExtension(filename string) string {
34+
// Handle common multi-level extensions
35+
for _, ext := range []string{".tar.gz", ".tar.zst"} {
36+
if strings.HasSuffix(filename, ext) {
37+
return ext
38+
}
39+
}
40+
return filepath.Ext(filename)
41+
}
42+
3043
// downloadFileWithCache downloads a file from url and returns the path to the cached file
3144
func downloadFileWithCache(url string) (string, error) {
3245
cacheDir, err := getCacheDir()
@@ -37,7 +50,17 @@ func downloadFileWithCache(url string) (string, error) {
3750
// Use URL's last path segment as filename
3851
urlPath := strings.Split(url, "/")
3952
filename := urlPath[len(urlPath)-1]
40-
cachedFile := filepath.Join(cacheDir, filename)
53+
54+
// Calculate SHA1 hash of the URL
55+
hasher := sha1.New()
56+
hasher.Write([]byte(url))
57+
urlHash := hex.EncodeToString(hasher.Sum(nil))[:8] // Use first 8 characters of hash
58+
59+
// Insert hash before the file extension, handling multi-level extensions
60+
ext := getFullExtension(filename)
61+
baseFilename := filename[:len(filename)-len(ext)]
62+
cachedFilename := fmt.Sprintf("%s-%s%s", baseFilename, urlHash, ext)
63+
cachedFile := filepath.Join(cacheDir, cachedFilename)
4164

4265
// Check if file exists in cache
4366
if _, err := os.Stat(cachedFile); err == nil {

0 commit comments

Comments
 (0)