-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_page.go
116 lines (111 loc) · 2.61 KB
/
server_page.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
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"path"
"path/filepath"
)
func serverAddPage(root *gin.RouterGroup) {
root.GET("/new", func(c *gin.Context) {
c.HTML(200, "page-new.tmpl", gin.H{
"root": webroot,
"title": "",
"unique": "",
})
})
root.POST("/edit", func(c *gin.Context) {
unique := c.DefaultPostForm("unique", "")
title := c.DefaultPostForm("title", "")
if len(unique) > 0 {
valid := IsValidNonce(unique)
if valid && notebook.rename(unique, title) {
c.Redirect(302, webroot+"view/"+unique)
} else {
c.HTML(500, "error.tmpl", gin.H{
"root": webroot,
"error": "cannot rename page",
})
}
} else {
file, err := c.FormFile("binary")
if err != nil {
fmt.Println(err)
c.HTML(500, "error.tmpl", gin.H{
"root": webroot,
"error": "invalid or missing binary",
})
return
}
filename := filepath.Base(file.Filename)
if len(filename) < 1 {
c.HTML(500, "error.tmpl", gin.H{
"root": webroot,
"error": "invalid binary name",
})
return
}
ext := filepath.Ext(filename)
binary := Nonce(ELEMENT_NONCE_SIZE) + ext
unique := notebook.new(title, filename, binary)
if len(unique) < 1 {
c.HTML(500, "error.tmpl", gin.H{
"root": webroot,
"error": "cannot create page",
})
return
}
if c.SaveUploadedFile(file, path.Join(notebook.storage, unique, binary)) != nil {
notebook.delete(unique)
c.HTML(500, "error.tmpl", gin.H{
"root": webroot,
"error": "cannot save binary",
})
} else {
c.Redirect(302, webroot+"view/"+unique)
}
}
})
root.GET("/delete/:unique", func(c *gin.Context) {
unique := c.Param("unique")
if !IsValidNonce(unique) || !notebook.delete(unique) {
c.HTML(404, "error.tmpl", gin.H{
"root": webroot,
"error": "cannot find page",
})
} else {
c.Redirect(302, webroot)
}
})
root.GET("/edit/:unique", func(c *gin.Context) {
unique := c.Param("unique")
page := notebook.get(unique)
if page == nil {
c.HTML(404, "error.tmpl", gin.H{
"root": webroot,
"error": "cannot find page",
})
} else {
c.HTML(200, "page-new.tmpl", gin.H{
"root": webroot,
"title": page["title"],
"unique": unique,
})
}
})
root.GET("/view/:unique", func(c *gin.Context) {
unique := c.Param("unique")
page := notebook.get(unique)
if page == nil {
c.HTML(404, "error.tmpl", gin.H{
"root": webroot,
"error": "cannot find a new page",
})
} else {
c.HTML(200, "page-view.tmpl", gin.H{
"root": webroot,
"page": page,
"pipe": notebook.open(unique, false) != nil,
})
}
})
}