Files
tajniak81andClaude Opus 5.5 1696f2e77e
CI / go (Server) (push) Failing after 22m44s
CI / go (Media) (push) Canceled after 0s
CI / go (Web-App) (push) Canceled after 0s
CI / frontend (push) Failing after 10m17s
CI / desktop (ubuntu-latest) (push) Failing after 1h44m2s
CI / desktop (macos-latest) (push) Canceled after 0s
CI / desktop (windows-latest) (push) Canceled after 0s
CI / mobile (push) Failing after 5m11s
CI / docker (push) Successful in 1h29m50s
Docs / build (push) Successful in 14m34s
Docs / deploy (push) Skipped
Search the text of files attached to pages
The search index now reads the files pages link to: PDF text layers,
Word (.docx), HTML and text files such as Markdown or CSV, up to 25 MB,
and their names. Each file is read once, a few seconds after upload,
and its text is kept in the index; each linked file is indexed as its
own document under its page, like database records, so a file taken
off its page, or a trashed page, drops out. Scanned PDFs are not read.

Hits carry a new file field (id and name). The web search palette and
the phone's search show the file with an attachment icon and the page it
is on; /docwell in Slack names the file too.

PDF, Word and HTML reading moved from the AI package into
internal/textract, which AI's extra files now use as well. The index
mapping version is bumped, so existing indexes rebuild on start.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 20:50:27 +01:00

107 lines
2.8 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
// Package textract reads the text of documents people upload: PDFs (their
// text layer), Word files, HTML and plain text formats. Search indexes
// attachments with it, and AI reads its extra files with it.
package textract
import (
"bytes"
"errors"
"path/filepath"
"strings"
"unicode/utf8"
"github.com/ledongthuc/pdf"
"docwell/server/internal/convert"
)
var (
// ErrUnsupported is a file type Text does not read.
ErrUnsupported = errors.New("unsupported file type")
// ErrUnreadable is a file of a supported type that could not be read.
ErrUnreadable = errors.New("the file could not be read")
// ErrNoText is a PDF without a text layer (scanned pages; no OCR here).
ErrNoText = errors.New("the PDF has no text layer")
// ErrNotUTF8 is a plain text file in another encoding.
ErrNotUTF8 = errors.New("the file is not UTF-8 text")
)
var plainTypes = map[string]bool{
".txt": true, ".md": true, ".markdown": true, ".csv": true, ".tsv": true, ".json": true, ".yaml": true,
".yml": true, ".xml": true, ".rst": true, ".org": true, ".log": true,
}
// Supported reports whether Text reads files named like name.
func Supported(name string) bool {
switch ext := strings.ToLower(filepath.Ext(name)); ext {
case ".html", ".htm", ".pdf", ".docx":
return true
default:
return plainTypes[ext]
}
}
// Text reads the text of a file, by its name's extension. HTML and Word
// come out as Markdown.
func Text(name string, data []byte) (string, error) {
ext := strings.ToLower(filepath.Ext(name))
switch {
case plainTypes[ext]:
data = bytes.TrimPrefix(data, []byte{0xEF, 0xBB, 0xBF})
if !utf8.Valid(data) {
return "", ErrNotUTF8
}
return string(data), nil
case ext == ".html" || ext == ".htm":
doc, err := convert.FromHTML(string(data))
if err != nil {
return "", ErrUnreadable
}
return convert.Markdown(doc, convert.Options{}), nil
case ext == ".pdf":
return PDF(data)
case ext == ".docx":
doc, _, err := convert.FromDOCX(data)
if err != nil {
return "", ErrUnreadable
}
return convert.Markdown(doc, convert.Options{}), nil
}
return "", ErrUnsupported
}
// PDF reads a PDF's text page by page. The reader panics on some malformed
// files, so that becomes ErrUnreadable.
func PDF(data []byte) (text string, err error) {
defer func() {
if recover() != nil {
text, err = "", ErrUnreadable
}
}()
r, err := pdf.NewReader(bytes.NewReader(data), int64(len(data)))
if err != nil {
return "", ErrUnreadable
}
var b strings.Builder
for i := 1; i <= r.NumPage(); i++ {
p := r.Page(i)
if p.V.IsNull() {
continue
}
t, err := p.GetPlainText(nil)
if err != nil {
continue
}
if t = strings.TrimSpace(t); t != "" {
b.WriteString(t)
b.WriteString("\n\n")
}
}
if strings.TrimSpace(b.String()) == "" {
return "", ErrNoText
}
return b.String(), nil
}