photoprism/internal/crop/cache.go

46 lines
1.2 KiB
Go
Raw Normal View History

package crop
import (
"fmt"
2021-09-05 17:10:52 +02:00
"path/filepath"
"path"
"github.com/photoprism/photoprism/pkg/fs"
"github.com/photoprism/photoprism/pkg/txt"
)
// FromCache returns the crop file name if cached.
2021-09-05 17:10:52 +02:00
func FromCache(hash, area string, size Size, thumbPath string) (fileName string, err error) {
fileName, err = FileName(hash, area, size.Width, size.Height, thumbPath)
if err != nil {
return "", err
}
if fs.FileExists(fileName) {
return fileName, nil
}
2021-09-05 17:10:52 +02:00
return "", fmt.Errorf("%s not found", filepath.Base(fileName))
}
// FileName returns the crop file name based on cache path, size, and area.
2021-09-05 17:10:52 +02:00
func FileName(hash, area string, width, height int, thumbPath string) (fileName string, err error) {
if len(hash) < 4 {
return "", fmt.Errorf("crop: invalid file hash %s", txt.Quote(hash))
}
if len(thumbPath) < 1 {
return "", fmt.Errorf("crop: cache path missing")
}
if width < 1 || height < 1 || width > 2048 || height > 2048 {
return "", fmt.Errorf("crop: invalid size %dx%d", width, height)
}
fileName = path.Join(thumbPath, hash[0:1], hash[1:2], hash[2:3], fmt.Sprintf("%s_%dx%d_crop_%s%s", hash, width, height, area, fs.JpegExt))
return fileName, nil
}