switched from s3 to disk for saving icons
This commit is contained in:
parent
113a261dae
commit
5b3f6a6870
9 changed files with 84 additions and 112 deletions
38
pipeline/03_icon_download/storage.go
Normal file
38
pipeline/03_icon_download/storage.go
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
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)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue