diff --git a/services/webdav/pkg/preprocessor/preprocessor.go b/services/webdav/pkg/preprocessor/preprocessor.go index 607ead90e7..89d1db0852 100644 --- a/services/webdav/pkg/preprocessor/preprocessor.go +++ b/services/webdav/pkg/preprocessor/preprocessor.go @@ -27,6 +27,8 @@ import ( // FileConverter is the interface for the file converter type FileConverter interface { Convert(r io.Reader) (*Image, error) + // Name tells cached results of different extractors apart. + Name() string } // Image is what a converter hands on: the bytes and the type they are in. @@ -47,6 +49,8 @@ type GgsDecoder struct { maxSize uint64 } +func (GgsDecoder) Name() string { return "oc-ggs" } + // Convert reads the ggs file and returns the thumbnail image. // // TODO: this parses user-provided bytes in-process (archive/zip streams the @@ -94,6 +98,8 @@ func (g GgsDecoder) Convert(r io.Reader) (*Image, error) { // Deprecated: Tika provides the cover art; kept for setups without Tika. type AudioDecoder struct{} +func (AudioDecoder) Name() string { return "oc-audio" } + // Convert reads the audio file and extracts the thumbnail image from the id3 tag. // // TODO: this parses user-provided bytes in-process (dhowden/tag reads the audio @@ -123,6 +129,8 @@ type TxtToImageConverter struct { fontLoader *FontLoader } +func (TxtToImageConverter) Name() string { return "oc-text" } + // Convert reads the text file and renders it into a thumbnail image func (t TxtToImageConverter) Convert(r io.Reader) (*Image, error) { img := image.NewRGBA(image.Rect(0, 0, 640, 480)) @@ -231,6 +239,8 @@ type GGPStruct struct { // Deprecated: see GgsDecoder. type GgpDecoder struct{} +func (GgpDecoder) Name() string { return "oc-ggp" } + // Convert reads the ggp file and returns the first thumbnail image. // // TODO: this parses user-provided bytes in-process (JSON/base64 extraction of diff --git a/services/webdav/pkg/preprocessor/tika.go b/services/webdav/pkg/preprocessor/tika.go index 56b2b813d2..1ef71b79fb 100644 --- a/services/webdav/pkg/preprocessor/tika.go +++ b/services/webdav/pkg/preprocessor/tika.go @@ -25,6 +25,8 @@ type TikaThumbnail struct { contentType string } +func (TikaThumbnail) Name() string { return "tika" } + // Convert hands the thumbnail on undecoded: the generator reads formats webdav // cannot, an .ico icon group out of a PE binary for one. func (t TikaThumbnail) Convert(r io.Reader) (*Image, error) { diff --git a/services/webdav/pkg/thumbnail/workflow/workflow.go b/services/webdav/pkg/thumbnail/workflow/workflow.go index d3f73d4a70..437a7b20d7 100644 --- a/services/webdav/pkg/thumbnail/workflow/workflow.go +++ b/services/webdav/pkg/thumbnail/workflow/workflow.go @@ -129,8 +129,8 @@ func thumbnailStorageKey(checksum, mimeType string, w, h int, format, operation } // sourceStorageKey holds the extracted image for all sizes; empty means the file has none. -func sourceStorageKey(checksum, mimeType string) string { - return storageKeyPrefix(checksum, mimeType) + "/source" +func sourceStorageKey(checksum, mimeType, extractor string) string { + return storageKeyPrefix(checksum, mimeType) + "/source-" + extractor } // storageKeyPrefix hashes the checksum because it is untrusted (OCM takes it from a @@ -464,11 +464,6 @@ func (w *ThumbnailWorkflow) generate(ctx context.Context, ref *providerv1beta1.R cached, err := w.cache.Get(cacheKey) switch { case err == nil: - // an empty entry is a file we know carries no thumbnail - if len(cached) == 0 { - return Thumbnail{}, ErrNoThumbnail - } - return Thumbnail{Data: cached, ContentType: outputType}, nil case !errors.Is(err, cache.ErrCacheMiss): // a miss is the normal answer; anything else is the cache itself @@ -483,13 +478,7 @@ func (w *ThumbnailWorkflow) generate(ctx context.Context, ref *providerv1beta1.R imgStream, srcType, cleanup, err := w.sourceImage(ctx, ref, auth, mimeType, tr.Filename, checksum, logger) if err != nil { if errors.Is(err, ErrNoThumbnail) { - // a file that carries no thumbnail is an answer, not a failure, and - // the empty entry keeps the next request from asking again logger.Debug().Err(err).Msg("no source image for thumbnail") - if w.cache != nil { - _ = w.cache.Put(cacheKey, nil) - } - return Thumbnail{}, err } @@ -553,8 +542,9 @@ func (w *ThumbnailWorkflow) sourceImage(ctx context.Context, ref *providerv1beta // what the extraction found is remembered per file, so a second size does // not send the file to Tika again, and a file that carries nothing is only // asked once. The answer is read before the file is downloaded at all. - sourceKey := sourceStorageKey(checksum, mimeType) + var sourceKey string if converter != nil && w.cache != nil { + sourceKey = sourceStorageKey(checksum, mimeType, converter.Name()) if cached, err := w.cache.Get(sourceKey); err == nil { if len(cached) == 0 { return nil, "", nil, ErrNoThumbnail diff --git a/services/webdav/pkg/thumbnail/workflow/workflow_test.go b/services/webdav/pkg/thumbnail/workflow/workflow_test.go index b7ed971f3f..4f96aff241 100644 --- a/services/webdav/pkg/thumbnail/workflow/workflow_test.go +++ b/services/webdav/pkg/thumbnail/workflow/workflow_test.go @@ -846,7 +846,7 @@ var _ = Describe("ThumbnailWorkflow", func() { _, err = wf2.Execute(context.Background(), tr, testToken, logger) Expect(err).ToNot(HaveOccurred()) - cached, err := testCache.Get(sourceStorageKey("1f0c9f1b6e4b4b2a9c1d7f3e8a5b6c7d8e9f0a1b", "audio/mpeg")) + cached, err := testCache.Get(sourceStorageKey("1f0c9f1b6e4b4b2a9c1d7f3e8a5b6c7d8e9f0a1b", "audio/mpeg", "oc-audio")) Expect(err).ToNot(HaveOccurred()) Expect(cached).To(Equal(captured.data), "the cover art is kept as the source, not per size") }) @@ -854,7 +854,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"), pngBytes)).To(Succeed()) + Expect(testCache.Put(sourceStorageKey("1f0c9f1b6e4b4b2a9c1d7f3e8a5b6c7d8e9f0a1b", "audio/mpeg", "oc-audio"), pngBytes)).To(Succeed()) //the file itself is not an mp3: extracting it would fail, so what //reaches the generator can only come from the cache @@ -873,7 +873,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"), nil)).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")) @@ -887,6 +887,28 @@ var _ = Describe("ThumbnailWorkflow", func() { _, err := wf2.Execute(context.Background(), tr, testToken, logger) Expect(err).To(MatchError(ErrNoThumbnail)) Expect(captured.data).To(BeEmpty(), "the generator is not asked at all") + + _, err = testCache.Get(thumbnailStorageKey("1f0c9f1b6e4b4b2a9c1d7f3e8a5b6c7d8e9f0a1b", "audio/mpeg", 256, 256, "jpg", "fill")) + Expect(err).To(MatchError(cache.ErrCacheMiss), "only the source entry says there is nothing") + }) + + It("does not take another extractor's empty answer", func() { + 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()) + wf2, captured := setupPreprocessTest(GinkgoT(), gatewayClient, logger, testCache, "audio/mpeg", "song.mp3", mp3Bytes) + + tr := &requests.ThumbnailRequest{ + Ref: spaceRef("storageid$spaceid!opaqueid", "song.mp3"), + Filename: "song.mp3", + Width: 64, + Height: 64, + } + + _, err = wf2.Execute(context.Background(), tr, testToken, logger) + Expect(err).ToNot(HaveOccurred()) + Expect(captured.data).ToNot(BeEmpty()) }) It("extracts the embedded thumbnail from geogebra slides", func() { @@ -1735,17 +1757,17 @@ var _ = Describe("postToGenerator", func() { var _ = Describe("storage keys", func() { It("keeps a checksum from leaving the cache directory", func() { - key := sourceStorageKey("../../../../../tmp/x", "audio/mpeg") - Expect(key).To(MatchRegexp(`^[0-9a-f]{2}/[0-9a-f]{2}/[0-9a-f]{60}/source$`)) + key := sourceStorageKey("../../../../../tmp/x", "audio/mpeg", "tika") + Expect(key).To(MatchRegexp(`^[0-9a-f]{2}/[0-9a-f]{2}/[0-9a-f]{60}/source-tika$`)) }) It("gives the same bytes under two types two source entries", func() { - Expect(sourceStorageKey("cb56752477cae6405f85b131872c60d21b967c6a", "application/pdf")). - ToNot(Equal(sourceStorageKey("cb56752477cae6405f85b131872c60d21b967c6a", "text/plain"))) + Expect(sourceStorageKey("cb56752477cae6405f85b131872c60d21b967c6a", "application/pdf", "tika")). + ToNot(Equal(sourceStorageKey("cb56752477cae6405f85b131872c60d21b967c6a", "text/plain", "oc-text"))) }) It("files a thumbnail and its source under one prefix", func() { - source := sourceStorageKey("cb56752477cae6405f85b131872c60d21b967c6a", "image/x-nikon-nef") + source := sourceStorageKey("cb56752477cae6405f85b131872c60d21b967c6a", "image/x-nikon-nef", "tika") thumb := thumbnailStorageKey("cb56752477cae6405f85b131872c60d21b967c6a", "image/x-nikon-nef", 64, 64, "jpg", "fill") Expect(path.Dir(thumb)).To(Equal(path.Dir(source))) })