-
Notifications
You must be signed in to change notification settings - Fork 1
/
extras.go
311 lines (247 loc) · 8.04 KB
/
extras.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package main
import (
"errors"
"html"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/go-chi/chi"
)
type TagInfo struct {
ID int `json:"id"`
Name string `json:"name"`
Value string `json:"value"`
Color string `json:"color"`
}
type UserInfo struct {
ID int `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Avatar string `json:"avatar"`
}
type CommentInfo struct {
ID int `json:"id"`
Content string `json:"text"`
Modified time.Time `json:"date"`
UserId int `db:"user_id" json:"user_id"`
}
type CurrentUser struct {
ID int
Root int
}
type EditInfo struct {
ID int `json:"id"`
Modified time.Time `json:"date"`
User int `db:"user_id" json:"user"`
Content string `json:"content"`
Origin *time.Time `json:"origin"`
}
var User = CurrentUser{1, 1}
func dbID(id string) (res int) {
conn.Get(&res, "select id from entity where path =? and tree = ?", id, User.Root)
return res
}
func addExtrasRoutes(r chi.Router) {
r.Get("/tags/all", func(w http.ResponseWriter, r *http.Request) {
info := make([]TagInfo, 0)
conn.Select(&info, "select id, name, value, color from tag")
format.JSON(w, 200, info)
})
r.Get("/tags", func(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get("id")
did := dbID(id)
ids := make([]int, 0)
conn.Select(&ids, "select tag_id from entity_tag where entity_id = ? ", did)
format.JSON(w, 200, ids)
})
r.Put("/tags", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
id := r.Form.Get("id")
did := dbID(id)
tags := strings.Split(r.Form.Get("value"), ",")
conn.Exec("delete from entity_tag WHERE entity_id = ?", did)
for _, tag := range tags {
conn.Exec("insert into entity_tag(entity_id, tag_id) VALUES(?, ?)", did, tag)
}
format.JSON(w, 200, Response{ID: id})
})
r.Post("/tags", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
name := r.Form.Get("name")
color := r.Form.Get("color")
value := strings.ReplaceAll(name, " ", "")
res, err := conn.Exec("INSERT INTO tag (name, value, color) VALUES (?, ?, ?)", name, value, color)
if err != nil {
format.Text(w, 500, err.Error())
return
}
tid, _ := res.LastInsertId()
format.JSON(w, 200, TagInfo{ID: int(tid), Name: name, Value: value, Color: color})
})
r.Put("/tags/{id}", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
id := chi.URLParam(r, "id")
name := r.Form.Get("name")
color := r.Form.Get("color")
value := strings.ReplaceAll(name, " ", "")
_, err := conn.Exec("UPDATE tag SET name = ?, value = ?, color = ? WHERE id = ?", name, value, color, id)
if err != nil {
format.Text(w, 500, err.Error())
return
}
tid, _ := strconv.Atoi(id)
format.JSON(w, 200, TagInfo{ID: tid, Name: name, Value: value, Color: color})
})
r.Delete("/tags/{id}", func(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
conn.Exec("DELETE FROM tag WHERE id = ?", id)
conn.Exec("DELETE FROM entity_tag WHERE tag_id = ?", id)
format.JSON(w, 200, Response{ID: id})
})
r.Get("/users/all", func(w http.ResponseWriter, r *http.Request) {
info := make([]UserInfo, 0)
conn.Select(&info, "select id, name, email, avatar from user")
format.JSON(w, 200, info)
})
r.Get("/users/{id}/avatar/{name}", func(w http.ResponseWriter, r *http.Request) {
name := chi.URLParam(r, "name")
log.Println(filepath.Join(Config.UserFolder, name))
http.ServeFile(w, r, filepath.Join(Config.UserFolder, name))
})
r.Post("/favorite", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
id := r.Form.Get("id")
did := dbID(id)
conn.Exec("INSERT INTO favorite(entity_id, user_id) values(?, ?)", did, User.ID)
format.JSON(w, 200, Response{ID: id})
})
r.Delete("/favorite", func(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get("id")
did := dbID(id)
conn.Exec("DELETE FROM favorite WHERE entity_id = ? and user_id = ?", did, User.ID)
format.JSON(w, 200, Response{ID: id})
})
r.Post("/share", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
id := r.Form.Get("id")
uid := r.Form.Get("user")
did := dbID(id)
conn.Exec("INSERT INTO entity_user(entity_id, user_id) values(?, ?)", did, uid)
format.JSON(w, 200, Response{ID: id})
})
r.Delete("/share", func(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get("id")
uid := r.URL.Query().Get("user")
did := dbID(id)
conn.Exec("DELETE FROM entity_user WHERE entity_id = ? and user_id = ?", did, uid)
format.JSON(w, 200, Response{ID: id})
})
r.Get("/comments", func(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get("id")
did := dbID(id)
comments := make([]CommentInfo, 0)
conn.Select(&comments, "select id,content,user_id,modified from comment where entity_id = ?", did)
format.JSON(w, 200, comments)
})
r.Post("/comments", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
id := r.URL.Query().Get("id")
did := dbID(id)
content := r.Form.Get("value")
res, _ := conn.Exec("insert into comment(entity_id, user_id, content) values(?, ?, ?)", did, User.ID, content)
cid, _ := res.LastInsertId()
format.JSON(w, 200, Response{ID: strconv.FormatInt(cid, 10)})
})
r.Put("/comments/{id}", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
id := chi.URLParam(r, "id")
content := r.Form.Get("value")
var uid int
conn.Get(&uid, "select user_id from comment where id = ?", id)
if uid != User.ID {
format.Text(w, 500, "Access Denied")
return
}
conn.Exec("update comment SET content = ? WHERE id = ?", content, id)
format.JSON(w, 200, Response{ID: id})
})
r.Delete("/comments/{id}", func(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
var uid int
conn.Get(&uid, "select user_id from comment where id = ?", id)
if uid != User.ID {
format.Text(w, 500, "Access Denied")
return
}
conn.Exec("delete from comment WHERE id = ?", id)
format.JSON(w, 200, Response{ID: id})
})
r.Get("/versions", func(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get("id")
did := dbID(id)
versions := make([]EditInfo, 0)
err := conn.Select(&versions, "SELECT id,modified,user_id,origin FROM entity_edit WHERE entity_id = ? ORDER BY modified desc", did)
if err != nil {
panic(err)
}
format.JSON(w, 200, versions)
})
r.Get("/versions/{id}", func(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
_, diff := r.URL.Query()["diff"]
var content, previous string
conn.Get(&content, "SELECT content FROM entity_edit WHERE id = ?", id)
if diff {
conn.Get(&previous, "SELECT previous FROM entity_edit WHERE id = ?", id)
}
mode := r.URL.Query().Get("mode")
if mode == "text" {
w.Header().Add("Content-type", "text/plain")
text2 := getTextFromFile(filepath.Join(Config.DataFolder, content))
if previous != "" {
text1 := getTextFromFile(filepath.Join(Config.DataFolder, previous))
io.WriteString(w, diffHTML(text1, text2))
return
}
io.WriteString(w, html.EscapeString(text2))
} else if mode == "binary" {
data, err := os.Open(filepath.Join(Config.DataFolder, content))
if err != nil {
panic(errors.New("Can't open file for reading"))
}
disposition := "inline"
w.Header().Set("Content-Disposition", disposition+"; filename=\""+content+"\"")
http.ServeContent(w, r, "", time.Now(), data)
}
})
r.Post("/versions", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
id := r.Form.Get("id")
version := r.Form.Get("version")
var edit EditInfo
conn.Get(&edit, "SELECT content, modified FROM entity_edit WHERE id = ?", version)
file, err := os.Open(filepath.Join(Config.DataFolder, edit.Content))
if err != nil {
panic(errors.New("Can't open file for reading"))
}
err = drive.Write(id, file)
if err != nil {
panic(err)
}
info, _ := saveVersion(id, &edit.Modified)
format.JSON(w, 200, info)
})
}
func getTextFromFile(path string) string {
d, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
return string(d)
}