diff --git a/services/webdav/README.md b/services/webdav/README.md index 89f9bed07c..258b3a1ce1 100644 --- a/services/webdav/README.md +++ b/services/webdav/README.md @@ -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, which is what the default `*` in `OC_TIKA_THUMBNAIL_MIME_TYPES` (or `WEBDAV_TIKA_THUMBNAIL_MIME_TYPES`) says. That setting restricts it to a list of mime types instead, 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 the leaf type its detector left at the parent, a raw photo it only read as a plain tiff. The type decides which parser reads the file: a raw read as a tiff is parsed as a tiff, its maker notes stay unread, and the embedded preview a thumbnail would be made of is never found. The formats listed above are detected down to the leaf type, so none of them needs the hint, and it is there for the next format whose detection stops one level short. 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, and an empty list sends nothing at all, which leaves the Tika server to the search service alone. 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. +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. Which types go there is a list, and the default is the formats a Tika release serves a thumbnail for: documents, ebooks, office and iWork files, audio cover art and the camera raws. `OC_TIKA_THUMBNAIL_MIME_TYPES` (or `WEBDAV_TIKA_THUMBNAIL_MIME_TYPES`) replaces that list, `OC_TIKA_ADDITIONAL_THUMBNAIL_MIME_TYPES` adds to it, a single `*` in either sends every type that nothing else claims, and an empty list sends nothing at all, which leaves a Tika server shared with the search service to the search service alone. 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 the leaf type its detector left at the parent, a raw photo it only read as a plain tiff. The type decides which parser reads the file: a raw read as a tiff is parsed as a tiff, its maker notes stay unread, and the embedded preview a thumbnail would be made of is never found. The formats in the default list are detected down to the leaf type, so none of them needs the hint, and it is there for the next format whose detection stops one level short. 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. Who gets a file is decided in one order, first match wins: a type the Tika list names goes to Tika, which is why the camera raws are named there, reva types them as images and no generator reads them; then an image the generator reads is handed over as it is, and for an image type that is what `OC_THUMBNAILS_ADDITIONAL_SOURCE_TYPES` says, an imagor with ImageMagick reads heic where the built-in one does not; then the built-in extractors take text, audio and GeoGebra; then a `*` takes whatever is left. A type nothing claims answers `404` without reading the file at all. 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, and an empty list sends nothing at all, which leaves the Tika server to the search service alone. 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. The cache also keeps the image an extractor made of a file, under one entry per file, so a second size does not extract again and a file that carries no image is only asked once. Nothing expires: a changed file is a new entry because the key carries its checksum, but an extractor that learns to read a format it could not read before does not reach the files it already answered for. After such an upgrade, an Apache Tika release among them, empty the cache (`WEBDAV_THUMBNAILS_CACHE_DIR`, or the bucket) to have them asked again. diff --git a/services/webdav/pkg/config/config.go b/services/webdav/pkg/config/config.go index de5628538f..bb5f997e57 100644 --- a/services/webdav/pkg/config/config.go +++ b/services/webdav/pkg/config/config.go @@ -28,8 +28,9 @@ type Config struct { Thumbnails Thumbnails `yaml:"thumbnails"` - TikaURL string `yaml:"tika_url" env:"OC_TIKA_URL;WEBDAV_TIKA_URL" desc:"URL of an Apache Tika server. When set, thumbnails are generated for documents, raw photos, audio cover art and other formats Tika can extract a preview from. Needs Tika 4.2.0 or newer, started with TIKA_PRESETS=thumbnail. A 4.1.x server works as well, with the preset activated in its config file instead." introductionVersion:"%%NEXT%%"` - TikaThumbnailMimeTypes []string `yaml:"tika_thumbnail_mime_types" env:"OC_TIKA_THUMBNAIL_MIME_TYPES;WEBDAV_TIKA_THUMBNAIL_MIME_TYPES" desc:"Restrict thumbnail generation via Tika to these mime types. The default, a single '*', sends every type that is not handled directly and lets Tika decide; an empty list sends nothing, which leaves the Tika server behind OC_TIKA_URL to the search service alone. An entry can map a type to the one Tika knows the format by, e.g. image/x-nikon-nef:image/x-raw-nikon. Tika takes a declared type only when it refines what it detected itself, so an entry pins the leaf type its detector left at the parent, a raw photo it only read as a plain tiff. The type decides which parser reads the file, so a raw read as a tiff loses its maker notes and with them the embedded preview; the formats shipped here are detected down to the leaf type, so no entry is needed for them." introductionVersion:"%%NEXT%%"` + TikaURL string `yaml:"tika_url" env:"OC_TIKA_URL;WEBDAV_TIKA_URL" desc:"URL of an Apache Tika server. When set, thumbnails are generated for documents, raw photos, audio cover art and other formats Tika can extract a preview from. Needs Tika 4.2.0 or newer, started with TIKA_PRESETS=thumbnail. A 4.1.x server works as well, with the preset activated in its config file instead." introductionVersion:"%%NEXT%%"` + TikaThumbnailMimeTypes []string `yaml:"tika_thumbnail_mime_types" env:"OC_TIKA_THUMBNAIL_MIME_TYPES;WEBDAV_TIKA_THUMBNAIL_MIME_TYPES" desc:"The mime types sent to Tika for their thumbnail, which replaces the built-in list of the formats a Tika release serves one for. The camera raws are in that list because reva types them as images and an image is handed to the generator as it is unless something claims it. A single '*' sends every type that nothing else claims, an empty list leaves the built-in list in place. An entry can map a type to the one Tika knows the format by, e.g. image/x-nikon-nef:image/x-raw-nikon. Tika takes a declared type only when it refines what it detected itself, so an entry pins the leaf type its detector left at the parent, a raw photo it only read as a plain tiff. The type decides which parser reads the file, so a raw read as a tiff loses its maker notes and with them the embedded preview; the formats shipped here are detected down to the leaf type, so no entry is needed for them." introductionVersion:"%%NEXT%%"` + AdditionalTikaThumbnailMimeTypes []string `yaml:"additional_tika_thumbnail_mime_types" env:"OC_TIKA_ADDITIONAL_THUMBNAIL_MIME_TYPES;WEBDAV_TIKA_ADDITIONAL_THUMBNAIL_MIME_TYPES" desc:"Additional mime types sent to Tika for their thumbnail, on top of the built-in list or of whatever OC_TIKA_THUMBNAIL_MIME_TYPES names instead. An entry carries a type hint the same way." introductionVersion:"%%NEXT%%"` Context context.Context `yaml:"-"` } diff --git a/services/webdav/pkg/config/defaults/defaultconfig.go b/services/webdav/pkg/config/defaults/defaultconfig.go index c668cbb211..6706199fb8 100644 --- a/services/webdav/pkg/config/defaults/defaultconfig.go +++ b/services/webdav/pkg/config/defaults/defaultconfig.go @@ -7,6 +7,7 @@ import ( "github.com/opencloud-eu/opencloud/pkg/shared" "github.com/opencloud-eu/opencloud/pkg/structs" "github.com/opencloud-eu/opencloud/services/webdav/pkg/config" + "github.com/opencloud-eu/opencloud/services/webdav/pkg/thumbnail" ) // FullDefaultConfig returns a fully initialized default configuration @@ -44,7 +45,7 @@ func DefaultConfig() *config.Config { WebdavNamespace: "/users/{{.Id.OpaqueId}}", RevaGateway: shared.DefaultRevaConfig().Address, - TikaThumbnailMimeTypes: []string{"*"}, + TikaThumbnailMimeTypes: thumbnail.DefaultTikaTypes, Thumbnails: config.Thumbnails{ GeneratorURL: "http://127.0.0.1:9186", diff --git a/services/webdav/pkg/preprocessor/preprocessor.go b/services/webdav/pkg/preprocessor/preprocessor.go index 41953c9264..1003a7f832 100644 --- a/services/webdav/pkg/preprocessor/preprocessor.go +++ b/services/webdav/pkg/preprocessor/preprocessor.go @@ -362,6 +362,7 @@ type Options struct { FontFace *opentype.FaceOptions MaxImageSize uint64 Tika thumbnail.Tika + SourceTypes thumbnail.SourceTypes Filename string } @@ -374,11 +375,32 @@ func tikaOr(opts Options, mimeType string, fallback FileConverter) FileConverter return TikaThumbnail{tikaURL: opts.Tika.URL, filename: opts.Filename, contentType: opts.Tika.ContentType(mimeType)} } -// ForType returns the converter for the specified mimeType -func ForType(mimeType string, opts Options) FileConverter { - // We can ignore the error here because we parse it in IsMimeTypeSupported before and if it fails - // return the service call. So we should only get here when the mimeType parses fine. +// ForType says what has to happen to a file of this type before the generator +// sees it: a converter to run, nil when the generator reads the file as it is, +// and ErrNoThumbnail when nothing here can make an image of it. +// +// The order is the answer to two questions that look the same and are not, what +// a type is and who can serve it: +// 1. a type the configuration names for Tika goes to Tika, an image too, which +// is how a raw photo is extracted instead of being handed to a generator +// that cannot read it +// 2. an image the generator reads is handed over untouched +// 3. a type with a converter here is converted here +// 4. what is left goes to Tika when it was told to take everything +// 5. and otherwise nothing can make a preview of it +func ForType(mimeType string, opts Options) (FileConverter, error) { + // We can ignore the error here because the caller parses it as well, so we + // only get here when the mimeType parses fine. mimeType, _, _ = mime.ParseMediaType(mimeType) + + if opts.Tika.ConfiguredFor(mimeType) { + return tikaOr(opts, mimeType, nil), nil + } + + if opts.SourceTypes.GeneratorReads(mimeType) { + return nil, nil + } + switch mimeType { case "text/plain": fontFace := opts.FontFace @@ -396,28 +418,19 @@ func ForType(mimeType string, opts Options) FileConverter { // try to use the default font fontLoader, _ = NewFontLoader("", fontFace) } - return TxtToImageConverter{ - fontLoader: fontLoader, - } - case "application/vnd.geogebra.slides": - return tikaOr(opts, mimeType, GgsDecoder{thumbnailpath: "_slide0/geogebra_thumbnail.png", maxSize: maxExtractedSize(opts)}) - case "application/vnd.geogebra.pinboard": - return tikaOr(opts, mimeType, GgpDecoder{}) - case "audio/flac": - fallthrough - case "audio/mpeg": - fallthrough - case "audio/ogg": - return tikaOr(opts, mimeType, AudioDecoder{}) - default: - // 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) + return TxtToImageConverter{fontLoader: fontLoader}, nil + case "application/vnd.geogebra.slides": + return tikaOr(opts, mimeType, GgsDecoder{thumbnailpath: "_slide0/geogebra_thumbnail.png", maxSize: maxExtractedSize(opts)}), nil + case "application/vnd.geogebra.pinboard": + return tikaOr(opts, mimeType, GgpDecoder{}), nil + case "audio/flac", "audio/mpeg", "audio/ogg": + return tikaOr(opts, mimeType, AudioDecoder{}), nil } + + if converter := tikaOr(opts, mimeType, nil); converter != nil { + return converter, nil + } + + return nil, ErrNoThumbnail } diff --git a/services/webdav/pkg/preprocessor/preprocessor_test.go b/services/webdav/pkg/preprocessor/preprocessor_test.go index bc9eedb8eb..436f738c58 100644 --- a/services/webdav/pkg/preprocessor/preprocessor_test.go +++ b/services/webdav/pkg/preprocessor/preprocessor_test.go @@ -181,8 +181,16 @@ var _ = Describe("preprocessor", func() { Expect(ForType("text/plain", Options{})).To(BeAssignableToTypeOf(TxtToImageConverter{})) }) - It("has no converter for an unknown type", func() { - Expect(ForType("unknown", Options{})).To(BeNil()) + It("refuses a type nothing here can read", func() { + converter, err := ForType("application/pdf", Options{}) + Expect(err).To(MatchError(ErrNoThumbnail), "without a Tika server nothing makes an image of a pdf") + Expect(converter).To(BeNil()) + }) + + It("refuses an image the generator does not read, instead of handing it over", func() { + converter, err := ForType("image/x-nikon-nef", Options{}) + Expect(err).To(MatchError(ErrNoThumbnail)) + Expect(converter).To(BeNil()) }) }) }) diff --git a/services/webdav/pkg/preprocessor/tika_test.go b/services/webdav/pkg/preprocessor/tika_test.go index 4b1c86c484..7f1ff9304d 100644 --- a/services/webdav/pkg/preprocessor/tika_test.go +++ b/services/webdav/pkg/preprocessor/tika_test.go @@ -138,26 +138,45 @@ var _ = Describe("TikaThumbnail", func() { }) 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", []string{"*"}), Filename: "song.mp3"} + sourceTypes := thumbnail.NewSourceTypes(nil) + + It("sends what the list names to Tika, even an image, and leaves the generator its own", func() { + opts := Options{Tika: thumbnail.NewTika("http://tika:9998", thumbnail.DefaultTikaTypes, nil), SourceTypes: sourceTypes, Filename: "song.mp3"} Expect(ForType("audio/mpeg", opts)).To(BeAssignableToTypeOf(TikaThumbnail{})) Expect(ForType("application/vnd.geogebra.slides", opts)).To(BeAssignableToTypeOf(TikaThumbnail{})) Expect(ForType("application/pdf", opts)).To(BeAssignableToTypeOf(TikaThumbnail{})) + Expect(ForType("image/x-nikon-nef", opts)).To(BeAssignableToTypeOf(TikaThumbnail{}), "a raw is named, so it is extracted instead of handed over") 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("hands an image type the deployment announced to the generator", func() { + opts := Options{ + Tika: thumbnail.NewTika("http://tika:9998", thumbnail.DefaultTikaTypes, nil), + SourceTypes: thumbnail.NewSourceTypes([]string{"image/heic"}), + } + Expect(ForType("image/heic", opts)).To(BeNil(), "a generator that reads heic is told so through the source types") + }) + + It("takes what nothing else claims when it is told to take everything", func() { + opts := Options{Tika: thumbnail.NewTika("http://tika:9998", []string{"*"}, nil), SourceTypes: sourceTypes} + Expect(ForType("application/x-anything", opts)).To(BeAssignableToTypeOf(TikaThumbnail{})) + Expect(ForType("image/x-nikon-nef", opts)).To(BeAssignableToTypeOf(TikaThumbnail{}), "an image the generator does not read is not an exception") + Expect(ForType("image/jpeg", opts)).To(BeNil(), "but one it reads never travels to Tika") }) It("keeps the in-process converters without a Tika server", func() { Expect(ForType("audio/mpeg", Options{})).To(BeAssignableToTypeOf(AudioDecoder{})) Expect(ForType("application/vnd.geogebra.slides", Options{})).To(BeAssignableToTypeOf(GgsDecoder{})) - Expect(ForType("image/x-nikon-nef", Options{})).To(BeNil(), "an image reaches the generator undecoded") }) - It("follows a configured list and hands the mapped type to Tika", func() { - opts := Options{Tika: thumbnail.NewTika("http://tika:9998", []string{"application/pdf", "image/x-nikon-nef:image/x-raw-nikon"})} + It("hands the mapped type to Tika", func() { + opts := Options{Tika: thumbnail.NewTika("http://tika:9998", []string{"application/pdf", "image/x-nikon-nef:image/x-raw-nikon"}, nil), SourceTypes: sourceTypes} 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"), "a named image type does go to Tika") + + converter, err := ForType("image/x-nikon-nef", opts) + Expect(err).ToNot(HaveOccurred()) + Expect(converter.(TikaThumbnail).contentType).To(Equal("image/x-raw-nikon")) }) }) diff --git a/services/webdav/pkg/service/v0/service.go b/services/webdav/pkg/service/v0/service.go index f75457df3b..0dea3ba24c 100644 --- a/services/webdav/pkg/service/v0/service.go +++ b/services/webdav/pkg/service/v0/service.go @@ -216,7 +216,7 @@ func newThumbnailWorkflow(conf *config.Config, sourceTypes thumbnail.SourceTypes workflow.WithMaxInputSize(maxInputSize), workflow.WithSizes(sizes), workflow.WithSourceTypes(sourceTypes), - workflow.WithTika(thumbnail.NewTika(conf.TikaURL, conf.TikaThumbnailMimeTypes)), + workflow.WithTika(thumbnail.NewTika(conf.TikaURL, conf.TikaThumbnailMimeTypes, conf.AdditionalTikaThumbnailMimeTypes)), workflow.WithFormats(formats), workflow.WithTypeMapping(typeMapping), workflow.WithWebdavNamespace(conf.WebdavNamespace), diff --git a/services/webdav/pkg/thumbnail/sourcetypes.go b/services/webdav/pkg/thumbnail/sourcetypes.go index 63d1ba0128..cdc11cf3c9 100644 --- a/services/webdav/pkg/thumbnail/sourcetypes.go +++ b/services/webdav/pkg/thumbnail/sourcetypes.go @@ -54,6 +54,22 @@ func NewSourceTypes(entries []string) SourceTypes { return t } +// GeneratorReads reports whether the generator is handed a file of this type as +// it is. Only images are, and which ones is what the source types say: the +// built-in list is what both resize backends read, a deployment adds the types +// its own generator reads on top of it. +func (t SourceTypes) GeneratorReads(contentType string) bool { + parsed, _, err := mime.ParseMediaType(contentType) + if err != nil || !strings.HasPrefix(parsed, "image/") { + return false + } + if len(t.byType) == 0 { + return slices.Contains(GeneratorImageTypes, parsed) + } + + return t.Supports(parsed) +} + // Supports reports whether a preview is made of this type. func (t SourceTypes) Supports(contentType string) bool { parsed, _, err := mime.ParseMediaType(contentType) diff --git a/services/webdav/pkg/thumbnail/sourcetypes_test.go b/services/webdav/pkg/thumbnail/sourcetypes_test.go new file mode 100644 index 0000000000..26dee184dc --- /dev/null +++ b/services/webdav/pkg/thumbnail/sourcetypes_test.go @@ -0,0 +1,32 @@ +package thumbnail_test + +import ( + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "github.com/opencloud-eu/opencloud/services/webdav/pkg/thumbnail" +) + +var _ = Describe("SourceTypes", func() { + defaults := thumbnail.NewSourceTypes(nil) + withHeic := thumbnail.NewSourceTypes([]string{"image/heic"}) + + DescribeTable("GeneratorReads", + func(types thumbnail.SourceTypes, mimeType string, want bool) { + Expect(types.GeneratorReads(mimeType)).To(Equal(want)) + }, + Entry("an image both backends read", defaults, "image/jpeg", true), + Entry("with parameters", defaults, "image/png; charset=binary", true), + Entry("not a camera raw, which is an image nobody renders", defaults, "image/x-nikon-nef", false), + Entry("not a document, whatever the list says", defaults, "application/pdf", false), + Entry("not audio, which is announced but extracted", defaults, "audio/mpeg", false), + Entry("an image type the deployment added", withHeic, "image/heic", true), + Entry("and the built-in ones are still in there", withHeic, "image/tiff", true), + Entry("an unparsable type", defaults, "not a mime", false), + ) + + It("falls back to the built-in image types when nothing was configured", func() { + Expect(thumbnail.SourceTypes{}.GeneratorReads("image/jpeg")).To(BeTrue()) + Expect(thumbnail.SourceTypes{}.GeneratorReads("image/heic")).To(BeFalse()) + }) +}) diff --git a/services/webdav/pkg/thumbnail/tika.go b/services/webdav/pkg/thumbnail/tika.go index 0becb9b311..f8f892038e 100644 --- a/services/webdav/pkg/thumbnail/tika.go +++ b/services/webdav/pkg/thumbnail/tika.go @@ -2,23 +2,79 @@ package thumbnail import ( "mime" + "slices" "strings" ) +// DefaultTikaTypes are the types sent to a Tika server for their thumbnail, +// the formats a Tika release serves one for. The camera raws have to be named +// because reva types them as images, and an image is handed to the generator +// as it is unless something claims it. +var DefaultTikaTypes = []string{ + "application/pdf", + "application/epub+zip", + "application/vnd.ms-xpsdocument", + + "application/msword", + "application/vnd.openxmlformats-officedocument.wordprocessingml.document", + "application/vnd.ms-powerpoint", + "application/vnd.openxmlformats-officedocument.presentationml.presentation", + "application/vnd.ms-excel", + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + "application/vnd.oasis.opendocument.text", + "application/vnd.oasis.opendocument.spreadsheet", + "application/vnd.oasis.opendocument.presentation", + "application/vnd.apple.pages", + "application/vnd.apple.numbers", + "application/vnd.apple.keynote", + + "application/vnd.geogebra.file", + "application/vnd.geogebra.slides", + "application/vnd.geogebra.pinboard", + + "audio/flac", + "audio/mpeg", + "audio/ogg", + "audio/x-flac", + "audio/x-m4a", + + "application/x-msdownload", + + "image/x-adobe-dng", + "image/x-canon-cr2", + "image/x-canon-crw", + "image/x-epson-erf", + "image/x-fuji-raf", + "image/x-kodak-dcr", + "image/x-kodak-kdc", + "image/x-minolta-mrw", + "image/x-nikon-nef", + "image/x-nikon-nrw", + "image/x-olympus-orf", + "image/x-panasonic-raw", + "image/x-panasonic-rw2", + "image/x-pentax-pef", + "image/x-sigma-x3f", + "image/x-sony-arw", + "image/x-sony-sr2", + "image/x-sony-srf", +} + // Tika is the configured Tika server: its URL and the types it is asked for. // It only decides which file goes there, the call lives in the preprocessor. -// The default list is a single "*", so every non-image file is sent and Tika -// decides; a list that names types restricts it, an empty one sends nothing. type Tika struct { URL string // our mime type -> Tika's, "" when equal; "*" means every type mimeTypes map[string]string } -// NewTika parses "mime[:tika-mime]" entries, "*" being every type. -func NewTika(url string, mimeTypes []string) Tika { +// NewTika parses "mime[:tika-mime]" entries. mimeTypes is the configured list, +// which the default config fills with DefaultTikaTypes, additional adds to it, +// and "*" in either sends every type that nothing else claims. An empty list +// sends nothing, which leaves a shared Tika server to the search service. +func NewTika(url string, mimeTypes, additional []string) Tika { t := Tika{URL: url, mimeTypes: map[string]string{}} - for _, entry := range mimeTypes { + for _, entry := range slices.Concat(mimeTypes, additional) { ours, theirs, _ := strings.Cut(strings.ToLower(strings.TrimSpace(entry)), ":") if ours = strings.TrimSpace(ours); ours != "" { t.mimeTypes[ours] = strings.TrimSpace(theirs) diff --git a/services/webdav/pkg/thumbnail/tika_test.go b/services/webdav/pkg/thumbnail/tika_test.go index 7cdad6b4fa..6feef7cbd5 100644 --- a/services/webdav/pkg/thumbnail/tika_test.go +++ b/services/webdav/pkg/thumbnail/tika_test.go @@ -8,10 +8,13 @@ import ( ) var _ = Describe("Tika", func() { - none := thumbnail.NewTika("", nil) - all := thumbnail.NewTika("http://tika:9998", []string{"*"}) - nothing := thumbnail.NewTika("http://tika:9998", nil) - some := thumbnail.NewTika("http://tika:9998", []string{" Application/PDF ", "audio/mpeg", "image/x-nikon-nef:image/x-raw-nikon"}) + none := thumbnail.NewTika("", thumbnail.DefaultTikaTypes, nil) + all := thumbnail.NewTika("http://tika:9998", []string{"*"}, nil) + nothing := thumbnail.NewTika("http://tika:9998", nil, nil) + some := thumbnail.NewTika("http://tika:9998", []string{" Application/PDF ", "audio/mpeg", "image/x-nikon-nef:image/x-raw-nikon"}, nil) + built := thumbnail.NewTika("http://tika:9998", thumbnail.DefaultTikaTypes, nil) + extended := thumbnail.NewTika("http://tika:9998", thumbnail.DefaultTikaTypes, []string{"application/zip", "image/heic"}) + extendedAll := thumbnail.NewTika("http://tika:9998", thumbnail.DefaultTikaTypes, []string{"*"}) DescribeTable("Supports", func(tika thumbnail.Tika, mimeType string, want bool) { @@ -26,6 +29,21 @@ var _ = Describe("Tika", func() { Entry("a listed type with parameters", some, "audio/mpeg; charset=binary", true), Entry("a mapped type", some, "image/x-nikon-nef", true), Entry("an unlisted type", some, "application/zip", false), + Entry("a document from the built-in list", built, "application/pdf", true), + Entry("a camera raw from the built-in list, which is why it is named there", built, "image/x-nikon-nef", true), + Entry("but not a type the built-in list leaves alone", built, "application/zip", false), + Entry("a type the additional list adds", extended, "application/zip", true), + Entry("while the built-in list is still in effect", extended, "application/pdf", true), + Entry("and a catch-all in the additional list takes the rest", extendedAll, "application/x-anything", true), + ) + + DescribeTable("ConfiguredFor", + func(tika thumbnail.Tika, mimeType string, want bool) { + Expect(tika.ConfiguredFor(mimeType)).To(Equal(want)) + }, + Entry("a named type", built, "image/x-nikon-nef", true), + Entry("not a type the catch-all takes", all, "application/pdf", false), + Entry("not an unnamed type", built, "application/zip", false), ) DescribeTable("ContentType", diff --git a/services/webdav/pkg/thumbnail/workflow/workflow.go b/services/webdav/pkg/thumbnail/workflow/workflow.go index 5a9c7ba5fc..d76a3ec656 100644 --- a/services/webdav/pkg/thumbnail/workflow/workflow.go +++ b/services/webdav/pkg/thumbnail/workflow/workflow.go @@ -554,12 +554,19 @@ func (w *ThumbnailWorkflow) sourceImage(ctx context.Context, ref *providerv1beta // 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{ + converter, err := preprocessor.ForType(mimeType, preprocessor.Options{ FontMapFile: w.fontMapFile, MaxImageSize: w.maxInputSize, Tika: w.tika, + SourceTypes: w.sourceTypes, Filename: filename, }) + if err != nil { + // nothing can make an image of this type, so there is nothing to fetch + logger.Debug().Str("mimetype", mimeType).Msg("no preview for this type") + + return nil, "", nil, ErrNoThumbnail + } // 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