-
Notifications
You must be signed in to change notification settings - Fork 0
/
document.go
440 lines (389 loc) · 13.5 KB
/
document.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package controllers
import (
"fmt"
"regexp"
"strings"
"mm-wiki/app/models"
"mm-wiki/app/utils"
)
type DocumentController struct {
BaseController
}
// document index
func (this *DocumentController) Index() {
documentId := this.GetString("document_id", "")
if documentId == "" {
this.ViewError("页面参数错误!", "/space/index")
}
document, err := models.DocumentModel.GetDocumentByDocumentId(documentId)
if err != nil {
this.ErrorLog("查找空间文档 " + documentId + " 失败:" + err.Error())
this.ViewError("查找文档失败!")
}
if len(document) == 0 {
this.ViewError("文档不存在!")
}
spaceId := document["space_id"]
space, err := models.SpaceModel.GetSpaceBySpaceId(spaceId)
if err != nil {
this.ErrorLog("查找文档 " + documentId + " 所在空间失败:" + err.Error())
this.ViewError("查找文档失败!")
}
if len(space) == 0 {
this.ViewError("文档所在空间不存在!")
}
// check space visit_level
isVisit, _, _ := this.GetDocumentPrivilege(space)
if !isVisit {
this.ViewError("您没有权限访问该空间下的文档!")
}
// get default space document
spaceDocument, err := models.DocumentModel.GetSpaceDefaultDocument(spaceId)
if err != nil {
this.ErrorLog("查找文档 " + documentId + " 失败:" + err.Error())
this.ViewError("查找文档失败!")
}
if len(spaceDocument) == 0 {
this.ViewError(" 空间首页文档不存在!")
}
// get space all document
documents, err := models.DocumentModel.GetAllSpaceDocuments(spaceId)
if err != nil {
this.ErrorLog("查找文档 " + documentId + " 所在空间失败:" + err.Error())
this.ViewError("查找文档失败!")
}
// get space privilege
_, isEditor, isDelete := this.GetDocumentPrivilege(space)
this.Data["is_editor"] = isEditor
this.Data["is_delete"] = isDelete
this.Data["documents"] = documents
this.Data["default_document_id"] = documentId
this.Data["space"] = space
this.Data["space_document"] = spaceDocument
this.viewLayout("document/index", "document")
}
// add document
func (this *DocumentController) Add() {
spaceId := this.GetString("space_id", "0")
parentId := this.GetString("parent_id", "0")
if spaceId == "0" {
this.ViewError("没有选择空间!")
}
if parentId == "0" {
this.ViewError("没有选择上级!")
}
space, err := models.SpaceModel.GetSpaceBySpaceId(spaceId)
if err != nil {
this.ErrorLog("添加文档失败:" + err.Error())
this.ViewError("添加文档失败!")
}
if len(space) == 0 {
this.ViewError("空间不存在!")
}
// check space document privilege
_, isEditor, _ := this.GetDocumentPrivilege(space)
if !isEditor {
this.ViewError("您没有权限在该空间下创建文档!")
}
parentDocument, err := models.DocumentModel.GetDocumentByDocumentId(parentId)
if err != nil {
this.ErrorLog("添加文档 " + parentId + " 失败:" + err.Error())
this.ViewError("添加文档失败!")
}
if len(parentDocument) == 0 {
this.ViewError("父文档不存在!")
}
path := parentDocument["path"] + "," + parentId
// get parent documents by path
parentDocuments, err := models.DocumentModel.GetParentDocumentsByPath(path)
if err != nil {
this.ErrorLog("查找父文档失败:" + err.Error())
this.ViewError("查找父文档失败!")
}
if len(parentDocuments) == 0 {
this.ViewError("父文档不存在!")
}
this.Data["parent_documents"] = parentDocuments
this.Data["parent_id"] = parentId
this.Data["space_id"] = spaceId
this.viewLayout("document/form", "default")
}
// save document
func (this *DocumentController) Save() {
if !this.IsPost() {
this.ViewError("请求方式有误!", "/main/index")
}
spaceId := strings.TrimSpace(this.GetString("space_id", "0"))
parentId := strings.TrimSpace(this.GetString("parent_id", "0"))
docType, _ := this.GetInt("type", models.Document_Type_Page)
name := strings.TrimSpace(this.GetString("name", ""))
if spaceId == "0" {
this.jsonError("没有选择空间!")
}
if parentId == "0" {
this.jsonError("没有选择父文档!")
}
if name == "" {
this.jsonError("文档名称不能为空!")
}
match, err := regexp.MatchString(`[\\\\/:*?\"<>、|]`, name)
if err != nil {
this.jsonError("文档名称格式不正确!")
}
if match {
this.jsonError("文档名称格式不正确!")
}
if name == utils.Document_Default_FileName {
this.jsonError("文档名称不能为 " + utils.Document_Default_FileName + " !")
}
if docType != models.Document_Type_Page &&
docType != models.Document_Type_Dir {
this.jsonError("文档类型错误!")
}
space, err := models.SpaceModel.GetSpaceBySpaceId(spaceId)
if err != nil {
this.ErrorLog("创建保存文档失败:" + err.Error())
this.jsonError("创建文档失败!")
}
if len(space) == 0 {
this.jsonError("空间不存在!")
}
// check space document privilege
_, isEditor, _ := this.GetDocumentPrivilege(space)
if !isEditor {
this.jsonError("您没有权限在该空间下创建文档!")
}
parentDocument, err := models.DocumentModel.GetDocumentByDocumentId(parentId)
if err != nil {
this.ErrorLog("创建保存文档失败:" + err.Error())
this.jsonError("创建文档失败!")
}
if len(parentDocument) == 0 {
this.jsonError("父文档不存在!")
}
if parentDocument["type"] != fmt.Sprintf("%d", models.Document_Type_Dir) {
this.jsonError("父文档不是目录!")
}
// check document name
document, err := models.DocumentModel.GetDocumentByNameParentIdAndSpaceId(name, parentId, spaceId, docType)
if err != nil {
this.ErrorLog("创建保存文档失败:" + err.Error())
this.jsonError("创建文档失败!")
}
if len(document) != 0 {
this.jsonError("该文档名称已经存在!")
}
insertDocument := map[string]interface{}{
"parent_id": parentId,
"space_id": spaceId,
"name": name,
"type": docType,
"path": parentDocument["path"] + "," + parentId,
"create_user_id": this.UserId,
"edit_user_id": this.UserId,
}
documentId, err := models.DocumentModel.Insert(insertDocument)
if err != nil {
this.ErrorLog("创建文档失败:" + err.Error())
this.jsonError("创建文档失败")
}
this.InfoLog("创建文档 " + utils.Convert.IntToString(documentId, 10) + " 成功")
this.jsonSuccess("创建文档成功", nil, "/document/index?document_id="+utils.Convert.IntToString(documentId, 10))
}
// document history
func (this *DocumentController) History() {
page, _ := this.GetInt("page", 1)
documentId := this.GetString("document_id", "0")
number, _ := this.GetRangeInt("number", 10, 10, 100)
limit := (page - 1) * number
if documentId == "0" {
this.ViewError("没有选择文档目录!")
}
document, err := models.DocumentModel.GetDocumentByDocumentId(documentId)
if err != nil {
this.ErrorLog("查看文档 " + documentId + " 修改历史失败:" + err.Error())
this.ViewError("查看文档修改历史失败!")
}
if len(document) == 0 {
this.jsonError("文档不存在!")
}
spaceId := document["space_id"]
space, err := models.SpaceModel.GetSpaceBySpaceId(spaceId)
if err != nil {
this.ErrorLog("查找文档 " + documentId + " 所在空间失败:" + err.Error())
this.ViewError("查找文档失败!")
}
if len(space) == 0 {
this.ViewError("文档所在空间不存在!")
}
// check space visit_level
isVisit, _, _ := this.GetDocumentPrivilege(space)
if !isVisit {
this.ViewError("您没有权限查看该空间修改历史!")
}
logDocuments, err := models.LogDocumentModel.GetLogDocumentsByDocumentIdAndLimit(documentId, limit, number)
if err != nil {
this.ErrorLog("查看文档 " + documentId + " 修改历史失败:" + err.Error())
this.ViewError("查看文档修改历史失败!")
}
count, err := models.LogDocumentModel.CountLogDocumentsByDocumentId(documentId)
if err != nil {
this.ErrorLog("查看文档 " + documentId + " 修改历史失败:" + err.Error())
this.ViewError("查看文档修改历史失败!")
}
userIds := []string{}
for _, logDocument := range logDocuments {
userIds = append(userIds, logDocument["user_id"])
}
users, err := models.UserModel.GetUsersByUserIds(userIds)
if err != nil {
this.ErrorLog("查看文档 " + documentId + " 修改历史失败:" + err.Error())
this.ViewError("查看文档修改历史失败!")
}
for _, logDocument := range logDocuments {
logDocument["username"] = ""
for _, user := range users {
if logDocument["user_id"] == user["user_id"] {
logDocument["username"] = user["username"]
break
}
}
}
this.Data["logDocuments"] = logDocuments
this.SetPaginator(number, count)
this.viewLayout("document/history", "default")
}
// move document
func (this *DocumentController) Move() {
documentId := this.GetString("document_id", "0")
targetId := this.GetString("target_id", "0")
if documentId == "0" {
this.jsonError("没有选择文档节点!")
}
if targetId == "0" {
this.jsonError("没有选择目标文档节点!")
}
document, err := models.DocumentModel.GetDocumentByDocumentId(documentId)
if err != nil {
this.ErrorLog("查找移动文档失败:" + err.Error())
this.jsonError("移动文档失败!")
}
if len(document) == 0 {
this.jsonError("文档不存在!")
}
if document["type"] == fmt.Sprintf("%d", models.Document_Type_Dir) {
this.jsonError("不能移动文档目录!")
}
targetDocument, err := models.DocumentModel.GetDocumentByDocumentId(targetId)
if err != nil {
this.ErrorLog("查找目标文档失败:" + err.Error())
this.jsonError("移动文档失败!")
}
if len(targetDocument) == 0 {
this.jsonError("目标文档不存在!")
}
if document["space_id"] != targetDocument["space_id"] {
this.jsonError("文档和目标文档不在同一空间!")
}
if targetDocument["type"] != fmt.Sprintf("%d", models.Document_Type_Dir) {
this.jsonError("目标文档必须是目录!")
}
space, err := models.SpaceModel.GetSpaceBySpaceId(document["space_id"])
if err != nil {
this.ErrorLog("移动文档失败:" + err.Error())
this.jsonError("移动文档失败!")
}
if len(space) == 0 {
this.jsonError("文档空间不存在!")
}
// check space document privilege
_, isEditor, _ := this.GetDocumentPrivilege(space)
if !isEditor {
this.jsonError("您没有权限移动该空间下的文档!")
}
_, oldPageFile, err := models.DocumentModel.GetParentDocumentsByDocument(document)
if err != nil {
this.ErrorLog("移动文档 " + documentId + " 失败:" + err.Error())
this.jsonError("移动文档失败!")
}
newDocument := map[string]string{
"space_id": document["space_id"],
"parent_id": targetId,
"name": document["name"],
"type": document["type"],
"path": targetDocument["path"] + "," + targetId,
}
_, newPageFile, err := models.DocumentModel.GetParentDocumentsByDocument(newDocument)
if err != nil {
this.ErrorLog("移动文档 " + documentId + " 失败:" + err.Error())
this.jsonError("移动文档失败!")
}
// update database and move document file
updateValue := map[string]interface{}{
"parent_id": targetId,
"path": targetDocument["path"] + "," + targetId,
"edit_user_id": this.UserId,
}
_, err = models.DocumentModel.MoveDBAndFile(documentId, updateValue, oldPageFile, newPageFile, document["type"], "移动文档到 "+targetDocument["name"])
if err != nil {
this.ErrorLog("移动文档 " + documentId + " 失败:" + err.Error())
this.jsonError("移动文档失败!")
}
this.InfoLog("移动文档 " + documentId + " 成功")
this.jsonSuccess("移动文档成功", nil, "/document/index?document_id="+documentId)
}
// delete document
func (this *DocumentController) Delete() {
documentId := this.GetString("document_id", "0")
if documentId == "0" {
this.jsonError("没有选择文档!")
}
document, err := models.DocumentModel.GetDocumentByDocumentId(documentId)
if err != nil {
this.ErrorLog("删除文档失败:" + err.Error())
this.jsonError("删除文档失败!")
}
if len(document) == 0 {
this.jsonError("文档不存在!")
}
if document["type"] == fmt.Sprintf("%d", models.Document_Type_Dir) {
childDocs, err := models.DocumentModel.GetDocumentsByParentId(document["document_id"])
if err != nil {
this.ErrorLog("删除文档失败:" + err.Error())
this.jsonError("删除文档失败!")
}
if len(childDocs) > 0 {
this.jsonError("请先删除或移动目录下所有文档!")
}
}
space, err := models.SpaceModel.GetSpaceBySpaceId(document["space_id"])
if err != nil {
this.ErrorLog("删除文档失败:" + err.Error())
this.jsonError("删除文档失败!")
}
if len(space) == 0 {
this.jsonError("文档空间不存在!")
}
// check space document privilege
_, _, isManager := this.GetDocumentPrivilege(space)
if !isManager {
this.jsonError("您没有权限删除该空间下的文档!")
}
_, pageFile, err := models.DocumentModel.GetParentDocumentsByDocument(document)
if err != nil {
this.ErrorLog("删除文档 " + documentId + " 失败:" + err.Error())
this.jsonError("删除文档失败!")
}
err = models.DocumentModel.DeleteDBAndFile(documentId, this.UserId, pageFile, document["type"])
if err != nil {
this.ErrorLog("删除文档 " + documentId + " 失败:" + err.Error())
this.jsonError("删除文档失败!")
}
// delete attachment
err = models.AttachmentModel.DeleteAttachmentsDBFileByDocumentId(documentId)
if err != nil {
this.ErrorLog("删除文档 " + documentId + " 附件失败:" + err.Error())
}
this.InfoLog("删除文档 " + documentId + " 成功")
this.jsonSuccess("删除文档成功", "", "/document/index?document_id="+document["parent_id"])
}