This repository has been archived by the owner on Sep 19, 2021. It is now read-only.
/
attachment.go
327 lines (284 loc) · 10.8 KB
/
attachment.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
package http
import (
"bytes"
"encoding/base64"
"fmt"
"io"
"net/http"
"path/filepath"
"strconv"
"strings"
"github.com/18F/e-QIP-prototype/api"
"github.com/gorilla/mux"
)
// AttachmentListHandler is the handler for listing attachments.
type AttachmentListHandler struct {
Env api.Settings
Log api.LogService
Token api.TokenService
Database api.DatabaseService
}
// ServeHTTP serves the HTTP response.
func (service AttachmentListHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if !service.Env.True(api.AttachmentsEnabled) {
service.Log.Warn(api.AttachmentsNotImplemented, api.LogFields{})
RespondWithStructuredError(w, api.AttachmentsNotImplemented, http.StatusInternalServerError)
return
}
// Get account ID
id := AccountIDFromRequestContext(r)
// Get the account information from the data store.
// Proceed even if the account is locked, as files are presented
// after application submission, on the Print page.
account := &api.Account{ID: id}
if _, err := account.Get(service.Database, id); err != nil {
service.Log.WarnError(api.NoAccount, err, api.LogFields{})
RespondWithStructuredError(w, api.NoAccount, http.StatusUnauthorized)
return
}
var attachments []api.Attachment
if err := service.Database.Where(&attachments, "account_id = ?", account.ID); err != nil {
service.Log.Warn(api.AttachmentNotFound, api.LogFields{})
RespondWithStructuredError(w, api.AttachmentNotFound, http.StatusInternalServerError)
return
}
EncodeJSON(w, attachments)
}
// AttachmentSaveHandler is the handler for saving attachments.
type AttachmentSaveHandler struct {
Env api.Settings
Log api.LogService
Token api.TokenService
Database api.DatabaseService
}
// ServeHTTP serves the HTTP response.
func (service AttachmentSaveHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if !service.Env.True(api.AttachmentsEnabled) {
service.Log.Warn(api.AttachmentsNotImplemented, api.LogFields{})
RespondWithStructuredError(w, api.AttachmentsNotImplemented, http.StatusInternalServerError)
return
}
// Get account ID
id := AccountIDFromRequestContext(r)
// Get the account information from the data store
account := &api.Account{ID: id}
if _, err := account.Get(service.Database, id); err != nil {
service.Log.WarnError(api.NoAccount, err, api.LogFields{})
RespondWithStructuredError(w, api.NoAccount, http.StatusUnauthorized)
return
}
// If the account is locked then we cannot proceed
if account.Locked {
service.Log.Warn(api.AccountLocked, api.LogFields{})
RespondWithStructuredError(w, api.AccountLocked, http.StatusForbidden)
return
}
// Retrieve the file and metadata from the multipart form data.
r.ParseMultipartForm(32 << 20)
file, header, err := r.FormFile("file")
if err != nil {
service.Log.WarnError(api.AttachmentNoFile, err, api.LogFields{})
RespondWithStructuredError(w, api.AttachmentNoFile, http.StatusBadRequest)
return
}
defer file.Close()
// Copy the buffer so we can safely write it to storage.
var buffer bytes.Buffer
if _, err := io.Copy(&buffer, file); err != nil {
service.Log.WarnError(api.AttachmentCopyBufferError, err, api.LogFields{})
RespondWithStructuredError(w, api.AttachmentCopyBufferError, http.StatusInternalServerError)
return
}
// Check size contraints
maximumSize := service.Env.Int(api.FileMaximumSize)
bufferSize := buffer.Len()
headerSize := int(header.Size)
if headerSize != bufferSize {
service.Log.Warn(api.AttachmentSizeMismatch, api.LogFields{"header": headerSize, "buffer": bufferSize})
RespondWithStructuredError(w, api.AttachmentSizeMismatch, http.StatusInternalServerError)
return
}
if headerSize > maximumSize {
service.Log.Warn(api.AttachmentSizeExceeded, api.LogFields{"size": headerSize})
RespondWithStructuredError(w, api.AttachmentSizeExceeded, http.StatusInternalServerError)
return
}
// Check file type constraints
allowedTypes := strings.Split(service.Env.String(api.FileTypes), ";")
extension := filepath.Ext(header.Filename)
allowed := false
for _, ext := range allowedTypes {
if strings.EqualFold(ext, extension) {
allowed = true
break
}
}
if !allowed {
service.Log.Warn(api.AttachmentTypeNotAllowed, api.LogFields{"extension": extension})
RespondWithStructuredError(w, api.AttachmentTypeNotAllowed, http.StatusInternalServerError)
return
}
// Create the attachment and store any metadata
attachment := &api.Attachment{
AccountID: account.ID,
Filename: header.Filename,
Size: header.Size,
Raw: buffer.Bytes(),
}
if _, err := attachment.Save(service.Database, id); err != nil {
service.Log.WarnError(api.AttachmentNotSaved, err, api.LogFields{})
RespondWithStructuredError(w, api.AttachmentNotSaved, http.StatusInternalServerError)
return
}
buffer.Reset()
service.Log.Info(api.AttachmentSaved, api.LogFields{"attachment": attachment.ID})
fmt.Fprint(w, attachment.ID)
}
// AttachmentUpdateHandler is the handler for updating attachments.
type AttachmentUpdateHandler struct {
Env api.Settings
Log api.LogService
Token api.TokenService
Database api.DatabaseService
}
// ServeHTTP serves the HTTP response.
func (service AttachmentUpdateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if !service.Env.True(api.AttachmentsEnabled) {
service.Log.Warn(api.AttachmentsNotImplemented, api.LogFields{})
RespondWithStructuredError(w, api.AttachmentsNotImplemented, http.StatusInternalServerError)
return
}
// Get account ID
id := AccountIDFromRequestContext(r)
// Get the account information from the data store
account := &api.Account{ID: id}
if _, err := account.Get(service.Database, id); err != nil {
service.Log.WarnError(api.NoAccount, err, api.LogFields{})
RespondWithStructuredError(w, api.NoAccount, http.StatusUnauthorized)
return
}
// If the account is locked then we cannot proceed
if account.Locked {
service.Log.Warn(api.AccountLocked, api.LogFields{})
RespondWithStructuredError(w, api.AccountLocked, http.StatusForbidden)
return
}
// Get the attachment by identifier.
vars := mux.Vars(r)
attachmentID, err := strconv.Atoi(vars["id"])
if err != nil {
service.Log.WarnError(api.AttachmentNoID, err, api.LogFields{})
RespondWithStructuredError(w, api.AttachmentNoID, http.StatusBadRequest)
return
}
attachment := &api.Attachment{ID: attachmentID}
if _, err := attachment.Get(service.Database, account.ID); err != nil {
service.Log.WarnError(api.AttachmentNotFound, err, api.LogFields{"attachment": attachmentID})
RespondWithStructuredError(w, api.AttachmentNotFound, http.StatusNotFound)
return
}
// Get the body of additional settings to update.
var body struct {
Description string `json:"description"`
}
if err := DecodeJSON(r.Body, &body); err != nil {
service.Log.WarnError(api.PayloadDeserializeError, err, api.LogFields{})
RespondWithStructuredError(w, api.PayloadDeserializeError, http.StatusBadRequest)
}
// Apply the settings and save it back to storage.
attachment.Description = body.Description
if _, err := attachment.Save(service.Database, account.ID); err != nil {
service.Log.WarnError(api.AttachmentNotSaved, err, api.LogFields{})
RespondWithStructuredError(w, api.AttachmentNotSaved, http.StatusBadRequest)
return
}
service.Log.Info(api.AttachmentSaved, api.LogFields{"attachment": attachment.ID})
fmt.Fprint(w, attachment.ID)
}
// AttachmentGetHandler is the handler for getting attachments.
type AttachmentGetHandler struct {
Env api.Settings
Log api.LogService
Token api.TokenService
Database api.DatabaseService
}
// ServeHTTP serves the HTTP response.
func (service AttachmentGetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if !service.Env.True(api.AttachmentsEnabled) {
service.Log.Warn(api.AttachmentsNotImplemented, api.LogFields{})
RespondWithStructuredError(w, api.AttachmentsNotImplemented, http.StatusInternalServerError)
return
}
// Get account ID
id := AccountIDFromRequestContext(r)
// Get the account information from the data store.
// Proceed even if the account is locked, as files are presented
// after application submission, on the Print page.
account := &api.Account{ID: id}
if _, err := account.Get(service.Database, id); err != nil {
service.Log.WarnError(api.NoAccount, err, api.LogFields{})
RespondWithStructuredError(w, api.NoAccount, http.StatusUnauthorized)
return
}
// Get the attachment by identifier.
vars := mux.Vars(r)
attachmentID, err := strconv.Atoi(vars["id"])
if err != nil {
service.Log.WarnError(api.AttachmentNoID, err, api.LogFields{})
RespondWithStructuredError(w, api.AttachmentNoID, http.StatusBadRequest)
return
}
attachment := &api.Attachment{ID: attachmentID}
if _, err := attachment.Get(service.Database, account.ID); err != nil {
service.Log.WarnError(api.AttachmentNotFound, err, api.LogFields{"attachment": attachmentID})
RespondWithStructuredError(w, api.AttachmentNotFound, http.StatusNotFound)
return
}
service.Log.Info(api.AttachmentDownloaded, api.LogFields{"attachment": attachmentID})
fmt.Fprint(w, base64.StdEncoding.EncodeToString(attachment.Raw))
}
// AttachmentDeleteHandler is the handler for deleting attachments.
type AttachmentDeleteHandler struct {
Env api.Settings
Log api.LogService
Token api.TokenService
Database api.DatabaseService
}
// ServeHTTP serves the HTTP response.
func (service AttachmentDeleteHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if !service.Env.True(api.AttachmentsEnabled) {
service.Log.Warn(api.AttachmentsNotImplemented, api.LogFields{})
RespondWithStructuredError(w, api.AttachmentsNotImplemented, http.StatusInternalServerError)
return
}
// Get account ID
id := AccountIDFromRequestContext(r)
// Get the account information from the data store
account := &api.Account{ID: id}
if _, err := account.Get(service.Database, id); err != nil {
service.Log.WarnError(api.NoAccount, err, api.LogFields{})
RespondWithStructuredError(w, api.NoAccount, http.StatusUnauthorized)
return
}
// If the account is locked then we cannot proceed
if account.Locked {
service.Log.Warn(api.AccountLocked, api.LogFields{})
RespondWithStructuredError(w, api.AccountLocked, http.StatusForbidden)
return
}
// Get the attachment by identifier.
vars := mux.Vars(r)
attachmentID, err := strconv.Atoi(vars["id"])
if err != nil {
service.Log.WarnError(api.AttachmentNoID, err, api.LogFields{})
RespondWithStructuredError(w, api.AttachmentNoID, http.StatusBadRequest)
return
}
attachment := &api.Attachment{ID: attachmentID}
if _, err := attachment.Delete(service.Database, account.ID); err != nil {
service.Log.WarnError(api.AttachmentDeleted, err, api.LogFields{"attachment": attachmentID})
RespondWithStructuredError(w, api.AttachmentDeleted, http.StatusInternalServerError)
return
}
service.Log.Info(api.AttachmentDeleted, api.LogFields{"attachment": attachmentID})
}