mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-11-26 10:07:34 -05:00
fix(webdav): record a file without an image as a nil image
This commit is contained in:
1 parent
5f1d77b1a8
commit
37b61663df
9 files changed
+127
-100
No files matched your search
+4
-3
@@ -21,10 +21,11 @@ func DefaultFileCacheDir() string {
|
||||
return filepath.Join(defaults.BaseDataPath(), "thumbnails", "cache")
|
||||
}
|
||||
|
||||
// ThumbnailCache provides cached storage for generated thumbnails.
|
||||
// ThumbnailCache provides cached storage for generated thumbnails. A nil image
|
||||
// records that a file has none.
|
||||
type ThumbnailCache interface {
|
||||
Get(key string) (thumbnail.Image, error)
|
||||
Put(key string, img thumbnail.Image) error
|
||||
Get(key string) (*thumbnail.Image, error)
|
||||
Put(key string, img *thumbnail.Image) error
|
||||
}
|
||||
|
||||
// NewThumbnailCache creates a cache instance based on the configured backend. A
|
||||
|
||||
+42
-42
@@ -37,11 +37,11 @@ var _ = Describe("InMemoryCache", func() {
|
||||
It("should return ErrCacheMiss for missing key", func() {
|
||||
got, err := cache.Get("nonexistent")
|
||||
Expect(err).To(MatchError(ErrCacheMiss))
|
||||
Expect(got.Data).To(BeNil())
|
||||
Expect(got).To(BeNil())
|
||||
})
|
||||
|
||||
It("should store and retrieve data", func() {
|
||||
err := cache.Put("key1", thumbnail.Image{Data: []byte("thumbnail-data")})
|
||||
err := cache.Put("key1", &thumbnail.Image{Data: []byte("thumbnail-data")})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
got, err := cache.Get("key1")
|
||||
@@ -50,7 +50,7 @@ var _ = Describe("InMemoryCache", func() {
|
||||
})
|
||||
|
||||
It("should return independent copies on Get", func() {
|
||||
err := cache.Put("key1", thumbnail.Image{Data: []byte("original")})
|
||||
err := cache.Put("key1", &thumbnail.Image{Data: []byte("original")})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
got, err := cache.Get("key1")
|
||||
@@ -63,10 +63,10 @@ var _ = Describe("InMemoryCache", func() {
|
||||
})
|
||||
|
||||
It("should overwrite existing keys", func() {
|
||||
err := cache.Put("key1", thumbnail.Image{Data: []byte("v1")})
|
||||
err := cache.Put("key1", &thumbnail.Image{Data: []byte("v1")})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
err = cache.Put("key1", thumbnail.Image{Data: []byte("v2")})
|
||||
err = cache.Put("key1", &thumbnail.Image{Data: []byte("v2")})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
got, err := cache.Get("key1")
|
||||
@@ -74,13 +74,13 @@ var _ = Describe("InMemoryCache", func() {
|
||||
Expect(got.Data).To(Equal([]byte("v2")))
|
||||
})
|
||||
|
||||
It("should handle empty data", func() {
|
||||
err := cache.Put("empty-key", thumbnail.Image{Data: []byte{}})
|
||||
It("remembers a file without an image as nil", func() {
|
||||
err := cache.Put("empty-key", nil)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
got, err := cache.Get("empty-key")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(got.Data).To(BeEmpty())
|
||||
Expect(got).To(BeNil(), "a file without an image")
|
||||
})
|
||||
})
|
||||
|
||||
@@ -94,19 +94,19 @@ var _ = Describe("NoopCache", func() {
|
||||
It("should always return ErrCacheMiss on Get", func() {
|
||||
got, err := cache.Get("any-key")
|
||||
Expect(err).To(MatchError(ErrCacheMiss))
|
||||
Expect(got.Data).To(BeNil())
|
||||
Expect(got).To(BeNil())
|
||||
})
|
||||
|
||||
It("should accept Put without error", func() {
|
||||
err := cache.Put("key1", thumbnail.Image{Data: []byte("data")})
|
||||
err := cache.Put("key1", &thumbnail.Image{Data: []byte("data")})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
|
||||
It("should still miss after Put", func() {
|
||||
_ = cache.Put("key1", thumbnail.Image{Data: []byte("data")})
|
||||
_ = cache.Put("key1", &thumbnail.Image{Data: []byte("data")})
|
||||
got, err := cache.Get("key1")
|
||||
Expect(err).To(MatchError(ErrCacheMiss))
|
||||
Expect(got.Data).To(BeNil())
|
||||
Expect(got).To(BeNil())
|
||||
})
|
||||
})
|
||||
|
||||
@@ -115,7 +115,7 @@ var _ = Describe("NewThumbnailCache", func() {
|
||||
cache, err := NewThumbnailCache("memory", "", nil)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(cache).ToNot(BeNil())
|
||||
err = cache.Put("k", thumbnail.Image{Data: []byte("v")})
|
||||
err = cache.Put("k", &thumbnail.Image{Data: []byte("v")})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
got, err := cache.Get("k")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
@@ -137,7 +137,7 @@ var _ = Describe("NewThumbnailCache", func() {
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
got, err := cache.Get("k")
|
||||
Expect(err).To(MatchError(ErrCacheMiss))
|
||||
Expect(got.Data).To(BeNil())
|
||||
Expect(got).To(BeNil())
|
||||
})
|
||||
|
||||
It("should create S3Cache for 's3' backend with complete config", func() {
|
||||
@@ -277,11 +277,11 @@ var _ = Describe("S3Cache", func() {
|
||||
It("should return ErrCacheMiss for missing key", func() {
|
||||
got, err := cache.Get("nonexistent-key")
|
||||
Expect(err).To(MatchError(ErrCacheMiss))
|
||||
Expect(got.Data).To(BeNil())
|
||||
Expect(got).To(BeNil())
|
||||
})
|
||||
|
||||
It("should store and retrieve data", func() {
|
||||
err := cache.Put("sha256:abc123-400x400.jpg", thumbnail.Image{Data: []byte("thumbnail-data")})
|
||||
err := cache.Put("sha256:abc123-400x400.jpg", &thumbnail.Image{Data: []byte("thumbnail-data")})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
got, err := cache.Get("sha256:abc123-400x400.jpg")
|
||||
@@ -290,7 +290,7 @@ var _ = Describe("S3Cache", func() {
|
||||
})
|
||||
|
||||
It("should return independent copies on Get", func() {
|
||||
err := cache.Put("key1", thumbnail.Image{Data: []byte("original")})
|
||||
err := cache.Put("key1", &thumbnail.Image{Data: []byte("original")})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
got, err := cache.Get("key1")
|
||||
@@ -303,10 +303,10 @@ var _ = Describe("S3Cache", func() {
|
||||
})
|
||||
|
||||
It("should overwrite existing keys", func() {
|
||||
err := cache.Put("key1", thumbnail.Image{Data: []byte("v1")})
|
||||
err := cache.Put("key1", &thumbnail.Image{Data: []byte("v1")})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
err = cache.Put("key1", thumbnail.Image{Data: []byte("v2")})
|
||||
err = cache.Put("key1", &thumbnail.Image{Data: []byte("v2")})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
got, err := cache.Get("key1")
|
||||
@@ -314,18 +314,18 @@ var _ = Describe("S3Cache", func() {
|
||||
Expect(got.Data).To(Equal([]byte("v2")))
|
||||
})
|
||||
|
||||
It("should handle empty data", func() {
|
||||
err := cache.Put("empty-key", thumbnail.Image{Data: []byte{}})
|
||||
It("remembers a file without an image as nil", func() {
|
||||
err := cache.Put("empty-key", nil)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
got, err := cache.Get("empty-key")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(got.Data).To(BeEmpty())
|
||||
Expect(got).To(BeNil(), "a file without an image")
|
||||
})
|
||||
|
||||
It("should use flat key format", func() {
|
||||
key := "sha256:abc123-400x400.jpg"
|
||||
err := cache.Put(key, thumbnail.Image{Data: []byte("data")})
|
||||
err := cache.Put(key, &thumbnail.Image{Data: []byte("data")})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
got, err := cache.Get(key)
|
||||
@@ -336,7 +336,7 @@ var _ = Describe("S3Cache", func() {
|
||||
It("should initialize client lazily on first access", func() {
|
||||
Expect(cache.client).To(BeNil())
|
||||
|
||||
_ = cache.Put("key1", thumbnail.Image{Data: []byte("data")})
|
||||
_ = cache.Put("key1", &thumbnail.Image{Data: []byte("data")})
|
||||
Expect(cache.client).ToNot(BeNil())
|
||||
})
|
||||
|
||||
@@ -345,7 +345,7 @@ var _ = Describe("S3Cache", func() {
|
||||
|
||||
got, err := cache.Get("any-key")
|
||||
Expect(err).To(HaveOccurred())
|
||||
Expect(got.Data).To(BeNil())
|
||||
Expect(got).To(BeNil())
|
||||
})
|
||||
|
||||
It("should keep an error that is not a missing key an error", func() {
|
||||
@@ -359,7 +359,7 @@ var _ = Describe("S3Cache", func() {
|
||||
It("should handle S3 errors gracefully on Put", func() {
|
||||
mockClient.putErr = errors.New("connection refused")
|
||||
|
||||
err := cache.Put("key1", thumbnail.Image{Data: []byte("data")})
|
||||
err := cache.Put("key1", &thumbnail.Image{Data: []byte("data")})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
|
||||
@@ -370,11 +370,11 @@ var _ = Describe("S3Cache", func() {
|
||||
|
||||
got, err := cache.Get("any-key")
|
||||
Expect(err).To(HaveOccurred())
|
||||
Expect(got.Data).To(BeNil())
|
||||
Expect(got).To(BeNil())
|
||||
})
|
||||
|
||||
It("stores the entry's type as the object's Content-Type", func() {
|
||||
Expect(cache.Put("key1", thumbnail.Image{Data: []byte("data"), ContentType: "image/png"})).To(Succeed())
|
||||
Expect(cache.Put("key1", &thumbnail.Image{Data: []byte("data"), ContentType: "image/png"})).To(Succeed())
|
||||
Expect(mockClient.contentTypes["key1"]).To(Equal("image/png"))
|
||||
|
||||
got, err := cache.Get("key1")
|
||||
@@ -404,12 +404,12 @@ var _ = Describe("FileCache", func() {
|
||||
It("should return ErrCacheMiss for missing key", func() {
|
||||
got, err := cache.Get("ab/cd/ef1234567890/400x400.jpg")
|
||||
Expect(err).To(MatchError(ErrCacheMiss))
|
||||
Expect(got.Data).To(BeNil())
|
||||
Expect(got).To(BeNil())
|
||||
})
|
||||
|
||||
It("should store and retrieve data", func() {
|
||||
key := "ab/cd/ef1234567890/400x400.jpg"
|
||||
err := cache.Put(key, thumbnail.Image{Data: []byte("thumbnail-data")})
|
||||
err := cache.Put(key, &thumbnail.Image{Data: []byte("thumbnail-data")})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
got, err := cache.Get(key)
|
||||
@@ -419,7 +419,7 @@ var _ = Describe("FileCache", func() {
|
||||
|
||||
It("should use hierarchical directory layout", func() {
|
||||
key := "ab/cd/ef1234567890/400x400.jpg"
|
||||
err := cache.Put(key, thumbnail.Image{Data: []byte("thumbnail-data")})
|
||||
err := cache.Put(key, &thumbnail.Image{Data: []byte("thumbnail-data")})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
expectedPath := filepath.Join(tmpdir, "ab", "cd", "ef1234567890", "400x400.jpg")
|
||||
@@ -430,7 +430,7 @@ var _ = Describe("FileCache", func() {
|
||||
|
||||
It("should create parent directories with 0700 permissions", func() {
|
||||
key := "ab/cd/ef1234567890/400x400.jpg"
|
||||
err := cache.Put(key, thumbnail.Image{Data: []byte("thumbnail-data")})
|
||||
err := cache.Put(key, &thumbnail.Image{Data: []byte("thumbnail-data")})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
dirPath := filepath.Join(tmpdir, "ab", "cd")
|
||||
@@ -442,7 +442,7 @@ var _ = Describe("FileCache", func() {
|
||||
|
||||
It("should return independent copies on Get", func() {
|
||||
key := "ab/cd/ef1234567890/400x400.jpg"
|
||||
err := cache.Put(key, thumbnail.Image{Data: []byte("original")})
|
||||
err := cache.Put(key, &thumbnail.Image{Data: []byte("original")})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
got, err := cache.Get(key)
|
||||
@@ -456,22 +456,22 @@ var _ = Describe("FileCache", func() {
|
||||
|
||||
It("should overwrite existing keys", func() {
|
||||
key := "ab/cd/ef1234567890/400x400.jpg"
|
||||
Expect(cache.Put(key, thumbnail.Image{Data: []byte("v1")})).To(Succeed())
|
||||
Expect(cache.Put(key, thumbnail.Image{Data: []byte("v2")})).To(Succeed())
|
||||
Expect(cache.Put(key, &thumbnail.Image{Data: []byte("v1")})).To(Succeed())
|
||||
Expect(cache.Put(key, &thumbnail.Image{Data: []byte("v2")})).To(Succeed())
|
||||
|
||||
got, err := cache.Get(key)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(got.Data).To(Equal([]byte("v2")), "a key whose value can change has to be replaceable")
|
||||
})
|
||||
|
||||
It("should handle empty data", func() {
|
||||
It("remembers a file without an image as nil", func() {
|
||||
key := "ab/cd/ef1234567890/400x400.jpg"
|
||||
err := cache.Put(key, thumbnail.Image{Data: []byte{}})
|
||||
err := cache.Put(key, nil)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
got, err := cache.Get(key)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(got.Data).To(BeEmpty())
|
||||
Expect(got).To(BeNil(), "a file without an image")
|
||||
})
|
||||
|
||||
It("should use atomic write via temp file + rename", func() {
|
||||
@@ -499,7 +499,7 @@ var _ = Describe("FileCache", func() {
|
||||
}
|
||||
}()
|
||||
|
||||
err := cache.Put(key, thumbnail.Image{Data: bytes.Repeat([]byte("x"), 1024*1024)})
|
||||
err := cache.Put(key, &thumbnail.Image{Data: bytes.Repeat([]byte("x"), 1024*1024)})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
close(done)
|
||||
@@ -517,13 +517,13 @@ var _ = Describe("FileCache", func() {
|
||||
// The cache dir path passes through a regular file, so MkdirAll cannot
|
||||
// create the target directory for any user (root or not) -> Put must error.
|
||||
rc := NewFileCache(filepath.Join(blocker, "sub"))
|
||||
err := rc.Put("ab/cd/ef1234567890/400x400.jpg", thumbnail.Image{Data: []byte("data")})
|
||||
err := rc.Put("ab/cd/ef1234567890/400x400.jpg", &thumbnail.Image{Data: []byte("data")})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
|
||||
It("keeps the content type in an extended attribute", func() {
|
||||
key := "ab/cd/ef1234567890/400x400"
|
||||
Expect(cache.Put(key, thumbnail.Image{Data: []byte("thumbnail-data"), ContentType: "image/png"})).To(Succeed())
|
||||
Expect(cache.Put(key, &thumbnail.Image{Data: []byte("thumbnail-data"), ContentType: "image/png"})).To(Succeed())
|
||||
|
||||
got, err := cache.Get(key)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
@@ -562,7 +562,7 @@ var _ = Describe("NewThumbnailCache factory", func() {
|
||||
Expect(ok).To(BeTrue())
|
||||
Expect(fileCache.dir).To(Equal(tmpdir))
|
||||
|
||||
err = cache.Put("ab/cd/ef1234567890/400x400.jpg", thumbnail.Image{Data: []byte("file-data")})
|
||||
err = cache.Put("ab/cd/ef1234567890/400x400.jpg", &thumbnail.Image{Data: []byte("file-data")})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
got, err := cache.Get("ab/cd/ef1234567890/400x400.jpg")
|
||||
|
||||
+18
-12
@@ -46,30 +46,34 @@ func (c *FileCache) probe() error {
|
||||
|
||||
// The cache key is already a hierarchical relative path, so the on-disk
|
||||
// location is the cache dir joined with it.
|
||||
func (c *FileCache) Get(key string) (thumbnail.Image, error) {
|
||||
func (c *FileCache) Get(key string) (*thumbnail.Image, error) {
|
||||
f, err := os.Open(filepath.Join(c.dir, key))
|
||||
if err != nil {
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
return thumbnail.Image{}, ErrCacheMiss
|
||||
return nil, ErrCacheMiss
|
||||
}
|
||||
return thumbnail.Image{}, err
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
data, err := io.ReadAll(f)
|
||||
if err != nil {
|
||||
return thumbnail.Image{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
contentType, err := xattr.FGet(f, contentTypeAttr)
|
||||
if err != nil && !errors.Is(err, xattr.ENOATTR) {
|
||||
return thumbnail.Image{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return thumbnail.Image{Data: data, ContentType: string(contentType)}, nil
|
||||
if len(data) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return &thumbnail.Image{ContentType: string(contentType), Data: data}, nil
|
||||
}
|
||||
|
||||
func (c *FileCache) Put(key string, img thumbnail.Image) error {
|
||||
func (c *FileCache) Put(key string, img *thumbnail.Image) error {
|
||||
imgPath := filepath.Join(c.dir, key)
|
||||
dir := filepath.Dir(imgPath)
|
||||
|
||||
@@ -83,13 +87,15 @@ func (c *FileCache) Put(key string, img thumbnail.Image) error {
|
||||
}
|
||||
tempPath := f.Name()
|
||||
|
||||
if _, err := f.Write(img.Data); err != nil {
|
||||
f.Close()
|
||||
os.Remove(tempPath)
|
||||
return err
|
||||
if img != nil {
|
||||
if _, err := f.Write(img.Data); err != nil {
|
||||
f.Close()
|
||||
os.Remove(tempPath)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if img.ContentType != "" {
|
||||
if img != nil && img.ContentType != "" {
|
||||
if err := xattr.FSet(f, contentTypeAttr, []byte(img.ContentType)); err != nil {
|
||||
f.Close()
|
||||
os.Remove(tempPath)
|
||||
|
||||
+26
-15
@@ -15,7 +15,7 @@ const InMemoryCacheMaxBytes = 128 << 20
|
||||
// InMemoryCacheMaxBytes and evicting in insertion order.
|
||||
type InMemoryCache struct {
|
||||
mu sync.RWMutex
|
||||
data map[string]thumbnail.Image
|
||||
data map[string]*thumbnail.Image
|
||||
order []string
|
||||
size int
|
||||
maxSize int
|
||||
@@ -23,47 +23,58 @@ type InMemoryCache struct {
|
||||
|
||||
func NewInMemoryCache() *InMemoryCache {
|
||||
return &InMemoryCache{
|
||||
data: make(map[string]thumbnail.Image),
|
||||
data: make(map[string]*thumbnail.Image),
|
||||
maxSize: InMemoryCacheMaxBytes,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *InMemoryCache) Get(key string) (thumbnail.Image, error) {
|
||||
func (c *InMemoryCache) Get(key string) (*thumbnail.Image, error) {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
|
||||
img, ok := c.data[key]
|
||||
if !ok {
|
||||
return thumbnail.Image{}, ErrCacheMiss
|
||||
return nil, ErrCacheMiss
|
||||
}
|
||||
|
||||
data := make([]byte, len(img.Data))
|
||||
copy(data, img.Data)
|
||||
return thumbnail.Image{Data: data, ContentType: img.ContentType}, nil
|
||||
return clone(img), nil
|
||||
}
|
||||
|
||||
func (c *InMemoryCache) Put(key string, img thumbnail.Image) error {
|
||||
func (c *InMemoryCache) Put(key string, img *thumbnail.Image) error {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
data := make([]byte, len(img.Data))
|
||||
copy(data, img.Data)
|
||||
|
||||
if old, ok := c.data[key]; ok {
|
||||
c.size -= len(old.Data)
|
||||
c.size -= imageSize(old)
|
||||
} else {
|
||||
c.order = append(c.order, key)
|
||||
}
|
||||
c.data[key] = thumbnail.Image{Data: data, ContentType: img.ContentType}
|
||||
c.size += len(data)
|
||||
c.data[key] = clone(img)
|
||||
c.size += imageSize(img)
|
||||
|
||||
// the entry just written stays, even when it is larger than the budget
|
||||
for c.size > c.maxSize && len(c.order) > 1 {
|
||||
oldest := c.order[0]
|
||||
c.order = c.order[1:]
|
||||
c.size -= len(c.data[oldest].Data)
|
||||
c.size -= imageSize(c.data[oldest])
|
||||
delete(c.data, oldest)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func clone(img *thumbnail.Image) *thumbnail.Image {
|
||||
if img == nil {
|
||||
return nil
|
||||
}
|
||||
data := make([]byte, len(img.Data))
|
||||
copy(data, img.Data)
|
||||
return &thumbnail.Image{ContentType: img.ContentType, Data: data}
|
||||
}
|
||||
|
||||
func imageSize(img *thumbnail.Image) int {
|
||||
if img == nil {
|
||||
return 0
|
||||
}
|
||||
return len(img.Data)
|
||||
}
|
||||
+3
-3
@@ -5,10 +5,10 @@ import "github.com/opencloud-eu/opencloud/services/webdav/pkg/thumbnail"
|
||||
// NoopCache always returns a cache miss and discards puts.
|
||||
type NoopCache struct{}
|
||||
|
||||
func (NoopCache) Get(_ string) (thumbnail.Image, error) {
|
||||
return thumbnail.Image{}, ErrCacheMiss
|
||||
func (NoopCache) Get(_ string) (*thumbnail.Image, error) {
|
||||
return nil, ErrCacheMiss
|
||||
}
|
||||
|
||||
func (NoopCache) Put(_ string, _ thumbnail.Image) error {
|
||||
func (NoopCache) Put(_ string, _ *thumbnail.Image) error {
|
||||
return nil
|
||||
}
|
||||
+20
-10
@@ -86,28 +86,32 @@ func (c *S3Cache) initClient() error {
|
||||
return c.initErr
|
||||
}
|
||||
|
||||
func (c *S3Cache) Get(key string) (thumbnail.Image, error) {
|
||||
func (c *S3Cache) Get(key string) (*thumbnail.Image, error) {
|
||||
if err := c.initClient(); err != nil {
|
||||
return thumbnail.Image{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
obj, err := c.client.GetObject(context.Background(), c.config.Bucket, key, minio.GetObjectOptions{})
|
||||
if err != nil {
|
||||
return thumbnail.Image{}, asCacheError(err)
|
||||
return nil, asCacheError(err)
|
||||
}
|
||||
defer obj.Close()
|
||||
|
||||
info, err := obj.Stat()
|
||||
if err != nil {
|
||||
return thumbnail.Image{}, asCacheError(err)
|
||||
return nil, asCacheError(err)
|
||||
}
|
||||
|
||||
data, err := io.ReadAll(obj)
|
||||
if err != nil {
|
||||
return thumbnail.Image{}, asCacheError(err)
|
||||
return nil, asCacheError(err)
|
||||
}
|
||||
|
||||
return thumbnail.Image{Data: data, ContentType: info.ContentType}, nil
|
||||
if len(data) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return &thumbnail.Image{ContentType: info.ContentType, Data: data}, nil
|
||||
}
|
||||
|
||||
// asCacheError reports a missing object as a cache miss. minio's GetObject only
|
||||
@@ -127,19 +131,25 @@ func asCacheError(err error) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *S3Cache) Put(key string, img thumbnail.Image) error {
|
||||
func (c *S3Cache) Put(key string, img *thumbnail.Image) error {
|
||||
if err := c.initClient(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var data []byte
|
||||
var contentType string
|
||||
if img != nil {
|
||||
data, contentType = img.Data, img.ContentType
|
||||
}
|
||||
|
||||
_, err := c.client.PutObject(
|
||||
context.Background(),
|
||||
c.config.Bucket,
|
||||
key,
|
||||
bytes.NewReader(img.Data),
|
||||
int64(len(img.Data)),
|
||||
bytes.NewReader(data),
|
||||
int64(len(data)),
|
||||
minio.PutObjectOptions{
|
||||
ContentType: img.ContentType,
|
||||
ContentType: contentType,
|
||||
},
|
||||
)
|
||||
return err
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
package thumbnail
|
||||
|
||||
// Image is an image and the type it is in, as an extractor hands it on and as
|
||||
// the cache keeps it. webdav does not decode it, the generator does. An Image
|
||||
// without data records that a file has none.
|
||||
// the cache keeps it. webdav does not decode it, the generator does.
|
||||
type Image struct {
|
||||
ContentType string
|
||||
Data []byte
|
||||
|
||||
@@ -457,7 +457,7 @@ func (w *ThumbnailWorkflow) generate(ctx context.Context, ref *providerv1beta1.R
|
||||
if w.cache != nil {
|
||||
cached, err := w.cache.Get(cacheKey)
|
||||
switch {
|
||||
case err == nil && cached.ContentType != "":
|
||||
case err == nil && cached != nil && cached.ContentType != "":
|
||||
return Thumbnail{Data: cached.Data, ContentType: cached.ContentType}, nil
|
||||
case err == nil:
|
||||
// written without a type, e.g. copied without its xattrs: made again
|
||||
@@ -508,7 +508,7 @@ func (w *ThumbnailWorkflow) generate(ctx context.Context, ref *providerv1beta1.R
|
||||
}
|
||||
|
||||
if w.cache != nil {
|
||||
if err := w.cache.Put(cacheKey, thumbnail.Image{Data: thumbBytes, ContentType: outputType}); err != nil {
|
||||
if err := w.cache.Put(cacheKey, &thumbnail.Image{ContentType: outputType, Data: thumbBytes}); err != nil {
|
||||
logger.Debug().Err(err).Msg("could not cache thumbnail")
|
||||
}
|
||||
}
|
||||
@@ -545,7 +545,7 @@ func (w *ThumbnailWorkflow) sourceImage(ctx context.Context, ref *providerv1beta
|
||||
if converter != nil && w.cache != nil {
|
||||
sourceKey = sourceStorageKey(checksum, mimeType, converter.Name())
|
||||
if cached, err := w.cache.Get(sourceKey); err == nil {
|
||||
if len(cached.Data) == 0 {
|
||||
if cached == nil {
|
||||
return nil, "", nil, ErrNoThumbnail
|
||||
}
|
||||
if cached.ContentType != "" {
|
||||
@@ -573,14 +573,14 @@ func (w *ThumbnailWorkflow) sourceImage(ctx context.Context, ref *providerv1beta
|
||||
if errors.Is(err, preprocessor.ErrNoThumbnail) {
|
||||
//the file carries no image, which is an answer and worth remembering;
|
||||
//a failed extraction is not, it may succeed on the next try
|
||||
w.rememberSource(sourceKey, thumbnail.Image{})
|
||||
w.rememberSource(sourceKey, nil)
|
||||
return nil, "", nil, ErrNoThumbnail
|
||||
}
|
||||
if err != nil || img == nil {
|
||||
logger.Debug().Err(err).Msg("could not convert file to image")
|
||||
return nil, "", nil, fmt.Errorf("%w: could not get image", ErrNotFound)
|
||||
}
|
||||
w.rememberSource(sourceKey, *img)
|
||||
w.rememberSource(sourceKey, img)
|
||||
|
||||
return bytes.NewReader(img.Data), img.ContentType, func() {}, nil
|
||||
}
|
||||
@@ -591,11 +591,11 @@ func (w *ThumbnailWorkflow) sourceImage(ctx context.Context, ref *providerv1beta
|
||||
// thumbnails it serves to make room for one source.
|
||||
const maxCachedSourceBytes = 8 << 20
|
||||
|
||||
// rememberSource keeps what an extractor made of a file, or the empty entry
|
||||
// that says it made nothing. An image too large to keep is simply not kept:
|
||||
// rememberSource keeps what an extractor made of a file, or nil when it found
|
||||
// nothing. An image too large to keep is simply not kept:
|
||||
// the extraction runs again next time, which costs time, not correctness.
|
||||
func (w *ThumbnailWorkflow) rememberSource(key string, img thumbnail.Image) {
|
||||
if w.cache == nil || len(img.Data) > maxCachedSourceBytes {
|
||||
func (w *ThumbnailWorkflow) rememberSource(key string, img *thumbnail.Image) {
|
||||
if w.cache == nil || img != nil && len(img.Data) > maxCachedSourceBytes {
|
||||
return
|
||||
}
|
||||
_ = w.cache.Put(key, img)
|
||||
|
||||
@@ -856,7 +856,7 @@ var _ = Describe("ThumbnailWorkflow", func() {
|
||||
It("sends the kept source instead of extracting a second time", func() {
|
||||
pngBytes, err := os.ReadFile(filepath.Join("..", "..", "preprocessor", "test_assets", "noise.png"))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(testCache.Put(sourceStorageKey("1f0c9f1b6e4b4b2a9c1d7f3e8a5b6c7d8e9f0a1b", "audio/mpeg", "oc-audio"), thumbnail.Image{Data: pngBytes, ContentType: "image/png"})).To(Succeed())
|
||||
Expect(testCache.Put(sourceStorageKey("1f0c9f1b6e4b4b2a9c1d7f3e8a5b6c7d8e9f0a1b", "audio/mpeg", "oc-audio"), &thumbnail.Image{Data: pngBytes, ContentType: "image/png"})).To(Succeed())
|
||||
|
||||
//the file itself is not an mp3: extracting it would fail, so what
|
||||
//reaches the generator can only come from the cache
|
||||
@@ -875,7 +875,7 @@ var _ = Describe("ThumbnailWorkflow", func() {
|
||||
})
|
||||
|
||||
It("asks once for a file that carries nothing, whatever size follows", func() {
|
||||
Expect(testCache.Put(sourceStorageKey("1f0c9f1b6e4b4b2a9c1d7f3e8a5b6c7d8e9f0a1b", "audio/mpeg", "oc-audio"), thumbnail.Image{})).To(Succeed())
|
||||
Expect(testCache.Put(sourceStorageKey("1f0c9f1b6e4b4b2a9c1d7f3e8a5b6c7d8e9f0a1b", "audio/mpeg", "oc-audio"), nil)).To(Succeed())
|
||||
|
||||
wf2, captured := setupPreprocessTest(GinkgoT(), gatewayClient, logger, testCache, "audio/mpeg", "song.mp3", []byte("not an mp3"))
|
||||
|
||||
@@ -895,7 +895,7 @@ var _ = Describe("ThumbnailWorkflow", func() {
|
||||
})
|
||||
|
||||
It("does not take another extractor's empty answer", func() {
|
||||
Expect(testCache.Put(sourceStorageKey("1f0c9f1b6e4b4b2a9c1d7f3e8a5b6c7d8e9f0a1b", "audio/mpeg", "tika"), thumbnail.Image{})).To(Succeed())
|
||||
Expect(testCache.Put(sourceStorageKey("1f0c9f1b6e4b4b2a9c1d7f3e8a5b6c7d8e9f0a1b", "audio/mpeg", "tika"), nil)).To(Succeed())
|
||||
|
||||
mp3Bytes, err := os.ReadFile(filepath.Join("..", "..", "preprocessor", "test_assets", "empty.mp3"))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
@@ -1063,7 +1063,7 @@ var _ = Describe("ThumbnailWorkflow", func() {
|
||||
Describe("cache hit shortcut", func() {
|
||||
It("should return cached thumbnail without calling generator or downloading the file", func() {
|
||||
cachedThumb := []byte("cached-thumbnail")
|
||||
err := testCache.Put(thumbnailStorageKey("cb56752477cae6405f85b131872c60d21b967c6a", "image/jpeg", 128, 128, "fill"), thumbnail.Image{Data: cachedThumb, ContentType: "image/png"})
|
||||
err := testCache.Put(thumbnailStorageKey("cb56752477cae6405f85b131872c60d21b967c6a", "image/jpeg", 128, 128, "fill"), &thumbnail.Image{Data: cachedThumb, ContentType: "image/png"})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
// No download expectations are set up. If the workflow tried to
|
||||
|
||||
Reference in new issue
Block a user