-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtxt.go
More file actions
74 lines (58 loc) · 1.78 KB
/
txt.go
File metadata and controls
74 lines (58 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package docreader
import (
"fmt"
"os"
"strings"
)
// TxtReader 用于读取 .txt 文件
type TxtReader struct{}
// ReadText 读取 TXT 文件的文本内容
func (r *TxtReader) ReadText(filePath string) (string, error) {
// 读取文件内容
data, err := os.ReadFile(filePath)
if err != nil {
return "", WrapError("TxtReader.ReadText", filePath, ErrFileRead)
}
return string(data), nil
}
// GetMetadata 获取 TXT 文件的元数据
func (r *TxtReader) GetMetadata(filePath string) (map[string]string, error) {
metadata := make(map[string]string)
// 获取文件信息
fileInfo, err := os.Stat(filePath)
if err != nil {
return nil, WrapError("TxtReader.GetMetadata", filePath, ErrFileNotFound)
}
metadata["size"] = fmt.Sprintf("%d", fileInfo.Size())
metadata["modified"] = fileInfo.ModTime().String()
return metadata, nil
}
// ReadWithConfig 根据配置读取 TXT 文件,返回结构化结果
func (r *TxtReader) ReadWithConfig(filePath string, config *ReadConfig) (*DocumentResult, error) {
data, err := os.ReadFile(filePath)
if err != nil {
return nil, WrapError("TxtReader.ReadWithConfig", filePath, ErrFileRead)
}
content := string(data)
lines := strings.Split(content, "\n")
result := &DocumentResult{
FilePath: filePath,
TotalPages: 1,
Pages: make([]PageContent, 0),
Metadata: make(map[string]string),
}
// 获取元数据
metadata, _ := r.GetMetadata(filePath)
result.Metadata = metadata
// 根据配置筛选行
filteredLines := filterLinesForSinglePage(lines, config)
pageContent := PageContent{
PageNumber: 0,
Lines: filteredLines,
TotalLines: len(filteredLines),
}
result.Pages = append(result.Pages, pageContent)
result.TotalLines = len(filteredLines)
result.Content = strings.Join(filteredLines, "\n")
return result, nil
}