forked from ajadex/udocs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filesystem_dao.go
143 lines (115 loc) · 3.01 KB
/
filesystem_dao.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package storage
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"sync"
"time"
)
var globalData *sync.RWMutex = new(sync.RWMutex)
type FileSystemDao struct {
root string
mode os.FileMode
*SearchDB
}
func NewFileSystemDao(root string, mode os.FileMode, searchDir string) *FileSystemDao {
return &FileSystemDao{
root: root,
mode: mode,
SearchDB: NewSearchDB(searchDir),
}
}
func (fs *FileSystemDao) Fetch(pageID string) ([]byte, error) {
globalData.RLock()
defer globalData.RUnlock()
if filepath.Ext(pageID) == "" {
pageID = filepath.Join(pageID, "index.html")
}
filename := filepath.Join(fs.root, pageID)
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("storage.Fetch: %v", err)
}
return data, nil
}
func (fs *FileSystemDao) FetchGlob(pattern string) []string {
globalData.RLock()
defer globalData.RUnlock()
ids := make([]string, 0)
files, _ := filepath.Glob(filepath.Join(fs.root, pattern))
for i, file := range files {
if fi, err := os.Stat(file); err != nil || fi.IsDir() {
continue
}
if filepath.Ext(file) != "" {
files[i] = file[len(fs.root):]
ids = append(ids, file[len(fs.root):])
}
}
return ids
}
func (fs *FileSystemDao) Insert(pageID string, pageData []byte) error {
globalData.Lock()
defer globalData.Unlock()
filename := filepath.Join(fs.root, pageID)
if err := os.MkdirAll(filepath.Dir(filename), fs.mode); err != nil {
return fmt.Errorf("storage.Insert: %v", err)
}
if err := ioutil.WriteFile(filename, pageData, fs.mode); err != nil {
return fmt.Errorf("storage.Insert: %v", err)
}
return nil
}
func (fs *FileSystemDao) Delete(pageID string) error {
globalData.Lock()
defer globalData.Unlock()
filename := filepath.Join(fs.root, pageID)
if err := os.Remove(filename); err != nil {
return fmt.Errorf("storage.Delete: %v", err)
}
if err := fs.SearchDB.Index.Delete(pageID); err != nil {
return fmt.Errorf("storage.Delete: %v", err)
}
return nil
}
func (fs *FileSystemDao) DeleteGlob(pattern string) error {
globalData.Lock()
defer globalData.Unlock()
files, err := filepath.Glob(filepath.Join(fs.root, pattern))
if err != nil {
return fmt.Errorf("storage.Delete: %v", err)
}
for _, f := range files {
if err := os.RemoveAll(f); err != nil {
log.Println(err.Error())
}
if err := fs.SearchDB.Index.Delete(f); err != nil {
log.Println(err.Error())
}
}
return nil
}
func (fs *FileSystemDao) Query(query string) (*QueryResult, error) {
globalData.RLock()
defer globalData.RUnlock()
return fs.SearchDB.Query(query)
}
func (fs *FileSystemDao) Index(pageID, pageTitle string, pageData []byte) error {
globalData.Lock()
defer globalData.Unlock()
indexData := struct {
Title string `json:"title"`
Body string `json:"body"`
Modified time.Time `json:"modified"`
}{
Title: pageTitle,
Body: string(filterHTMLTags(pageData)),
Modified: time.Now(),
}
return fs.SearchDB.Index.Index(pageID, indexData)
}
func (fs *FileSystemDao) Drop() error {
return fs.DeleteGlob("**")
}