4
4
"archive/tar"
5
5
"archive/zip"
6
6
"compress/gzip"
7
+ "crypto/sha1"
8
+ "encoding/hex"
7
9
"fmt"
8
10
"io"
9
11
"net/http"
@@ -27,6 +29,17 @@ func getCacheDir() (string, error) {
27
29
return cacheDir , nil
28
30
}
29
31
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
+
30
43
// downloadFileWithCache downloads a file from url and returns the path to the cached file
31
44
func downloadFileWithCache (url string ) (string , error ) {
32
45
cacheDir , err := getCacheDir ()
@@ -37,7 +50,17 @@ func downloadFileWithCache(url string) (string, error) {
37
50
// Use URL's last path segment as filename
38
51
urlPath := strings .Split (url , "/" )
39
52
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 )
41
64
42
65
// Check if file exists in cache
43
66
if _ , err := os .Stat (cachedFile ); err == nil {
0 commit comments