-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers_attachments.go
203 lines (169 loc) · 5.84 KB
/
handlers_attachments.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
package api
import (
"encoding/json"
"fmt"
"net/http"
"github.com/YaleSpinup/ds-api/apierror"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
)
// limit individual attachment files to 32 MB
const maxAttachmentSize = 32 * 1024 * 1024
// AttachmentCreateHandler adds an attachment file to a dataset
func (s *server) AttachmentCreateHandler(w http.ResponseWriter, r *http.Request) {
w = LogWriter{w}
vars := mux.Vars(r)
account := vars["account"]
id := vars["id"]
service, ok := s.datasetServices[account]
if !ok {
msg := fmt.Sprintf("account not found: %s", account)
handleError(w, apierror.New(apierror.ErrNotFound, msg, nil))
return
}
metadata, err := service.MetadataRepository.Get(r.Context(), account, id)
if err != nil {
handleError(w, err)
return
}
attachmentRepo, ok := service.AttachmentRepository[metadata.DataStorage]
if !ok {
msg := fmt.Sprintf("requested attachment repository type not supported for this account: %s", metadata.DataStorage)
handleError(w, apierror.New(apierror.ErrBadRequest, msg, nil))
return
}
// limit total request size to 50 MB
r.Body = http.MaxBytesReader(w, r.Body, 50<<20)
defer r.Body.Close()
// parse the multipart form and keep up to 32 MB in memory (the rest on temp disk)
err = r.ParseMultipartForm(32 << 20)
if err != nil {
handleError(w, apierror.New(apierror.ErrBadRequest, "failed to parse multipart request", err))
return
}
// get the attachment name
attachmentName := r.FormValue("name")
if attachmentName == "" {
handleError(w, apierror.New(apierror.ErrBadRequest, "failed to parse form value: name", err))
return
}
log.Debugf("attachment name: %s", attachmentName)
// get the (first) attachment file
attachment, attachmentHeader, err := r.FormFile("attachment")
if err != nil {
handleError(w, apierror.New(apierror.ErrBadRequest, "failed to parse attachment", err))
return
}
defer attachment.Close()
log.Debugf("attachment size (bytes): %v", attachmentHeader.Size)
if attachmentHeader.Size > maxAttachmentSize {
msg := fmt.Sprintf("attachment size too big (max limit is %d bytes)", maxAttachmentSize)
handleError(w, apierror.New(apierror.ErrBadRequest, msg, err))
return
}
err = attachmentRepo.CreateAttachment(r.Context(), id, attachmentName, attachment)
if err != nil {
msg := fmt.Sprintf("failed to create attachment for dataset %s: %s", id, err)
handleError(w, apierror.New(apierror.ErrInternalError, msg, err))
return
}
output := []string{
attachmentName,
}
j, err := json.Marshal(&output)
if err != nil {
msg := fmt.Sprintf("cannot encode dataset attachment output into json: %s", err)
handleError(w, apierror.New(apierror.ErrBadRequest, msg, err))
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(j)
}
// AttachmentListHandler lists all attachments for a dataset
func (s *server) AttachmentListHandler(w http.ResponseWriter, r *http.Request) {
w = LogWriter{w}
vars := mux.Vars(r)
account := vars["account"]
id := vars["id"]
service, ok := s.datasetServices[account]
if !ok {
msg := fmt.Sprintf("account not found: %s", account)
handleError(w, apierror.New(apierror.ErrNotFound, msg, nil))
return
}
metadata, err := service.MetadataRepository.Get(r.Context(), account, id)
if err != nil {
handleError(w, err)
return
}
attachmentRepo, ok := service.AttachmentRepository[metadata.DataStorage]
if !ok {
msg := fmt.Sprintf("requested attachment repository type not supported for this account: %s", metadata.DataStorage)
handleError(w, apierror.New(apierror.ErrBadRequest, msg, nil))
return
}
// list attachments for this data repository
datasetAttachments, err := attachmentRepo.ListAttachments(r.Context(), id, true)
if err != nil {
msg := fmt.Sprintf("failed to list attachments for dataset %s: %s", id, err)
handleError(w, apierror.New(apierror.ErrInternalError, msg, err))
return
}
j, err := json.Marshal(&datasetAttachments)
if err != nil {
msg := fmt.Sprintf("cannot encode dataset attachments output into json: %s", err)
handleError(w, apierror.New(apierror.ErrBadRequest, msg, err))
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(j)
}
// AttachmentDeleteHandler removes an attachment file from a dataset
func (s *server) AttachmentDeleteHandler(w http.ResponseWriter, r *http.Request) {
w = LogWriter{w}
vars := mux.Vars(r)
account := vars["account"]
id := vars["id"]
service, ok := s.datasetServices[account]
if !ok {
msg := fmt.Sprintf("account not found: %s", account)
handleError(w, apierror.New(apierror.ErrNotFound, msg, nil))
return
}
input := struct {
AttachmentName string `json:"attachment_name"`
}{}
err := json.NewDecoder(r.Body).Decode(&input)
if err != nil {
msg := fmt.Sprintf("cannot decode body into delete attachment input: %s", err)
handleError(w, apierror.New(apierror.ErrBadRequest, msg, err))
return
}
if input.AttachmentName == "" {
handleError(w, apierror.New(apierror.ErrBadRequest, "attachment_name is required", nil))
return
}
metadata, err := service.MetadataRepository.Get(r.Context(), account, id)
if err != nil {
handleError(w, err)
return
}
attachmentRepo, ok := service.AttachmentRepository[metadata.DataStorage]
if !ok {
msg := fmt.Sprintf("requested attachment repository type not supported for this account: %s", metadata.DataStorage)
handleError(w, apierror.New(apierror.ErrBadRequest, msg, nil))
return
}
// delete attachment from this data repository
err = attachmentRepo.DeleteAttachment(r.Context(), id, input.AttachmentName)
if err != nil {
msg := fmt.Sprintf("failed to delete attachment '%s' for dataset %s: %s", input.AttachmentName, id, err)
handleError(w, apierror.New(apierror.ErrInternalError, msg, err))
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNoContent)
w.Write([]byte{})
}