-
Notifications
You must be signed in to change notification settings - Fork 0
/
txt.go
124 lines (111 loc) · 2.67 KB
/
txt.go
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package txt
import (
"bufio"
"fmt"
"log"
"os"
"path/filepath"
"sync"
"github.com/fsnotify/fsnotify"
)
// TxtStore 存储每个.txt文件的内容
type TxtStore struct {
files map[string][]string
mu sync.RWMutex
}
var (
instance *TxtStore
once sync.Once
)
// GetInstance 以单例模式返回TxtStore实例
func GetInstance() *TxtStore {
once.Do(func() {
instance = &TxtStore{
files: make(map[string][]string),
}
instance.loadTxtFiles(".")
go instance.watchTxtFiles(".")
})
return instance
}
// loadTxtFiles 加载目录下所有.txt文件
func (ts *TxtStore) loadTxtFiles(dirPath string) {
filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Println("Error accessing path:", path, "Error:", err)
return err
}
if filepath.Ext(info.Name()) == ".txt" {
fileContent, err := ts.readFileContent(path)
if err != nil {
log.Println("Error reading file:", path, "Error:", err)
return err
}
ts.mu.Lock()
ts.files[info.Name()] = fileContent
ts.mu.Unlock()
}
return nil
})
}
// readFileContent 读取文件内容到字符串数组
func (ts *TxtStore) readFileContent(filePath string) ([]string, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
// watchTxtFiles 监听.txt文件的变更
func (ts *TxtStore) watchTxtFiles(dirPath string) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
done := make(chan bool)
go func() {
for {
select {
case event := <-watcher.Events:
if event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create {
log.Println("Detected file change:", event.Name)
if filepath.Ext(event.Name) == ".txt" {
fileContent, err := ts.readFileContent(event.Name)
if err != nil {
log.Println("Error reading file:", event.Name, "Error:", err)
continue
}
ts.mu.Lock()
ts.files[filepath.Base(event.Name)] = fileContent
ts.mu.Unlock()
}
}
case err := <-watcher.Errors:
log.Println("Watcher error:", err)
}
}
}()
err = watcher.Add(dirPath)
if err != nil {
log.Fatal(err)
}
<-done
}
// GetFileContent 根据文件名返回文件内容
func (ts *TxtStore) GetFileContent(fileName string) ([]string, error) {
ts.mu.RLock()
defer ts.mu.RUnlock()
content, ok := ts.files[fileName+".txt"]
if !ok {
// 文件不存在时的处理方式
return nil, fmt.Errorf("file not found: %s", fileName+".txt")
}
return content, nil
}