2021-09-03 17:42:37 +02:00
|
|
|
package crop
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-09-05 17:10:52 +02:00
|
|
|
"path/filepath"
|
2021-09-03 17:42:37 +02:00
|
|
|
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2021-12-14 20:01:39 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/sanitize"
|
2021-09-03 17:42:37 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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)
|
2021-09-03 17:42:37 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2021-09-05 21:19:52 +02:00
|
|
|
return fileName, err
|
2021-09-03 17:42:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if fs.FileExists(fileName) {
|
|
|
|
return fileName, nil
|
|
|
|
}
|
|
|
|
|
2021-09-05 21:19:52 +02:00
|
|
|
return fileName, fmt.Errorf("%s not found", filepath.Base(fileName))
|
2021-09-03 17:42:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2021-09-03 17:42:37 +02:00
|
|
|
if len(hash) < 4 {
|
2021-12-14 20:01:39 +01:00
|
|
|
return "", fmt.Errorf("crop: invalid file hash %s", sanitize.Log(hash))
|
2021-09-03 17:42:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|