everytab/pipeline/03_icon_download/storage.go

38 lines
922 B
Go

package main
import (
"fmt"
"os"
"path/filepath"
)
var iconsDir string
func initStorage(dir string) error {
iconsDir = dir
return os.MkdirAll(dir, 0755)
}
// hashToPath converts a SHA-256 hex hash to a sharded file path.
// e.g., "abcdef1234..." → "icons/ab/cd/ef/abcdef1234..."
func hashToPath(hash string) string {
if len(hash) < 6 {
return filepath.Join(iconsDir, hash)
}
return filepath.Join(iconsDir, hash[:2], hash[2:4], hash[4:6], hash)
}
// iconExists checks if an icon is already stored on disk (for dedup).
func iconExists(hash string) bool {
_, err := os.Stat(hashToPath(hash))
return err == nil
}
// iconWrite saves icon data to disk in the sharded directory structure.
func iconWrite(hash string, data []byte) error {
path := hashToPath(hash)
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
return fmt.Errorf("mkdir: %w", err)
}
return os.WriteFile(path, data, 0644)
}