Skip to content

Commit cefefae

Browse files
committed
feat: 新增返回支持的格式
1 parent 3510a96 commit cefefae

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

reader.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ package docreader
33
import (
44
"os"
55
"path/filepath"
6+
"slices"
67
"strings"
78
)
89

10+
// 支持的文档格式列表
11+
var supportedFormats = []string{".docx", ".pdf", ".xlsx", ".pptx", ".txt", ".csv", ".md", ".markdown", ".rtf"}
12+
913
// DocumentReader 定义了文档读取器的通用接口
1014
type DocumentReader interface {
1115
// ReadText 读取文档的文本内容
@@ -22,6 +26,22 @@ type Document struct {
2226
Metadata map[string]string
2327
}
2428

29+
// GetSupportedFormats 返回当前支持的文档格式列表
30+
func GetSupportedFormats() []string {
31+
formats := make([]string, len(supportedFormats))
32+
copy(formats, supportedFormats)
33+
return formats
34+
}
35+
36+
// IsFormatSupported 检查指定的文件格式是否被支持
37+
func IsFormatSupported(ext string) bool {
38+
ext = strings.ToLower(ext)
39+
if !strings.HasPrefix(ext, ".") {
40+
ext = "." + ext
41+
}
42+
return slices.Contains(supportedFormats, ext)
43+
}
44+
2545
// ReadDocument 根据文件扩展名自动选择合适的读取器
2646
func ReadDocument(filePath string) (*Document, error) {
2747
// 检查文件是否存在

0 commit comments

Comments
 (0)