Files
opencloud/services/webdav/pkg/preprocessor/tika.go
T
Dominik Schmidt 17a9874be2 feat(webdav): make the Tika thumbnail preset configurable
OC_TIKA_THUMBNAIL_PRESET (WEBDAV_TIKA_THUMBNAIL_PRESET) names the catalog
preset webdav unpacks thumbnails with, "thumbnail" by default. A preset of
your own has to bring the same filter, only the raster images Tika marks as
THUMBNAIL or RENDERING, since webdav takes the first image in the answer.
An empty preset fails at startup.

Changing the preset needs the thumbnail cache emptied, as a Tika upgrade
does.
2026-12-02 16:22:19 +01:00

141 lines
4.5 KiB
Go

package preprocessor
import (
"archive/zip"
"bytes"
"fmt"
"io"
"mime"
"net/http"
"net/url"
"path/filepath"
"strings"
"time"
)
// maxTikaResponse bounds what is read back from Tika
const maxTikaResponse = 100 * 1024 * 1024
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
}
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) {
data, err := io.ReadAll(r)
if err != nil {
return nil, err
}
contentType, img, err := tikaThumbnail(t.tikaURL, t.preset, t.filename, t.contentType, data)
if err != nil {
return nil, err
}
return &Image{ContentType: contentType, Data: img}, nil
}
// tikaThumbnail returns the thumbnail Tika found in the document and its
// content type. The thumbnail catalog preset (TIKA-4856) unpacks exactly the
// raster image a client would show for the document, a stored thumbnail such as
// 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. 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/"+url.PathEscape(preset), bytes.NewReader(data))
if err != nil {
return "", nil, err
}
req.Header.Set("Content-Type", contentType)
req.Header.Set("Accept", "application/zip")
// the extension is what routes a raw image to its parser
if filename != "" {
req.Header.Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", filename))
}
resp, err := tikaHTTPClient.Do(req)
if err != nil {
return "", nil, fmt.Errorf("tika request failed: %w", err)
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusOK:
case http.StatusNoContent:
return "", nil, ErrNoThumbnail
case http.StatusNotFound:
// the route exists once the catalog preset is active
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)
}
body, err := io.ReadAll(io.LimitReader(resp.Body, maxTikaResponse))
if err != nil {
return "", nil, fmt.Errorf("tika unpack response: %w", err)
}
zr, err := zip.NewReader(bytes.NewReader(body), int64(len(body)))
if err != nil {
return "", nil, fmt.Errorf("tika unpack response: %w", err)
}
return thumbnailFromZip(zr)
}
// thumbnailFromZip takes the first image in the package Tika answers with.
// What else lies in there is the package's own bookkeeping, and it has been
// renamed before (TIKA-4971 made it datapackage.json and rmeta.json beside an
// unpacked/ directory), so the image is found by what it is, not by where it
// lies or what the others are called.
func thumbnailFromZip(zr *zip.Reader) (string, []byte, error) {
for _, f := range zr.File {
if f.FileInfo().IsDir() {
continue
}
img, err := readZipEntry(zr, f.Name)
if err != nil || len(img) == 0 {
continue
}
contentType := entryContentType(f.Name, img)
if !strings.HasPrefix(contentType, "image/") {
continue
}
return contentType, img, nil
}
return "", nil, ErrNoThumbnail
}
// entryContentType names the image's type: the extension decides and the bytes
// break the tie.
func entryContentType(name string, data []byte) string {
if byExt := mime.TypeByExtension(filepath.Ext(name)); byExt != "" {
return byExt
}
return http.DetectContentType(data)
}
func readZipEntry(zr *zip.Reader, name string) ([]byte, error) {
rc, err := zr.Open(name)
if err != nil {
return nil, err
}
defer rc.Close()
return io.ReadAll(io.LimitReader(rc, maxTikaResponse))
}