-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.go
More file actions
158 lines (151 loc) · 4.09 KB
/
admin.go
File metadata and controls
158 lines (151 loc) · 4.09 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
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
package main
import (
"errors"
"fmt"
"io"
"log"
"net/url"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"github.com/gin-gonic/gin"
)
const (
filePerm = 0660 // `-rw-rw----`
dirPerm = 0770 // `drwxrwx---`
)
var (
// only lowercase-`.md` supported by silent
regexExtMdStrict = regexp.MustCompile(`\.md$`) // case-sensitive
regexEnsureExtMdStrict = regexp.MustCompile(`(\.md)?$`) // case-sensitive
regexEnsureTrailingSlash = regexp.MustCompile(`/?$`)
_deliveryUrl = os.Getenv("DELIVERY_URL")
deliveryUrl = ensureTrailingSlash(_deliveryUrl)
)
func adminApis(r *gin.Engine) {
a := r.Group("/")
a.Use(checkAuth)
{
a.GET("/api/list", func(c *gin.Context) {
dirKey := c.Query("dir")
// mind security
dirAbs, ok := checkIllegalDirToList(c, dirKey)
if !ok {
c.JSON(400, errorRes{"Bad request"})
return
}
list, err := os.ReadDir(dirAbs)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
// ignore and continue to list
// there should be some use-cases
} else {
c.JSON(500, errorRes{"Failed to read dir"})
log.Printf("Failed to read dir %q. err=%v\n", dirAbs, err)
return
}
}
res := &listRes{}
res.List = []listFile{}
for _, v := range list {
res.List = append(res.List, listFile{v.Name(), v.IsDir()})
}
sort.Slice(res.List, func(i, j int) bool {
if res.List[i].IsDir && !res.List[j].IsDir {
return true
}
return false
})
c.JSON(200, res)
})
a.POST("/api/save", func(c *gin.Context) {
fileKey := c.GetHeader("x-wiki-file")
fileKey, _ = url.PathUnescape(fileKey)
if fileKey == "" || !isExtMdStrict(fileKey) {
c.JSON(400, errorRes{"Bad request"})
return
}
// mind security
fileAbs, ok := checkIllegalFileToSave(c, fileKey)
if !ok {
c.JSON(400, errorRes{"Bad request"})
return
}
bytes, err := io.ReadAll(c.Request.Body)
if err != nil {
c.JSON(400, errorRes{"Bad request"})
return
}
if err := os.WriteFile(fileAbs, bytes, filePerm); err != nil {
c.JSON(500, errorRes{"Failed to save file"})
log.Printf("Failed to write file %q. err=%v\n", fileAbs, err)
return
}
c.JSON(200, successRes{})
})
a.POST("/api/new", func(c *gin.Context) {
key := c.GetHeader("x-wiki-file")
key, _ = url.PathUnescape(key)
if key == "" {
c.JSON(400, errorRes{"Bad request"})
return
}
isDir := strings.HasSuffix(key, "/")
if !isDir {
// for simplicity, only allow `*.md` as New-File for now
key = ensureExtMdStrict(key)
}
// mind security
pathAbs, ok := checkIllegalPathToCreate(c, key)
if !ok {
c.JSON(400, errorRes{"Bad request"})
return
}
// 检查文件是否存在
if _, err := os.Stat(pathAbs); os.IsNotExist(err) {
// 文件不存在,则创建文件
if isDir {
if err := os.MkdirAll(pathAbs, dirPerm); err != nil {
c.JSON(500, errorRes{"Failed to create"})
log.Printf("Failed to mkdir %q. err=%v\n", pathAbs, err)
return
}
} else {
filename := filepath.Base(pathAbs)
base := strings.TrimSuffix(filename, filepath.Ext(filename))
content := fmt.Sprintf("# %s\n\n> ...", base)
if err := os.WriteFile(pathAbs, []byte(content), filePerm); err != nil {
c.JSON(500, errorRes{"Failed to create"})
log.Printf("Failed to write file %q. err=%v\n", pathAbs, err)
return
}
}
c.JSON(200, successRes{})
} else if err != nil {
// 其他错误(非“文件不存在”错误)
c.JSON(500, errorRes{"Failed to create"})
log.Printf("Failed to check if %q exists. err=%v\n", pathAbs, err)
} else {
// 文件已存在,不进行任何操作
c.JSON(400, errorRes{"Path already exists"})
}
})
}
}
func getConfigRes() configRes {
return configRes{deliveryUrl}
}
func ensureTrailingSlash(str string) string {
if str == "" {
return str
}
return regexEnsureTrailingSlash.ReplaceAllLiteralString(str, "/")
}
func ensureExtMdStrict(key string) string {
return regexEnsureExtMdStrict.ReplaceAllLiteralString(key, ".md")
}
func isExtMdStrict(key string) bool {
return regexExtMdStrict.MatchString(key)
}