/
entry.go
186 lines (139 loc) · 3.68 KB
/
entry.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
//go:generate rice embed-go
package web
import (
"github.com/adriamb/runes/server/instance"
"github.com/adriamb/runes/store"
"github.com/adriamb/runes/store/model"
"github.com/adriamb/runes/web/render"
"github.com/gin-gonic/gin"
"net/http"
"os"
"sort"
"strings"
"time"
)
const (
versionDateTimeDisplay = "2006-01-02 15:04:05"
)
type dtoMarkdownRender struct {
Markdown string `json:"markdown" binding:"required"`
}
// Version of an entry
type Version struct {
Description string
URL string
}
func doPOSTUpload(c *gin.Context) {
ws := normalize(c.Param("ws"))
id := normalize(c.Param("id"))
file, fileHeader, err := c.Request.FormFile("file")
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
filename := normalize(fileHeader.Filename)
err = instance.Srv.Store.Entry.StoreFile(ws, id, filename, file)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
path := "/w/" + ws + "/e/" + id + "/f/" + filename
split := strings.Split(filename, ".")
ext := split[len(split)-1]
ico := ""
if ext == "png" || ext == "jpg" || ext == "jpeg" || ext == "gif" {
} else if _, err := os.Stat("web/static/ico/" + ext + ".png"); err == nil {
ico = "/static/ico/" + ext + ".png"
} else {
ico = "/static/ico/default.png"
}
c.JSON(http.StatusOK, gin.H{"name": fileHeader.Filename, "path": path, "ico": ico})
}
func doPOSTRender(c *gin.Context) {
var json dtoMarkdownRender
if c.BindJSON(&json) == nil {
html := string(render.Render(json.Markdown))
c.JSON(http.StatusOK, gin.H{"html": html})
}
}
func doGETEntryEdit(c *gin.Context) {
ws := normalize(c.Param("ws"))
id := normalize(c.Param("id"))
var entry *model.Entry
var err error
var editable = true
versions := []Version{}
if id != "new" {
if strings.Contains(id, ".") {
split := strings.SplitN(id, ".", 2)
id = split[0]
version := split[1]
entry, err = instance.Srv.Store.Entry.GetEntry(ws, id, version)
editable = false
} else {
entry, err = instance.Srv.Store.Entry.GetEntry(ws, id, "")
}
if err == nil {
versions = append(versions, Version{
Description: "Last",
URL: "/w/" + ws + "/e/" + id + "/edit",
})
var versionids []string
versionids, err = instance.Srv.Store.Entry.GetEntryVersions(ws, id)
sort.Sort(sort.Reverse(sort.StringSlice(versionids)))
if len(versionids) > 15 {
versionids = versionids[:15]
}
for _, versionid := range versionids {
t, timeerr := time.Parse(store.DateTimeFormat, versionid)
if timeerr == nil {
versions = append(versions, Version{
Description: t.Format(versionDateTimeDisplay),
URL: "/w/" + ws + "/e/" + id + "." + versionid + "/edit",
})
}
}
}
if err != nil {
dumpError(c, err)
return
}
} else {
entry = &model.Entry{
ID: instance.Srv.Store.Entry.NewID(),
Markdown: "# Title\n",
}
}
c.HTML(http.StatusOK, "entry.tmpl", gin.H{
"htmlHeaders": render.HTMLHeaders(),
"ws": ws,
"entry": entry,
"editable": editable,
"versions": versions,
})
}
func doPOSTEntryDelete(c *gin.Context) {
ws := normalize(c.Param("ws"))
id := normalize(c.Param("id"))
err := instance.Srv.Store.Entry.DeleteEntry(ws, id)
if err != nil {
dumpError(c, err)
}
c.Redirect(http.StatusSeeOther, "/w/"+ws)
}
func doPOSTEntry(c *gin.Context) {
ws := normalize(c.Param("ws"))
id := normalize(c.Param("id"))
entry := model.Entry{
Workspace: ws,
ID: id,
Markdown: c.DefaultPostForm("Markdown", "undefined"),
}
err := instance.Srv.Store.Entry.StoreEntry(&entry)
if err != nil {
dumpError(c, err)
return
}
c.Redirect(http.StatusSeeOther, "/w/"+ws)
return
}