Files
opencloud/services/webdav/pkg/preprocessor/tika.go
T
Dominik Schmidt 31b6cf6bd9 docs(webdav): say how a Tika before 4.2.0 activates the preset
Both answer with the same image for the same file, only the package differs, and a 4.1.x server has no preview for erf, orf and rw2. The 404 now names both ways to activate the preset.
2026-11-27 16:38:29 +01:00

134 lines
4.2 KiB
Go

package preprocessor
import (
"archive/zip"
"bytes"
"fmt"
"io"
"mime"
"net/http"
"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
// detection hints
filename string
contentType string
}
// 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.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.
func tikaThumbnail(tikaURL, 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))
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 "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)
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))
}