This repository has been archived by the owner on Apr 21, 2020. It is now read-only.
/
wikifile.go
183 lines (152 loc) · 3.52 KB
/
wikifile.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"html/template"
"io/ioutil"
"os"
"path/filepath"
)
const (
NEW_MD = "_New.md"
WIKIFILE_FOLDER = 1 + iota
WIKIFILE_MARKDOWN
WIKIFILE_OTHER
)
type WikiFile struct {
os.FileInfo
WikiPath string
fileType int
}
func loadFile(wpath string) (WikiFile, error) {
if wpath == "" {
wpath = string(filepath.Separator)
}
wpath = filepath.Clean(wpath)
// TODO security check
fpath := filepath.Join(setting.wikiRoot, wpath)
fi, err := os.Stat(fpath)
if err != nil {
return WikiFile{}, err
}
ft := 0
if fi.IsDir() {
ft = WIKIFILE_FOLDER
} else if isMarkdown(wpath) {
ft = WIKIFILE_MARKDOWN
} else {
ft = WIKIFILE_OTHER
}
log.Debug("wpath: %s, ft: %d", wpath, ft)
return WikiFile{
FileInfo: fi,
WikiPath: wpath,
fileType: ft,
}, nil
}
func createFile(wpath string) (WikiFile, error) {
// TODO security check
fpath := filepath.Join(setting.wikiRoot, wpath)
// check if file is already exists
_, err := os.Stat(fpath)
if err == nil { // already exists
return WikiFile{}, os.ErrExist
}
b := []byte("\n")
// if the new page template is exists
tmplPath := filepath.Join(setting.wikiRoot, NEW_MD)
if src, err := os.Open(tmplPath); err == nil {
log.Debug("new page template found.", tmplPath)
b, err = ioutil.ReadAll(src)
if err != nil {
log.Debug(err.Error())
}
}
os.MkdirAll(filepath.Dir(fpath), 0755)
err = ioutil.WriteFile(fpath, b, 0644)
if err != nil {
log.Debug(err.Error())
return WikiFile{}, err
}
return loadFile(wpath)
}
func saveFile(wpath string, b []byte) (WikiFile, error) {
// TODO security check
fpath := filepath.Join(setting.wikiRoot, wpath)
// check if file is already exists
_, err := os.Stat(fpath)
if err == nil { // already exists
return WikiFile{}, os.ErrExist
}
os.MkdirAll(filepath.Dir(fpath), 0755)
err = ioutil.WriteFile(fpath, b, 0644)
if err != nil {
log.Debug(err.Error())
return WikiFile{}, err
}
return loadFile(wpath)
}
func (wf WikiFile) Name() string {
if wf.WikiPath == string(filepath.Separator) {
return ""
}
return wf.FileInfo.Name()
}
// URLPREFIX/a/b/c.md
func (wf WikiFile) URL() template.URL {
wpath := filepath.Join(setting.urlPrefix, wf.WikiPath)
s := queryEscapeWikiPath(wpath)
return template.URL(s)
}
func (wf WikiFile) path() string {
return filepath.Join(setting.wikiRoot, wf.WikiPath)
}
func (wf WikiFile) files() []WikiFile {
if !wf.IsDir() {
return nil
}
fis, _ := ioutil.ReadDir(wf.path())
ret := make([]WikiFile, len(fis))
for i, fi := range fis {
wf_ := WikiFile{
FileInfo: fi,
WikiPath: filepath.Join(wf.WikiPath, fi.Name()),
}
switch {
case fi.IsDir():
wf_.fileType = WIKIFILE_FOLDER
ret[i] = wf_
case isMarkdown(fi.Name()):
wf_.fileType = WIKIFILE_MARKDOWN
ret[i] = wf_
default:
wf_.fileType = WIKIFILE_OTHER
ret[i] = wf_
}
}
return ret
}
func (wf WikiFile) parent() (WikiFile, bool) {
if wf.WikiPath == string(filepath.Separator) {
return WikiFile{}, false
}
dir := filepath.Dir(wf.WikiPath)
fi, _ := os.Stat(filepath.Join(setting.wikiRoot, dir))
return WikiFile{
FileInfo: fi,
WikiPath: dir,
fileType: WIKIFILE_FOLDER,
}, true
}
func (wf WikiFile) read() ([]byte, error) {
return ioutil.ReadFile(wf.path())
}
func (wf WikiFile) save(b []byte) error {
return ioutil.WriteFile(wf.path(), b, 0644)
}
func (wf WikiFile) remove() error {
return os.RemoveAll(wf.path())
}
func (wf WikiFile) rename(newpath string) error {
path := filepath.Join(setting.wikiRoot, newpath)
os.MkdirAll(filepath.Dir(path), 0755)
return os.Rename(wf.path(), path)
}