-
Notifications
You must be signed in to change notification settings - Fork 3
/
files.go
269 lines (222 loc) · 6.63 KB
/
files.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
package server
import (
"fmt"
"net/http"
"strings"
"github.com/knoebber/dotfile/db"
"github.com/knoebber/dotfile/dotfile"
"github.com/knoebber/dotfile/usererror"
)
// Handles submitting the new file form.
func newTempFile(w http.ResponseWriter, r *http.Request, p *Page) (done bool) {
content := r.Form.Get("contents")
path := r.Form.Get("path")
alias, err := dotfile.Alias(r.Form.Get("alias"), path)
if err != nil {
return p.setError(w, err)
}
if err := db.ValidateFileNotExists(db.Connection, p.userID(), alias, path); err != nil {
return p.setError(w, err)
}
tempFile := &db.TempFileRecord{
UserID: p.userID(),
Alias: alias,
Path: path,
Content: []byte(content),
}
if err := tempFile.Create(db.Connection); err != nil {
return p.setError(w, err)
}
http.Redirect(w, r, fmt.Sprintf("/%s/%s/init", p.Username(), alias), http.StatusSeeOther)
return true
}
// Handles submitting the edit file form.
func editFile(w http.ResponseWriter, r *http.Request, p *Page) (done bool) {
content := r.Form.Get("contents")
existingFile, err := db.File(db.Connection, p.Username(), p.Vars["alias"])
if err != nil {
return p.setError(w, err)
}
alias := existingFile.Alias
path := existingFile.Path
tempFile := &db.TempFileRecord{
UserID: p.userID(),
Alias: alias,
Path: path,
Content: []byte(content),
}
if err := tempFile.Create(db.Connection); err != nil {
return p.setError(w, err)
}
http.Redirect(w, r, fmt.Sprintf("/%s/%s/commit", p.Username(), alias), http.StatusSeeOther)
return true
}
// Handles submitting the confirm file form.
// Either initializes a new file or makes a commit to an existing.
func confirmTempFile(w http.ResponseWriter, r *http.Request, p *Page) (done bool) {
if err := db.InitOrCommit(p.userID(), p.Vars["alias"], r.Form.Get("message")); err != nil {
return p.setError(w, err)
}
path := fmt.Sprintf("/%s/%s", p.Username(), p.Vars["alias"])
http.Redirect(w, r, path, http.StatusSeeOther)
return true
}
// Forks another user's file at hash.
func forkFile(w http.ResponseWriter, r *http.Request, p *Page) (done bool) {
username := r.Form.Get("username")
alias := r.Form.Get("alias")
hash := r.Form.Get("hash")
loggedInUserID := p.userID()
if loggedInUserID < 1 {
return p.setError(w, usererror.Invalid("Must be logged in to fork file."))
}
if err := db.ForkFile(username, alias, hash, loggedInUserID); err != nil {
return p.setError(w, err)
}
return
}
// Uncompress file and sets content to page data.
func uncompressFile(w http.ResponseWriter, r *http.Request, p *Page) (done bool) {
if !strings.Contains(r.Header.Get("Accept"), "text/html") {
handleRawFile(w, r)
return true
}
username := p.Vars["username"]
alias := p.Vars["alias"]
file, err := db.UncompressFile(db.Connection, username, alias)
if err != nil {
return p.setError(w, err)
}
p.Data["path"] = file.Path
p.Data["content"] = string(file.Content)
p.Data["hash"] = file.Hash
p.Title = file.Alias
return
}
// Loads data into the create/edit form.
// Fills the text area with content from a tempfile or a current file depending on query params.
func loadTempFileForm(w http.ResponseWriter, r *http.Request, p *Page) (done bool) {
pageAlias := p.Vars["alias"]
newFile := pageAlias == ""
p.Data["newFile"] = newFile
if newFile {
p.Title = "new file"
} else {
p.Title = "edit"
}
// Edit from a specific hash with the at param.
at := r.URL.Query().Get("at")
// Load the user's temp file if there is an ?edit query param.
editing := r.URL.Query().Get("edit") == "true"
if newFile && !editing {
// A new file that is not being edited. No content needs to be loaded.
return
}
if editing {
tempFile, err := db.TempFile(db.Connection, p.userID(), pageAlias)
if err != nil && !db.NotFound(err) {
return p.setError(w, err)
}
if tempFile == nil {
return
}
p.Data["alias"] = tempFile.Alias
p.Data["path"] = tempFile.Path
p.Data["content"] = string(tempFile.Content)
return
}
if at == "" {
// Load the current content.
file, err := db.UncompressFile(db.Connection, p.Vars["username"], pageAlias)
if err != nil {
return p.setError(w, err)
}
p.Data["path"] = file.Path
p.Data["content"] = string(file.Content)
return
} else if !newFile && !editing && at != "" {
commit, err := db.UncompressCommit(db.Connection, p.Vars["username"], pageAlias, at, p.Timezone())
if err != nil {
return p.setError(w, err)
}
p.Data["path"] = commit.Path
p.Data["content"] = string(commit.Content)
}
return
}
// Loads the contents of a users temp file for the confirm edit page.
func loadCommitConfirm(w http.ResponseWriter, r *http.Request, p *Page) (done bool) {
alias := p.Vars["alias"]
f, err := db.UncompressFile(db.Connection, p.Username(), alias)
if err != nil {
return p.setError(w, err)
}
p.Data["editAction"] = fmt.Sprintf("/%s/%s/edit", p.Username(), alias)
p.Data["alias"] = f.Alias
p.Data["path"] = f.Path
diff, err := dotfile.DiffPrettyHTML(&db.FileContent{
Connection: db.Connection,
Username: p.Username(),
UserID: p.userID(),
Alias: alias,
}, f.Hash, "")
if err != nil {
return p.setError(w, err)
}
p.Data["diff"] = diff
return
}
// Loads the contents of a users temp file for the new file page.
func loadNewFileConfirm(w http.ResponseWriter, r *http.Request, p *Page) (done bool) {
tempFile, err := db.TempFile(db.Connection, p.userID(), p.Vars["alias"])
if err != nil {
return p.setError(w, err)
}
p.Data["alias"] = tempFile.Alias
p.Data["path"] = tempFile.Path
p.Data["content"] = string(tempFile.Content)
p.Data["editAction"] = "/new_file"
p.Title = "confirm new file"
return
}
func newFileHandler() http.HandlerFunc {
return createHandler(&pageDescription{
templateName: "file_form.tmpl",
title: "new file",
handleForm: newTempFile,
loadData: loadTempFileForm,
protected: true,
})
}
func editFileHandler() http.HandlerFunc {
return createHandler(&pageDescription{
templateName: "file_form.tmpl",
handleForm: editFile,
loadData: loadTempFileForm,
protected: true,
})
}
func confirmNewFileHandler() http.HandlerFunc {
return createHandler(&pageDescription{
templateName: "confirm_file.tmpl",
handleForm: confirmTempFile,
loadData: loadNewFileConfirm,
protected: true,
})
}
func confirmEditHandler() http.HandlerFunc {
return createHandler(&pageDescription{
templateName: "confirm_edit.tmpl",
title: "commit",
handleForm: confirmTempFile,
loadData: loadCommitConfirm,
protected: true,
})
}
func fileHandler() http.HandlerFunc {
return createHandler(&pageDescription{
templateName: "file.tmpl",
handleForm: forkFile,
loadData: uncompressFile,
})
}