-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.go
226 lines (187 loc) · 5.78 KB
/
upload.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
package main
import (
"encoding/json"
"github.com/gorilla/securecookie"
"github.com/gorilla/sessions"
"github.com/joinmytalk/xlog"
"github.com/surma-dump/gouuid"
"net/http"
"time"
)
// Upload describes an uploaded presentation.
type Upload struct {
ID int `meddler:"id,pk" json:"-"`
Title string `meddler:"title" json:"title"`
PublicID string `meddler:"public_id" json:"id"`
UserID int `meddler:"user_id" json:"-"`
Uploaded time.Time `meddler:"uploaded,utctimez"`
Conversion string `meddler:"conversion" json:"conversion"`
}
// UploadHandler handles the file upload.
type UploadHandler struct {
SessionStore sessions.Store
DBStore *Store
UploadStore *FileUploadStore
SecureCookie *securecookie.SecureCookie
}
func (h *UploadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
/* // re-enable as soon as ng-upload can do that.
if !VerifyXSRFToken(w, r, h.SessionStore, h.SecureCookie) {
return
}
*/
session, err := h.SessionStore.Get(r, SESSIONNAME)
if err != nil {
xlog.Debugf("Getting session failed: %v", err)
StatCount("getting session failed", 1)
http.Error(w, err.Error(), http.StatusForbidden)
return
}
if session.Values["userID"] == nil {
http.Error(w, "authentication required", http.StatusForbidden)
return
}
if err := r.ParseMultipartForm(10 * 1024 * 1024); err != nil {
http.Error(w, "couldn't parse form", http.StatusInternalServerError)
return
}
StatCount("upload presentation", 1)
title := r.FormValue("title")
if title == "" {
http.Error(w, "empty title", http.StatusNotAcceptable)
return
}
file, fhdr, err := r.FormFile("file")
if err != nil {
http.Error(w, "couldn't read form", http.StatusInternalServerError)
return
}
id := generateID()
conversion := "success"
if isPDF, err := h.UploadStore.Store(id, file, fhdr.Filename); err != nil {
xlog.Errorf("Storing file for upload %s failed: %v", id, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
} else if !isPDF {
conversion = "progress"
}
if err := h.DBStore.InsertUpload(&Upload{
PublicID: id,
UserID: session.Values["userID"].(int),
Title: title,
Uploaded: time.Now(),
Conversion: conversion,
}); err != nil {
xlog.Errorf("Insert failed: %v", err)
http.Error(w, "insert failed", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"id": id})
}
// DeleteUploadHandler handles deleting of uploaded files.
type DeleteUploadHandler struct {
SessionStore sessions.Store
DBStore *Store
UploadStore *FileUploadStore
SecureCookie *securecookie.SecureCookie
}
func (h *DeleteUploadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if !VerifyXSRFToken(w, r, h.SessionStore, h.SecureCookie) {
return
}
session, err := h.SessionStore.Get(r, SESSIONNAME)
if err != nil {
xlog.Debugf("Getting session failed: %v", err)
StatCount("getting session failed", 1)
http.Error(w, err.Error(), http.StatusForbidden)
return
}
if session.Values["userID"] == nil {
http.Error(w, "authentication required", http.StatusForbidden)
return
}
StatCount("delete upload", 1)
requestData := struct {
UploadID string `json:"upload_id"`
}{}
if err := json.NewDecoder(r.Body).Decode(&requestData); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
rowsAffected, err := h.DBStore.DeleteUploadByPublicID(requestData.UploadID, session.Values["userID"].(int))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if rowsAffected > 0 {
h.UploadStore.Remove(requestData.UploadID)
}
w.WriteHeader(http.StatusNoContent)
}
// RenameUploadHandler handles changing file upload titles.
type RenameUploadHandler struct {
SessionStore sessions.Store
DBStore *Store
SecureCookie *securecookie.SecureCookie
}
func (h *RenameUploadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if !VerifyXSRFToken(w, r, h.SessionStore, h.SecureCookie) {
return
}
session, err := h.SessionStore.Get(r, SESSIONNAME)
if err != nil {
xlog.Debugf("Getting session failed: %v", err)
StatCount("getting session failed", 1)
http.Error(w, err.Error(), http.StatusForbidden)
return
}
if session.Values["userID"] == nil {
http.Error(w, "authentication required", http.StatusForbidden)
return
}
requestData := struct {
UploadID string `json:"upload_id"`
NewTitle string `json:"new_title"`
}{}
StatCount("rename upload", 1)
if err := json.NewDecoder(r.Body).Decode(&requestData); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if err := h.DBStore.SetTitleForPresentation(requestData.NewTitle, requestData.UploadID, session.Values["userID"].(int)); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}
// GetUploadsHandler returns a list of uploads for the current user.
type GetUploadsHandler struct {
SessionStore sessions.Store
DBStore *Store
}
func (h *GetUploadsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
session, err := h.SessionStore.Get(r, SESSIONNAME)
if err != nil {
xlog.Debugf("Getting session failed: %v", err)
StatCount("getting session failed", 1)
http.Error(w, err.Error(), http.StatusForbidden)
return
}
userID, ok := session.Values["userID"].(int)
if !ok {
http.Error(w, "no userID in session", http.StatusForbidden)
return
}
StatCount("get uploads", 1)
result, err := h.DBStore.GetUploadsForUser(userID)
if err != nil {
xlog.Errorf("Couldn't query uploads: %v", err)
http.Error(w, "query failed", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(result)
}
func generateID() string {
return gouuid.New().ShortString()
}