MCPcopy Create free account
hub / github.com/celer-pkg/celer / FindCachedFile

Function FindCachedFile

pkgs/fileio/cache_file.go:32–66  ·  view source on GitHub ↗

FindCachedFile searches for a file matching SHA256 in the cache directory, Cache file naming format: {basename}-{sha256}.{ext}

(cacheDir, fileName, sha256 string)

Source from the content-addressed store, hash-verified

30// FindCachedFile searches for a file matching SHA256 in the cache directory,
31// Cache file naming format: {basename}-{sha256}.{ext}
32func FindCachedFile(cacheDir, fileName, sha256 string) (string, error) {
33 if sha256 == "" {
34 panic(fmt.Sprintf("no sha256 hash provided for %s/%s", cacheDir, fileName))
35 }
36
37 if !PathExists(cacheDir) {
38 return "", nil
39 }
40
41 // Build cached filename directly: {name}-{sha256}.{ext}
42 cachedFileName := fmt.Sprintf("%s-%s%s", Base(fileName), sha256, Ext(fileName))
43 cachedFilePath := filepath.Join(cacheDir, cachedFileName)
44
45 // Check if the file exists.
46 if !PathExists(cachedFilePath) {
47 return "", nil
48 }
49
50 color.Printf(color.Title, "\n%s\n", fmt.Sprintf("[validating file cache: %s]", fileName))
51 color.PrintInline(color.Hint, "- validating with sha256: %s", sha256)
52
53 // Verify file's sha256.
54 computedHash, err := ComputeSHA256(cachedFilePath)
55 if err != nil {
56 color.PrintInline(color.Hint, "✘ validate with sha256: %s\n", sha256)
57 return "", fmt.Errorf("failed to compute sha-256 for cached file -> %w", err)
58 }
59 if computedHash == sha256 {
60 color.PrintInline(color.Hint, "✔ validate with sha256: %s\n", sha256)
61 return cachedFilePath, nil
62 }
63
64 color.PrintInline(color.Hint, "✘ validate with sha256: %s\n", sha256)
65 return "", nil
66}
67
68// SaveCachedFile saves a downloaded file to the cache directory using SHA256 in the filename.
69func SaveCachedFile(srcFile, cacheDir, fileName, sha256 string, chattrFS *ChattrFS) (string, error) {

Callers 2

tryRestoreFromCacheMethod · 0.85

Calls 7

PrintfFunction · 0.92
PrintInlineFunction · 0.92
PathExistsFunction · 0.85
BaseFunction · 0.85
ExtFunction · 0.85
ComputeSHA256Function · 0.85
SprintfMethod · 0.80

Tested by 1