2020-12-30 13:33:47 +01:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CachePath returns a cache directory name based on the base path, file hash and cache namespace.
|
|
|
|
func CachePath(basePath, fileHash, namespace string, create bool) (cachePath string, err error) {
|
|
|
|
if len(fileHash) < 4 {
|
|
|
|
return "", fmt.Errorf("cache: hash '%s' is too short", fileHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
if namespace == "" {
|
|
|
|
return "", fmt.Errorf("cache: namespace for hash '%s' is empty", fileHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
cachePath = path.Join(basePath, namespace, fileHash[0:1], fileHash[1:2], fileHash[2:3])
|
|
|
|
|
|
|
|
if create {
|
2022-10-31 15:01:48 +01:00
|
|
|
if err := os.MkdirAll(cachePath, ModeDir); err != nil {
|
2020-12-30 13:33:47 +01:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cachePath, nil
|
|
|
|
}
|