diff --git a/services/webdav/README.md b/services/webdav/README.md index 9905cc7f85..1d3ca8e709 100644 --- a/services/webdav/README.md +++ b/services/webdav/README.md @@ -49,6 +49,8 @@ So `*` never sends an image to Tika. An image type that only Tika can read has t Set `OC_TIKA_URL` (or `WEBDAV_TIKA_URL`) to an Apache Tika server 4.2.0 or newer, started with `TIKA_PRESETS=thumbnail`. The webdav service calls `PUT /unpack/preset/thumbnail`, which only exists with that preset active. +The preset is configurable with `OC_TIKA_THUMBNAIL_PRESET` (or `WEBDAV_TIKA_THUMBNAIL_PRESET`), `thumbnail` by default. The service then calls `PUT /unpack/preset/`, so a preset of your own has to be active on the Tika server. It has to bring the same filter as the built-in one and unpack only the raster images Tika marks as `THUMBNAIL` or `RENDERING`, because the service takes the first image in the answer as the thumbnail. The preset must not be empty. After changing the preset, empty the thumbnail cache (see Expiry below). + The Tika list starts from a default list: the formats a Tika release serves a thumbnail for, which are documents, ebooks, office and iWork files, GeoGebra, audio cover art and camera raws. It is configured with these variables: - `OC_TIKA_ADDITIONAL_THUMBNAIL_MIME_TYPES` adds to the default list. This is the usual way to send more to Tika, including `*` for every type that no other rule claims. @@ -103,7 +105,7 @@ A failed extraction (Tika unreachable, preset not active, a broken file) is not ##### Expiry -Entries never expire. A changed file gets new entries, because the key contains the file's checksum. An extractor that learns a new format, however, does not revisit files it has already answered for. After such an upgrade (a new Apache Tika release, for example), empty the cache (`WEBDAV_THUMBNAILS_CACHE_DIR`, or the bucket) so those files are asked again. +Entries never expire. A changed file gets new entries, because the key contains the file's checksum. An extractor that learns a new format, however, does not revisit files it has already answered for. After such an upgrade (a new Apache Tika release, for example), or after changing `OC_TIKA_THUMBNAIL_PRESET`, empty the cache (`WEBDAV_THUMBNAILS_CACHE_DIR`, or the bucket) so those files are asked again. #### Thumbnail Query String Parameters diff --git a/services/webdav/pkg/config/config.go b/services/webdav/pkg/config/config.go index fefdba5516..79469c5118 100644 --- a/services/webdav/pkg/config/config.go +++ b/services/webdav/pkg/config/config.go @@ -28,7 +28,8 @@ 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%%"` + 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 (or the preset named in OC_TIKA_THUMBNAIL_PRESET). A 4.1.x server works as well, with the preset activated in its config file instead." introductionVersion:"%%NEXT%%"` + TikaThumbnailPreset string `yaml:"tika_thumbnail_preset" env:"OC_TIKA_THUMBNAIL_PRESET;WEBDAV_TIKA_THUMBNAIL_PRESET" desc:"The Tika catalog preset that unpacks the thumbnail, called as PUT /unpack/preset/. It has to be active on the Tika server. A preset of your own has to bring the same filter as the built-in one, unpacking only the raster images Tika marks as THUMBNAIL or RENDERING, because the first image in the answer is taken as the thumbnail. Must not be empty." 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. This replaces the built-in list of the formats a Tika release serves one for. Use it to remove types from that list, or set it empty to send nothing, which leaves a Tika server shared with the search service to the search service alone. To send more types, use OC_TIKA_ADDITIONAL_THUMBNAIL_MIME_TYPES instead. Whatever this setting holds is the whole list, so a '*' alone drops the camera raws: reva types them as images, and only a named entry sends an image to Tika. A named type goes to Tika before anything else. An entry can map a type to the name Tika uses for the format, e.g. image/x-nikon-nef:image/x-raw-nikon. That name decides which parser reads the file, so a raw that Tika only detects as a plain tiff loses its embedded preview. The formats in the built-in list need no mapping." 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. They are added to the built-in list, or to whatever OC_TIKA_THUMBNAIL_MIME_TYPES names instead. This is the usual way to send more types to Tika. A single '*' sends every type that is neither an image nor has a built-in converter, so it never sends an image. An image type that only Tika can read has to be named. An entry can carry a type mapping the same way as in OC_TIKA_THUMBNAIL_MIME_TYPES." introductionVersion:"%%NEXT%%"` diff --git a/services/webdav/pkg/config/defaults/defaultconfig.go b/services/webdav/pkg/config/defaults/defaultconfig.go index 6706199fb8..7105473cb1 100644 --- a/services/webdav/pkg/config/defaults/defaultconfig.go +++ b/services/webdav/pkg/config/defaults/defaultconfig.go @@ -45,6 +45,7 @@ func DefaultConfig() *config.Config { WebdavNamespace: "/users/{{.Id.OpaqueId}}", RevaGateway: shared.DefaultRevaConfig().Address, + TikaThumbnailPreset: "thumbnail", TikaThumbnailMimeTypes: thumbnail.DefaultTikaTypes, Thumbnails: config.Thumbnails{ diff --git a/services/webdav/pkg/config/parser/parse.go b/services/webdav/pkg/config/parser/parse.go index e2650458bc..4d7d4a94dd 100644 --- a/services/webdav/pkg/config/parser/parse.go +++ b/services/webdav/pkg/config/parser/parse.go @@ -2,6 +2,7 @@ package parser import ( "errors" + "strings" occfg "github.com/opencloud-eu/opencloud/pkg/config" "github.com/opencloud-eu/opencloud/services/webdav/pkg/config" @@ -33,5 +34,8 @@ func ParseConfig(cfg *config.Config) error { } func Validate(cfg *config.Config) error { + if strings.TrimSpace(cfg.TikaThumbnailPreset) == "" { + return errors.New("OC_TIKA_THUMBNAIL_PRESET must not be empty") + } return nil } diff --git a/services/webdav/pkg/config/parser/parse_test.go b/services/webdav/pkg/config/parser/parse_test.go index 475c3222e9..882e8d8ae6 100644 --- a/services/webdav/pkg/config/parser/parse_test.go +++ b/services/webdav/pkg/config/parser/parse_test.go @@ -45,4 +45,21 @@ var _ = Describe("ParseConfig", func() { "OC_THUMBNAILS_MAX_INPUT_IMAGE_FILE_SIZE": "100MB", }, "20MB"), ) + + It("refuses an empty Tika thumbnail preset", func() { + GinkgoT().Setenv("OC_TIKA_THUMBNAIL_PRESET", "") + + Expect(parser.ParseConfig(defaults.DefaultConfig())).To(MatchError(ContainSubstring("OC_TIKA_THUMBNAIL_PRESET"))) + }) + + It("takes the thumbnail preset by default and a configured one instead", func() { + cfg := defaults.DefaultConfig() + Expect(parser.ParseConfig(cfg)).To(Succeed()) + Expect(cfg.TikaThumbnailPreset).To(Equal("thumbnail")) + + GinkgoT().Setenv("WEBDAV_TIKA_THUMBNAIL_PRESET", "previews") + cfg = defaults.DefaultConfig() + Expect(parser.ParseConfig(cfg)).To(Succeed()) + Expect(cfg.TikaThumbnailPreset).To(Equal("previews")) + }) }) diff --git a/services/webdav/pkg/preprocessor/preprocessor.go b/services/webdav/pkg/preprocessor/preprocessor.go index 89d1db0852..6a553b1e47 100644 --- a/services/webdav/pkg/preprocessor/preprocessor.go +++ b/services/webdav/pkg/preprocessor/preprocessor.go @@ -372,7 +372,7 @@ type Options struct { } func toTika(opts Options, mimeType string) FileConverter { - return TikaThumbnail{tikaURL: opts.Tika.URL, filename: opts.Filename, contentType: opts.Tika.ContentType(mimeType)} + return TikaThumbnail{tikaURL: opts.Tika.URL, preset: opts.Tika.Preset, filename: opts.Filename, contentType: opts.Tika.ContentType(mimeType)} } // ForType says what has to happen to a file of this type before the generator diff --git a/services/webdav/pkg/preprocessor/tika.go b/services/webdav/pkg/preprocessor/tika.go index 1ef71b79fb..6108cef58e 100644 --- a/services/webdav/pkg/preprocessor/tika.go +++ b/services/webdav/pkg/preprocessor/tika.go @@ -7,6 +7,7 @@ import ( "io" "mime" "net/http" + "net/url" "path/filepath" "strings" "time" @@ -20,6 +21,7 @@ var tikaHTTPClient = &http.Client{Timeout: 60 * time.Second} // TikaThumbnail asks a Tika server for the document's thumbnail. type TikaThumbnail struct { tikaURL string + preset string // detection hints filename string contentType string @@ -35,7 +37,7 @@ func (t TikaThumbnail) Convert(r io.Reader) (*Image, error) { return nil, err } - contentType, img, err := tikaThumbnail(t.tikaURL, t.filename, t.contentType, data) + contentType, img, err := tikaThumbnail(t.tikaURL, t.preset, t.filename, t.contentType, data) if err != nil { return nil, err } @@ -49,12 +51,15 @@ func (t TikaThumbnail) Convert(r io.Reader) (*Image, error) { // a raw photo's embedded preview or an audio file's cover, the rendering of an // office document's metafile thumbnail, or the rendered first page of a PDF. // The server has to run with the preset active: Tika 4.2.0 or newer takes -// TIKA_PRESETS=thumbnail, a 4.1.x server activates it in its config file. -func tikaThumbnail(tikaURL, filename, contentType string, data []byte) (string, []byte, error) { +// TIKA_PRESETS=thumbnail, a 4.1.x server activates it in its config file. A +// preset of another name has to bring the same filter, only the raster images +// Tika marks as THUMBNAIL or RENDERING: the first image in its answer is taken +// as the thumbnail. +func tikaThumbnail(tikaURL, preset, filename, contentType string, data []byte) (string, []byte, error) { if contentType == "" { contentType = "application/octet-stream" } - req, err := http.NewRequest(http.MethodPut, strings.TrimRight(tikaURL, "/")+"/unpack/preset/thumbnail", bytes.NewReader(data)) + req, err := http.NewRequest(http.MethodPut, strings.TrimRight(tikaURL, "/")+"/unpack/preset/"+url.PathEscape(preset), bytes.NewReader(data)) if err != nil { return "", nil, err } @@ -77,7 +82,7 @@ func tikaThumbnail(tikaURL, filename, contentType string, data []byte) (string, return "", nil, ErrNoThumbnail case http.StatusNotFound: // the route exists once the catalog preset is active - return "", nil, fmt.Errorf(`tika unpack returned %s: the "thumbnail" preset is not active, start Tika 4.2.0 or newer with TIKA_PRESETS=thumbnail, or a 4.1.x server with "presets": {"thumbnail": true} in its config`, resp.Status) + return "", nil, fmt.Errorf(`tika unpack returned %s: the %q preset is not active, start Tika 4.2.0 or newer with TIKA_PRESETS=%s, or a 4.1.x server with "presets": {%q: true} in its config`, resp.Status, preset, preset, preset) default: return "", nil, fmt.Errorf("tika unpack returned %s", resp.Status) } diff --git a/services/webdav/pkg/preprocessor/tika_test.go b/services/webdav/pkg/preprocessor/tika_test.go index 3636ff4595..3b6820ec53 100644 --- a/services/webdav/pkg/preprocessor/tika_test.go +++ b/services/webdav/pkg/preprocessor/tika_test.go @@ -72,7 +72,7 @@ var _ = Describe("TikaThumbnail", func() { _, _ = w.Write(presetZip(map[string][]byte{"1.png": pngBytes()})) } - img, err := TikaThumbnail{tikaURL: server.URL, filename: "shot.nef", contentType: "image/x-nikon-nef"}.Convert(bytes.NewReader([]byte("raw"))) + img, err := TikaThumbnail{tikaURL: server.URL, preset: "thumbnail", filename: "shot.nef", contentType: "image/x-nikon-nef"}.Convert(bytes.NewReader([]byte("raw"))) Expect(err).ToNot(HaveOccurred()) Expect(img).ToNot(BeNil()) Expect(requests).To(HaveLen(1)) @@ -89,7 +89,7 @@ var _ = Describe("TikaThumbnail", func() { })) } - img, err := TikaThumbnail{tikaURL: server.URL, filename: "document.pdf"}.Convert(bytes.NewReader([]byte("pdf"))) + img, err := TikaThumbnail{tikaURL: server.URL, preset: "thumbnail", filename: "document.pdf"}.Convert(bytes.NewReader([]byte("pdf"))) Expect(err).ToNot(HaveOccurred()) Expect(img).ToNot(BeNil()) Expect(img.ContentType).To(Equal("image/png")) @@ -102,7 +102,7 @@ var _ = Describe("TikaThumbnail", func() { _, _ = w.Write(presetZip(map[string][]byte{"thumbnail": pngBytes()})) } - img, err := TikaThumbnail{tikaURL: server.URL}.Convert(bytes.NewReader([]byte("raw"))) + img, err := TikaThumbnail{tikaURL: server.URL, preset: "thumbnail"}.Convert(bytes.NewReader([]byte("raw"))) Expect(err).ToNot(HaveOccurred()) Expect(img).ToNot(BeNil(), "the bytes name the type when the entry does not") }) @@ -110,7 +110,7 @@ var _ = Describe("TikaThumbnail", func() { It("reports a document without a thumbnail", func() { thumbnail = func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusNoContent) } - _, err := TikaThumbnail{tikaURL: server.URL}.Convert(bytes.NewReader([]byte("zip"))) + _, err := TikaThumbnail{tikaURL: server.URL, preset: "thumbnail"}.Convert(bytes.NewReader([]byte("zip"))) Expect(err).To(MatchError(ErrNoThumbnail)) }) @@ -120,29 +120,37 @@ var _ = Describe("TikaThumbnail", func() { _, _ = w.Write(presetZip(nil)) } - _, err := TikaThumbnail{tikaURL: server.URL}.Convert(bytes.NewReader([]byte("zip"))) + _, err := TikaThumbnail{tikaURL: server.URL, preset: "thumbnail"}.Convert(bytes.NewReader([]byte("zip"))) Expect(err).To(MatchError(ErrNoThumbnail)) }) It("reports a failing server", func() { thumbnail = func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusInternalServerError) } - _, err := TikaThumbnail{tikaURL: server.URL}.Convert(bytes.NewReader([]byte("raw"))) + _, err := TikaThumbnail{tikaURL: server.URL, preset: "thumbnail"}.Convert(bytes.NewReader([]byte("raw"))) Expect(err).To(MatchError(ContainSubstring("500"))) }) It("points at the preset activation when the route is missing", func() { - _, err := TikaThumbnail{tikaURL: server.URL + "/nowhere"}.Convert(bytes.NewReader([]byte("raw"))) + _, err := TikaThumbnail{tikaURL: server.URL + "/nowhere", preset: "thumbnail"}.Convert(bytes.NewReader([]byte("raw"))) Expect(err).To(MatchError(ContainSubstring("404"))) Expect(err).To(MatchError(ContainSubstring("preset")), "a 404 means the catalog preset is not active") }) + + It("calls the configured preset and names it when it is missing", func() { + _, err := TikaThumbnail{tikaURL: server.URL, preset: "previews"}.Convert(bytes.NewReader([]byte("raw"))) + Expect(requests).To(HaveLen(1)) + Expect(requests[0].URL.Path).To(Equal("/unpack/preset/previews")) + Expect(err).To(MatchError(ContainSubstring(`"previews" preset is not active`))) + Expect(err).To(MatchError(ContainSubstring("TIKA_PRESETS=previews"))) + }) }) var _ = Describe("ForType with a Tika server", func() { const tikaURL = "http://tika:9998" - named := thumbnail.NewTika(tikaURL, thumbnail.DefaultTikaTypes, nil) - everything := thumbnail.NewTika(tikaURL, []string{"*"}, nil) - off := thumbnail.NewTika("", thumbnail.DefaultTikaTypes, nil) + named := thumbnail.NewTika(tikaURL, "thumbnail", thumbnail.DefaultTikaTypes, nil) + everything := thumbnail.NewTika(tikaURL, "thumbnail", []string{"*"}, nil) + off := thumbnail.NewTika("", "thumbnail", thumbnail.DefaultTikaTypes, nil) tika := BeAssignableToTypeOf(TikaThumbnail{}) generator := BeNil() @@ -175,7 +183,7 @@ var _ = Describe("ForType with a Tika server", func() { } It("hands the mapped type to Tika", func() { - t := thumbnail.NewTika(tikaURL, []string{"application/pdf", "image/x-nikon-nef:image/x-raw-nikon"}, nil) + t := thumbnail.NewTika(tikaURL, "thumbnail", []string{"application/pdf", "image/x-nikon-nef:image/x-raw-nikon"}, nil) Expect(ForType("image/x-nikon-nef", Options{Tika: t}).(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 0dea3ba24c..30bb145db9 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, conf.AdditionalTikaThumbnailMimeTypes)), + workflow.WithTika(thumbnail.NewTika(conf.TikaURL, conf.TikaThumbnailPreset, conf.TikaThumbnailMimeTypes, conf.AdditionalTikaThumbnailMimeTypes)), workflow.WithFormats(formats), workflow.WithTypeMapping(typeMapping), workflow.WithWebdavNamespace(conf.WebdavNamespace), diff --git a/services/webdav/pkg/thumbnail/tika.go b/services/webdav/pkg/thumbnail/tika.go index d0e7ca22ff..37d1ee8353 100644 --- a/services/webdav/pkg/thumbnail/tika.go +++ b/services/webdav/pkg/thumbnail/tika.go @@ -65,16 +65,19 @@ var DefaultTikaTypes = []string{ // It only decides which file goes there, the call lives in the preprocessor. type Tika struct { URL string + // the catalog preset that unpacks the thumbnail, served at /unpack/preset/ + Preset string // our mime type -> Tika's, "" when equal; "*" means every type mimeTypes map[string]string } -// NewTika parses "mime[:tika-mime]" entries. mimeTypes is the configured list, +// NewTika parses "mime[:tika-mime]" entries, preset names the catalog preset Tika +// unpacks the thumbnail with. 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{}} +func NewTika(url, preset string, mimeTypes, additional []string) Tika { + t := Tika{URL: url, Preset: preset, mimeTypes: map[string]string{}} for _, entry := range slices.Concat(mimeTypes, additional) { ours, theirs, _ := strings.Cut(strings.ToLower(strings.TrimSpace(entry)), ":") if ours = strings.TrimSpace(ours); ours != "" { diff --git a/services/webdav/pkg/thumbnail/tika_test.go b/services/webdav/pkg/thumbnail/tika_test.go index 6feef7cbd5..5de95dacc2 100644 --- a/services/webdav/pkg/thumbnail/tika_test.go +++ b/services/webdav/pkg/thumbnail/tika_test.go @@ -8,13 +8,13 @@ import ( ) var _ = Describe("Tika", func() { - 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{"*"}) + none := thumbnail.NewTika("", "thumbnail", thumbnail.DefaultTikaTypes, nil) + all := thumbnail.NewTika("http://tika:9998", "thumbnail", []string{"*"}, nil) + nothing := thumbnail.NewTika("http://tika:9998", "thumbnail", nil, nil) + some := thumbnail.NewTika("http://tika:9998", "thumbnail", []string{" Application/PDF ", "audio/mpeg", "image/x-nikon-nef:image/x-raw-nikon"}, nil) + built := thumbnail.NewTika("http://tika:9998", "thumbnail", thumbnail.DefaultTikaTypes, nil) + extended := thumbnail.NewTika("http://tika:9998", "thumbnail", thumbnail.DefaultTikaTypes, []string{"application/zip", "image/heic"}) + extendedAll := thumbnail.NewTika("http://tika:9998", "thumbnail", thumbnail.DefaultTikaTypes, []string{"*"}) DescribeTable("Supports", func(tika thumbnail.Tika, mimeType string, want bool) {