mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-08-13 04:13:35 +08:00
fix: enable preview for legacy .doc documents in Go server (#17767)
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user