mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-12-05 22:46:09 -05:00
refactor(webdav): let the preprocessor say what a type needs
The cache key is derived from configuration again: a thumbnail is written in the type the file's own type maps to, so the answer no longer depends on what Tika hands back and needs no placeholder in the key. Which types Tika gets is decided in one place now. An image type goes to Tika only when a deployment names it, because the generator behind the URL may read it itself, which also lets a raw photo reach Tika at all.
This commit is contained in:
1 parent
299586bf7d
commit
6b2c30219c
7 files changed
+58
-87
No files matched your search
@@ -10,7 +10,7 @@ Currently, the webdav service handles request for two functionalities, which are
|
||||
|
||||
The webdav service provides various `GET` endpoints to get the thumbnails of a file in authenticated and unauthenticated contexts. It also provides thumbnails for spaces on different endpoints.
|
||||
|
||||
When `OC_TIKA_URL` (or `WEBDAV_TIKA_URL`) is set, an Apache Tika server (4.2.0 or newer) supplies the preview image for documents, raw photos, audio cover art and other types. Every file that is not a directly supported image is sent to Tika; `OC_TIKA_THUMBNAIL_MIME_TYPES` (or `WEBDAV_TIKA_THUMBNAIL_MIME_TYPES`) restricts that to a list of mime types, where an entry like `image/x-nikon-nef:image/x-raw-nikon` maps a type to the one Tika knows the format by. Tika takes a declared type only when it refines what it detected itself, so an entry pins a subtype its detector left at the parent type, a raw photo it only read as a tiff. The webdav service calls `PUT /unpack/preset/thumbnail`, so the Tika server has to run with the `thumbnail` catalog preset active: start it with `TIKA_PRESETS=thumbnail`, a comma separated list of the presets to activate. Without Tika, audio cover art and GeoGebra thumbnails are still extracted by the webdav service itself; those built-in extractors are deprecated. A type Tika takes is attempted even when `OC_THUMBNAILS_ADDITIONAL_SOURCE_TYPES` does not announce it, so a direct request can get a preview that a listing reports as absent.
|
||||
When `OC_TIKA_URL` (or `WEBDAV_TIKA_URL`) is set, an Apache Tika server (4.2.0 or newer) supplies the preview image for documents, raw photos, audio cover art and other types. Every file that is not a directly supported image is sent to Tika; `OC_TIKA_THUMBNAIL_MIME_TYPES` (or `WEBDAV_TIKA_THUMBNAIL_MIME_TYPES`) restricts that to a list of mime types, where an entry like `image/x-nikon-nef:image/x-raw-nikon` maps a type to the one Tika knows the format by. Tika takes a declared type only when it refines what it detected itself, so an entry pins a subtype its detector left at the parent type, a raw photo it only read as a tiff. The webdav service calls `PUT /unpack/preset/thumbnail`, so the Tika server has to run with the `thumbnail` catalog preset active: start it with `TIKA_PRESETS=thumbnail`, a comma separated list of the presets to activate. Without Tika, audio cover art and GeoGebra thumbnails are still extracted by the webdav service itself; those built-in extractors are deprecated. A type Tika takes is attempted even when `OC_THUMBNAILS_ADDITIONAL_SOURCE_TYPES` does not announce it, so a direct request can get a preview that a listing reports as absent. An image type is only sent to Tika when the list names it, because the generator behind the URL may read it itself, an imagor with ImageMagick reads heic where the built-in one does not. Naming a type also restricts Tika to the named ones, so a deployment that sends raw photos to Tika has to name the document types it wants from Tika as well. The thumbnail is written in the type the **file's** own type maps to (`OC_THUMBNAILS_ADDITIONAL_OUTPUT_TYPES`), not in whatever type Tika hands back, so the answer is the same before and after a cache hit. That is also why a windows binary is mapped to `image/png` by default: the icon Tika reads out of it carries transparency that a JPEG would flatten onto black.
|
||||
|
||||
Generated thumbnails are cached by the webdav service itself. The cache backend defaults to `file`, storing entries under `$OC_BASE_DATA_PATH/thumbnails/cache` (override with `WEBDAV_THUMBNAILS_CACHE_BACKEND` and `WEBDAV_THUMBNAILS_CACHE_DIR`). Use the `s3` backend when running multiple instances behind a load balancer so they share one cache. A backend nobody can serve, an unknown name or an `s3` backend without bucket, region and credentials, fails at startup instead of serving without a cache, as long as previews are enabled. A service that is not AWS needs `WEBDAV_THUMBNAILS_CACHE_S3_ENDPOINT` too, and it is always addressed over https. The `memory` backend is bounded and drops its oldest entries; `none` disables caching on purpose.
|
||||
|
||||
|
||||
@@ -410,16 +410,14 @@ func ForType(mimeType string, opts Options) FileConverter {
|
||||
case "audio/ogg":
|
||||
return tikaOr(opts, mimeType, AudioDecoder{})
|
||||
default:
|
||||
// webdav decodes no image: a type without a converter goes to Tika when
|
||||
// it is configured for it, and is otherwise streamed to the generator
|
||||
// untouched, which answers whether it can read it.
|
||||
// an image reaches the generator as it is: it reads the common types and
|
||||
// may read more, an imagor with ImageMagick reads heic where we do not.
|
||||
// OC_TIKA_THUMBNAIL_MIME_TYPES is how a deployment hands an image type
|
||||
// to Tika instead.
|
||||
if strings.HasPrefix(mimeType, "image/") && !opts.Tika.ConfiguredFor(mimeType) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return tikaOr(opts, mimeType, nil)
|
||||
}
|
||||
}
|
||||
|
||||
// Encoded is an image a converter hands on as it found it. The type decides
|
||||
// the format the thumbnail is written in.
|
||||
type Encoded struct {
|
||||
ContentType string
|
||||
Data []byte
|
||||
}
|
||||
@@ -141,10 +141,11 @@ var _ = Describe("ForType with a Tika server", func() {
|
||||
It("routes everything Tika is asked for to Tika, text and gif stay native", func() {
|
||||
opts := Options{Tika: thumbnail.NewTika("http://tika:9998", nil), Filename: "song.mp3"}
|
||||
Expect(ForType("audio/mpeg", opts)).To(BeAssignableToTypeOf(TikaThumbnail{}))
|
||||
Expect(ForType("image/x-nikon-nef", opts)).To(BeAssignableToTypeOf(TikaThumbnail{}))
|
||||
Expect(ForType("application/vnd.geogebra.slides", opts)).To(BeAssignableToTypeOf(TikaThumbnail{}))
|
||||
Expect(ForType("application/pdf", opts)).To(BeAssignableToTypeOf(TikaThumbnail{}))
|
||||
Expect(ForType("text/plain", opts)).To(BeAssignableToTypeOf(TxtToImageConverter{}))
|
||||
Expect(ForType("image/png", opts)).To(BeNil(), "the generator reads a png itself")
|
||||
Expect(ForType("image/x-nikon-nef", opts)).To(BeNil(), "an image type the catch-all does not take, the generator may read it")
|
||||
})
|
||||
|
||||
It("keeps the in-process converters without a Tika server", func() {
|
||||
@@ -157,6 +158,6 @@ var _ = Describe("ForType with a Tika server", func() {
|
||||
opts := Options{Tika: thumbnail.NewTika("http://tika:9998", []string{"application/pdf", "image/x-nikon-nef:image/x-raw-nikon"})}
|
||||
Expect(ForType("application/pdf", opts)).To(BeAssignableToTypeOf(TikaThumbnail{}))
|
||||
Expect(ForType("audio/mpeg", opts)).To(BeAssignableToTypeOf(AudioDecoder{}), "not in the configured list")
|
||||
Expect(ForType("image/x-nikon-nef", opts).(TikaThumbnail).contentType).To(Equal("image/x-raw-nikon"))
|
||||
Expect(ForType("image/x-nikon-nef", opts).(TikaThumbnail).contentType).To(Equal("image/x-raw-nikon"), "a named image type does go to Tika")
|
||||
})
|
||||
})
|
||||
@@ -6,11 +6,10 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// DefaultSourceTypes are the types a preview is announced and attempted for
|
||||
// without further configuration. The image types are what both resize backends
|
||||
// read; the others are converted to an image here, by a preprocessor, before a
|
||||
// generator ever sees them.
|
||||
var DefaultSourceTypes = []string{
|
||||
// GeneratorImageTypes are the image types both resize backends read, so webdav
|
||||
// hands the file to the generator as it is. Every other type needs something to
|
||||
// make an image of it first, a preprocessor here or a Tika server.
|
||||
var GeneratorImageTypes = []string{
|
||||
"image/png",
|
||||
"image/jpg",
|
||||
"image/jpeg",
|
||||
@@ -19,13 +18,18 @@ var DefaultSourceTypes = []string{
|
||||
"image/x-ms-bmp",
|
||||
"image/tiff",
|
||||
"image/webp",
|
||||
}
|
||||
|
||||
// DefaultSourceTypes are the types a preview is announced and attempted for
|
||||
// without further configuration.
|
||||
var DefaultSourceTypes = slices.Concat(GeneratorImageTypes, []string{
|
||||
"text/plain",
|
||||
"audio/flac",
|
||||
"audio/mpeg",
|
||||
"audio/ogg",
|
||||
"application/vnd.geogebra.slides",
|
||||
"application/vnd.geogebra.pinboard",
|
||||
}
|
||||
})
|
||||
|
||||
// SourceTypes are the types a preview is announced and attempted for: the
|
||||
// defaults plus what a deployment configured, which is also the list the
|
||||
|
||||
@@ -45,6 +45,23 @@ func (t Tika) Supports(mimeType string) bool {
|
||||
return all
|
||||
}
|
||||
|
||||
// ConfiguredFor reports whether OC_TIKA_THUMBNAIL_MIME_TYPES lists this type,
|
||||
// as opposed to the catch-all taking it. An image is only taken away from the
|
||||
// generator when the configuration asks for it, because the generator may well
|
||||
// read the type itself.
|
||||
func (t Tika) ConfiguredFor(mimeType string) bool {
|
||||
if t.URL == "" {
|
||||
return false
|
||||
}
|
||||
m, _, err := mime.ParseMediaType(mimeType)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
_, named := t.mimeTypes[m]
|
||||
|
||||
return named
|
||||
}
|
||||
|
||||
// ContentType is the type Tika is told, mapped where configured. Tika takes a
|
||||
// declared type only when it refines what it detected itself, so a mapping pins
|
||||
// a subtype its detector left at the parent type, a raw photo it only read as a
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
package workflow
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("thumbnail format", func() {
|
||||
DescribeTable("typeOfThumbnail reads the type back out of cached bytes",
|
||||
func(data []byte, contentType string) {
|
||||
Expect(typeOfThumbnail(data, "fallback")).To(Equal(contentType))
|
||||
},
|
||||
Entry("png", []byte{0x89, 'P', 'N', 'G', 0x0d, 0x0a, 0x1a, 0x0a}, "image/png"),
|
||||
Entry("jpeg", []byte{0xff, 0xd8, 0xff, 0xe0}, "image/jpeg"),
|
||||
Entry("gif", []byte("GIF89a"), "image/gif"),
|
||||
Entry("anything else keeps the fallback", []byte("whatever"), "fallback"),
|
||||
)
|
||||
})
|
||||
@@ -77,25 +77,6 @@ var ErrFileProcessing = fmt.Errorf("file is processing")
|
||||
// cached as an empty entry, so the next request does not ask Tika again.
|
||||
var ErrNoThumbnail = errors.New("thumbnails: the file has no thumbnail")
|
||||
|
||||
// tikaFormatPlaceholder stands in the cache key for a format only Tika's answer
|
||||
// reveals, so the entry is found without downloading the file first.
|
||||
const tikaFormatPlaceholder = "tika"
|
||||
|
||||
// typeOfThumbnail reads the type of bytes that were cached under the
|
||||
// placeholder, because the key cannot say what they turned out to be.
|
||||
func typeOfThumbnail(data []byte, fallback string) string {
|
||||
switch {
|
||||
case bytes.HasPrefix(data, []byte{0x89, 'P', 'N', 'G'}):
|
||||
return "image/png"
|
||||
case bytes.HasPrefix(data, []byte{0xff, 0xd8}):
|
||||
return "image/jpeg"
|
||||
case bytes.HasPrefix(data, []byte("GIF8")):
|
||||
return "image/gif"
|
||||
}
|
||||
|
||||
return fallback
|
||||
}
|
||||
|
||||
// ErrImageTooLarge is returned when the input image exceeds the configured
|
||||
// maximum width/height. Callers should surface this as HTTP 403 Forbidden.
|
||||
var ErrImageTooLarge = errors.New("thumbnails: image is too large")
|
||||
@@ -451,17 +432,14 @@ func (w *ThumbnailWorkflow) generate(ctx context.Context, ref *providerv1beta1.R
|
||||
}
|
||||
mimeType := info.GetMimeType()
|
||||
|
||||
// The thumbnail is returned in the type the source type maps to, asked for by
|
||||
// the name the generator's filter uses.
|
||||
// The thumbnail is returned in the type the source type maps to, asked for
|
||||
// by the name the generator's filter uses. What an extractor hands on is
|
||||
// re-encoded to that type, because the request has to decide it: a cache
|
||||
// hit knows the key, not the bytes. Keeping a png cover a png needs the
|
||||
// embedded type before the key is built, which is what the preview presence
|
||||
// work puts into the file's metadata.
|
||||
outputType, outputFormat := w.outputFor(mimeType)
|
||||
|
||||
// What Tika answers with is only known once it has answered, so the entry is
|
||||
// keyed under a placeholder until then.
|
||||
fromTika := !strings.HasPrefix(mimeType, "image/") && w.tika.Supports(mimeType)
|
||||
if fromTika {
|
||||
outputFormat = tikaFormatPlaceholder
|
||||
}
|
||||
|
||||
// The operation is resolved up front so it can be part of the cache key: a
|
||||
// fill and a fit-in request with the same dimensions produce different images
|
||||
// and must not share a cache entry. noUpscale reports whether the generator
|
||||
@@ -490,10 +468,6 @@ func (w *ThumbnailWorkflow) generate(ctx context.Context, ref *providerv1beta1.R
|
||||
if len(cached) == 0 {
|
||||
return Thumbnail{}, ErrNoThumbnail
|
||||
}
|
||||
if fromTika {
|
||||
// the format was not known when the entry was written
|
||||
return Thumbnail{Data: cached, ContentType: typeOfThumbnail(cached, outputType)}, nil
|
||||
}
|
||||
|
||||
return Thumbnail{Data: cached, ContentType: outputType}, nil
|
||||
case !errors.Is(err, cache.ErrCacheMiss):
|
||||
@@ -503,11 +477,6 @@ func (w *ThumbnailWorkflow) generate(ctx context.Context, ref *providerv1beta1.R
|
||||
}
|
||||
}
|
||||
|
||||
// webdav snaps the requested size onto a configured resolution and lets the
|
||||
// generator fill that box. Only fit-in suppresses upscaling (noUpscale); for
|
||||
// fill/thumbnail/resize the generator may enlarge a small source (e.g. a text
|
||||
// file rendered at a fixed canvas) to fill the snapped box, which is what
|
||||
// imagor's default resize does.
|
||||
// Produce the source image to send to the generator, never decoding an image
|
||||
// in webdav: real images (incl. gif) are streamed straight through undecoded;
|
||||
// non-image sources are converted to image bytes here so the generator always
|
||||
@@ -531,11 +500,11 @@ func (w *ThumbnailWorkflow) generate(ctx context.Context, ref *providerv1beta1.R
|
||||
}
|
||||
defer cleanup()
|
||||
|
||||
if fromTika {
|
||||
// only now is the type known that Tika answered with
|
||||
outputType, outputFormat = w.outputFor(srcType)
|
||||
}
|
||||
|
||||
// webdav snaps the requested size onto a configured resolution and lets the
|
||||
// generator fill that box. Only fit-in suppresses upscaling (noUpscale); for
|
||||
// fill/thumbnail/resize the generator may enlarge a small source (e.g. a text
|
||||
// file rendered at a fixed canvas) to fill the snapped box, which is what
|
||||
// imagor's default resize does.
|
||||
genURL := generator.BuildURL(w.generatorURL, int32(box.Dx()), int32(box.Dy()), operation, outputFormat, noUpscale)
|
||||
thumbBytes, thumbType, err := w.postToGenerator(ctx, genURL, imgStream, tr.Filename, srcType)
|
||||
if err != nil {
|
||||
@@ -571,24 +540,24 @@ func (w *ThumbnailWorkflow) generate(ctx context.Context, ref *providerv1beta1.R
|
||||
func (w *ThumbnailWorkflow) sourceImage(ctx context.Context, ref *providerv1beta1.Reference, auth, mimeType, filename string, logger log.Logger) (io.Reader, string, func(), error) {
|
||||
m, _, _ := mime.ParseMediaType(mimeType)
|
||||
|
||||
converter := preprocessor.ForType(m, preprocessor.Options{
|
||||
// the preprocessor decides what a type needs. No converter means the file
|
||||
// is handed to the generator as it is, which is the case for every type it
|
||||
// reads itself.
|
||||
converter := preprocessor.ForType(mimeType, preprocessor.Options{
|
||||
FontMapFile: w.fontMapFile,
|
||||
MaxImageSize: w.maxInputSize,
|
||||
Tika: w.tika,
|
||||
Filename: filename,
|
||||
})
|
||||
if strings.HasPrefix(m, "image/") || converter == nil {
|
||||
body, err := w.fileDownloader.DownloadStream(ctx, ref, auth)
|
||||
if err != nil {
|
||||
return nil, "", nil, fmt.Errorf("download: %w", err)
|
||||
}
|
||||
return body, m, func() { _ = body.Close() }, nil
|
||||
}
|
||||
|
||||
body, err := w.fileDownloader.DownloadStream(ctx, ref, auth)
|
||||
if err != nil {
|
||||
return nil, "", nil, fmt.Errorf("download: %w", err)
|
||||
}
|
||||
|
||||
if converter == nil {
|
||||
return body, m, func() { _ = body.Close() }, nil
|
||||
}
|
||||
defer body.Close()
|
||||
|
||||
fileBytes, err := io.ReadAll(body)
|
||||
|
||||
Reference in new issue
Block a user