fix: enable preview for legacy .doc documents in Go server (#17767)

This commit is contained in:
euvre
2026-08-12 00:43:10 -07:00
committed by GitHub
parent efa1c6c48a
commit 247c56fb4d

View File

@@ -9,6 +9,7 @@ import (
"ragflow/internal/common"
"ragflow/internal/dao"
"ragflow/internal/entity"
"ragflow/internal/parser/parser"
"ragflow/internal/storage"
"ragflow/internal/utility"
)
@@ -225,9 +226,28 @@ func (s *DocumentService) GetDocumentPreview(ctx context.Context, docID string)
ext := utility.GetFileExtension(fileName)
contentType := utility.GetContentType(ext, doc.Type)
// Legacy .doc (OLE2) documents cannot be rendered by the
// .docx-only web previewer; serve extracted plain text instead.
if ext == "doc" {
if text, cerr := extractDOCPreviewText(ctx, fileName, data); cerr == nil {
data = []byte(text)
contentType = "text/plain; charset=utf-8"
}
}
return &DocumentPreview{
Data: data,
ContentType: contentType,
FileName: fileName,
}, nil
}
// extractDOCPreviewText extracts plain text from a legacy .doc
// document via the office_oxide-backed DOCParser.
func extractDOCPreviewText(ctx context.Context, filename string, data []byte) (string, error) {
res := parser.NewDOCParser().ParseWithResult(ctx, filename, data)
if res.Err != nil {
return "", res.Err
}
return res.Text, nil
}