Files
André Duffeck e78b40e4d5 Keep the thumbnail cache apart from the old thumbnails
The cache keys changed, so the old files were never read again but stayed mixed in with the new ones. With the cache in its own directory the old one can be deleted.
2026-10-23 14:21:41 +02:00

66 lines
2.0 KiB
Go

package cache
import (
"errors"
"fmt"
"path/filepath"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/opencloud-eu/opencloud/pkg/config/defaults"
)
var ErrCacheMiss = errors.New("thumbnail cache miss")
// DefaultFileCacheDir is the on-disk location of file-backend thumbnails:
// $OC_BASE_DATA_PATH/thumbnails/cache. It is not the files directory the old
// thumbnails service wrote to: the keys changed, nothing in there is read again,
// and keeping the two apart lets that directory be deleted as a whole.
func DefaultFileCacheDir() string {
return filepath.Join(defaults.BaseDataPath(), "thumbnails", "cache")
}
// ThumbnailCache provides cached storage for generated thumbnails.
type ThumbnailCache interface {
Get(key string) ([]byte, error)
Put(key string, data []byte) error
}
// NewThumbnailCache creates a cache instance based on the configured backend. A
// backend nobody can serve is an error, not a silently disabled cache.
func NewThumbnailCache(backend string, cacheDir string, s3cfg *S3CacheConfig) (ThumbnailCache, error) {
switch backend {
case "none":
return NoopCache{}, nil
case "memory":
return NewInMemoryCache(), nil
case "file":
dir := cacheDir
if dir == "" {
dir = DefaultFileCacheDir()
}
return NewFileCache(dir), nil
case "s3":
if s3cfg == nil || !s3cfg.IsComplete() {
return nil, errors.New("s3 thumbnail cache needs a bucket, a region and credentials")
}
return NewS3Cache(*s3cfg), nil
default:
return nil, fmt.Errorf("unknown thumbnail cache backend %q, expected none, memory, file or s3", backend)
}
}
// BuildS3CacheConfig creates an S3CacheConfig from raw config fields.
func BuildS3CacheConfig(bucket, region, endpoint, accessKey, secretKey string) *S3CacheConfig {
cfg := &S3CacheConfig{
Bucket: bucket,
Region: region,
Endpoint: endpoint,
Secure: true,
}
if accessKey != "" && secretKey != "" {
cfg.Credentials = credentials.NewStaticV4(accessKey, secretKey, "")
}
return cfg
}