/
submit.go
441 lines (383 loc) · 12.4 KB
/
submit.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package public
import (
"context"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
radio "github.com/R-a-dio/valkyrie"
"github.com/R-a-dio/valkyrie/errors"
"github.com/R-a-dio/valkyrie/streamer/audio"
"github.com/R-a-dio/valkyrie/util"
"github.com/R-a-dio/valkyrie/util/secret"
"github.com/R-a-dio/valkyrie/website/middleware"
"github.com/rs/zerolog/hlog"
)
const (
formMaxCommentLength = 512
formMaxDaypassLength = 64
formMaxReplacementLength = 16
formMaxFileLength = (1 << 20) * 100 // 100MiB
formMaxSize = formMaxFileLength + (1<<20)*3 // 3MiB on-top of file limit
formMaxMIMEParts = 6
daypassHeader = "X-Daypass"
)
type SubmitInput struct {
middleware.Input
Form SubmissionForm
Stats radio.SubmissionStats
}
func NewSubmitInput(storage radio.SubmissionStorageService, r *http.Request, form *SubmissionForm) (*SubmitInput, error) {
const op errors.Op = "website.NewSubmitInput"
if form == nil {
form = new(SubmissionForm)
}
identifier, _ := getIdentifier(r)
stats, err := storage.Submissions(r.Context()).SubmissionStats(identifier)
if err != nil {
return nil, errors.E(op, err)
}
return &SubmitInput{
Input: middleware.InputFromRequest(r),
Form: *form,
Stats: stats,
}, nil
}
func (SubmitInput) TemplateBundle() string {
return "submit"
}
// getIdentifier either returns the username of a logged in user, or the RemoteAddr of
// the request
func getIdentifier(r *http.Request) (string, bool) {
if user := middleware.UserFromContext(r.Context()); user != nil {
return user.Username, true
}
return r.RemoteAddr, false
}
func (s State) canSubmitSong(r *http.Request) (time.Duration, error) {
const op errors.Op = "website.canSubmitSong"
identifier, isUser := getIdentifier(r)
if isUser { // logged in users can always submit songs
return 0, nil
}
last, err := s.Storage.Submissions(r.Context()).LastSubmissionTime(identifier)
if err != nil {
return 0, errors.E(op, err, errors.InternalServer)
}
since := time.Since(last)
if since > time.Hour { // cooldown has passed so can submit song
return 0, nil
}
daypass := r.Header.Get(daypassHeader)
if s.Daypass.Equal(daypass, nil) { // daypass was used so can submit song
return 0, nil
}
return since, errors.E(op, errors.UserCooldown)
}
func (s State) GetSubmit(w http.ResponseWriter, r *http.Request) {
err := s.getSubmit(w, r, nil)
if err != nil {
hlog.FromRequest(r).Error().Err(err).Msg("")
return
}
}
func (s State) getSubmit(w http.ResponseWriter, r *http.Request, form *SubmissionForm) error {
const op errors.Op = "website.getSubmit"
input, err := NewSubmitInput(s.Storage, r, form)
if err != nil {
return errors.E(op, err)
}
return s.Templates.Execute(w, r, input)
}
func (s State) PostSubmit(w http.ResponseWriter, r *http.Request) {
// setup response function that differs between htmx/non-htmx request
responseFn := func(form SubmissionForm) {
var err error
if util.IsHTMX(r) {
err = s.Templates.Execute(w, r, form)
} else {
err = s.getSubmit(w, r, &form)
}
if err != nil {
s.errorHandler(w, r, err)
}
}
// parse and validate the form
form, err := s.postSubmit(w, r)
if err != nil {
// for unknown reason if we send a response without reading the body the connection is
// hard-reset instead and our response goes missing, so discard the body up to our
// allowed max size and then cut off if required
io.CopyN(io.Discard, r.Body, formMaxSize) // TODO: add a possible timeout
hlog.FromRequest(r).Error().Err(err).Msg("")
responseFn(form)
return
}
// success, update the submission time for the identifier
identifier, _ := getIdentifier(r)
if err = s.Storage.Submissions(r.Context()).UpdateSubmissionTime(identifier); err != nil {
hlog.FromRequest(r).Error().Err(err).Msg("")
responseFn(form)
return
}
// send a new empty form back to the user
back := SubmissionForm{Success: true}
if form.IsDaypass {
// if the submission was with a daypass, prefill the daypass for them again
back.Daypass = form.Daypass
back.IsDaypass = true
}
responseFn(back)
}
func (s State) postSubmit(w http.ResponseWriter, r *http.Request) (SubmissionForm, error) {
const op errors.Op = "website.PostSubmit"
// find out if the client is allowed to upload
cooldown, err := s.canSubmitSong(r)
if err != nil {
return SubmissionForm{
Errors: map[string]string{
"cooldown": strconv.FormatInt(int64(cooldown/time.Second), 10),
},
}, errors.E(op, err)
}
// start parsing the form, it's multipart encoded due to file upload and we manually
// handle some details due to reasons described in ParseForm
mr, err := r.MultipartReader()
if err != nil {
return SubmissionForm{}, errors.E(op, err, errors.InternalServer)
}
form, err := NewSubmissionForm(filepath.Join(s.Conf().MusicPath, "pending"), mr)
if err != nil {
return SubmissionForm{}, errors.E(op, err, errors.InternalServer)
}
// ParseForm just saved a temporary file that we want to delete if any other error
// occurs after this, so prep a defer for it that we can no-op later
var tmpFilename = form.File
defer func() {
if tmpFilename != "" {
os.Remove(tmpFilename)
}
}()
// Run a sanity check on the form input
if !form.Validate(s.Storage.Track(r.Context()), s.Daypass) {
return *form, errors.E(op, errors.InvalidForm)
}
// Probe the uploaded file for more information
song, err := PendingFromProbe(form.File)
if err != nil {
form.Errors["track"] = "File invalid; probably not an audio file."
return *form, errors.E(op, err, errors.InternalServer)
}
// Copy information over from the form and request to the PendingSong
song.Comment = form.Comment
song.Filename = form.OriginalFilename
song.UserIdentifier, _ = getIdentifier(r)
if form.Replacement != nil {
song.ReplacementID = *form.Replacement
}
song.SubmittedAt = time.Now()
form.Song = song
// Add the pending entry to the database
err = s.Storage.Submissions(r.Context()).InsertSubmission(*song)
if err != nil {
form.Errors["postprocessing"] = "Internal error, yell at someone in IRC"
return *form, errors.E(op, err, errors.InternalServer)
}
// clear the tmpFilename so that it doesn't get deleted after we return
tmpFilename = ""
return *form, nil
}
// readString reads a string from r no longer than maxSize
func readString(r io.Reader, maxSize int64) (string, error) {
r = io.LimitReader(r, maxSize)
if b, err := io.ReadAll(r); err != nil {
return "", err
} else {
return string(b), nil
}
}
// PendingFromProbe runs ffprobe on the given filename and constructs
// a PendingSong with the information found
func PendingFromProbe(filename string) (*radio.PendingSong, error) {
const op errors.Op = "website/api.PendingFromProbe"
info, err := audio.ProbeText(context.Background(), filename)
if err != nil {
return nil, errors.E(op, err)
}
s := radio.PendingSong{
Status: radio.SubmissionAwaitingReview,
FilePath: filename,
Artist: info.Artist,
Title: info.Title,
Album: info.Album,
SubmittedAt: time.Now(),
Format: info.FormatName,
Bitrate: info.Bitrate,
Length: info.Duration,
}
return &s, nil
}
// SubmissionForm is the form struct passed to the submit page templates as .Form
type SubmissionForm struct {
Token string // csrf token?
// Success indicates if the upload was a success
Success bool
// IsDaypass is true if Daypass was valid
IsDaypass bool
// Errors is populated when any errors were found with the uploaded form
// this is populated with their form field names as indicated below in addition to
// the following possible keys:
// "postprocessing": something failed after successful form parsing but before we saved all the data
// "cooldown": indicates the user was not permitted to upload yet, they need to wait longer
Errors map[string]string
// form fields
OriginalFilename string // name="track" The filename of the uploaded file
Daypass string // name="daypass"
Comment string // name="comment"
Replacement *radio.TrackID // name="replacement"
// after processing fields
// File is the on-disk filename for the uploaded file
File string
// Song we managed to populate by analyzing the uploaded file
Song *radio.PendingSong
}
func (SubmissionForm) TemplateBundle() string {
return "submit"
}
func (SubmissionForm) TemplateName() string {
return "form_submit"
}
// ParseForm parses a multipart form into the SubmissionForm
//
// Go has standard library support to do this simpler, but it doesn't let you set limits on individual fields,
// so we're parsing each field ourselves so we can limit their length.
//
// Fields supported:
//
// "track": audio file being submitted
// "comment": comment to be shown on the pending admin panel
// "daypass": daypass to bypass upload limits
// "replacement": an TrackID (number) indicating what song to replace in the database with this
//
// Any other fields cause an error to be returned and all form parsing to stop.
func NewSubmissionForm(tempdir string, mr *multipart.Reader) (*SubmissionForm, error) {
const op errors.Op = "public.NewSubmissionForm"
var sf SubmissionForm
for i := 0; i < formMaxMIMEParts; i++ {
part, err := mr.NextPart()
if errors.IsE(err, io.EOF) {
// finished reading parts
return &sf, nil
}
if err != nil {
return nil, errors.E(op, err, errors.InternalServer)
}
switch part.FormName() {
case "track": // audio file that is being submitted
err = func() error {
// clean the extension from the user
path := filepath.Clean("/" + part.FileName())
ext := filepath.Ext(path)
if !AllowedExtension(ext) {
return errors.E(op, errors.InvalidForm, "extension not allowed", errors.Info(ext))
}
// remove any * because CreateTemp uses them for the random replacement
ext = strings.ReplaceAll(ext, "*", "")
f, err := os.CreateTemp(tempdir, "pending-*"+ext)
if err != nil {
return errors.E(op, err, errors.Info("failed to create temp file"))
}
defer f.Close()
n, err := io.CopyN(f, part, formMaxFileLength)
if err != nil && !errors.IsE(err, io.EOF) {
os.Remove(f.Name())
return errors.E(op, err, errors.Info("copying to temp file failed"))
}
if n >= formMaxFileLength {
os.Remove(f.Name())
return errors.E(op, err)
}
sf.OriginalFilename = part.FileName()
sf.File = f.Name()
return nil
}()
if err != nil {
return nil, errors.E(op, err)
}
case "comment": // comment to be shown on the pending admin panel
s, err := readString(part, formMaxCommentLength)
if err != nil {
return nil, errors.E(op, err)
}
sf.Comment = s
case "daypass": // a daypass
s, err := readString(part, formMaxDaypassLength)
if err != nil {
return nil, errors.E(op, err)
}
sf.Daypass = s
case "replacement": // replacement track identifier
s, err := readString(part, formMaxReplacementLength)
if err != nil {
return nil, errors.E(op, err)
}
tid, err := radio.ParseTrackID(s)
if err != nil {
return nil, errors.E(op, err)
}
sf.Replacement = &tid
default:
// unknown form field, we just cancel everything and return
return nil, errors.E(op, errors.InvalidForm)
}
}
return &sf, nil
}
// Validate checks if required fields are filled in the SubmissionForm and
// if a daypass was supplied if it was a valid one. Populates sf.Errors with
// any errors that occur and what input field caused it.
func (sf *SubmissionForm) Validate(ts radio.TrackStorage, dp secret.Secret) bool {
sf.Errors = make(map[string]string)
if sf.File == "" {
sf.Errors["track"] = "no temporary file"
}
if sf.OriginalFilename == "" {
sf.Errors["track"] = "no file selected"
}
if sf.Comment == "" {
sf.Errors["comment"] = "no comment supplied"
}
if sf.Daypass != "" {
sf.IsDaypass = dp.Equal(sf.Daypass, nil)
if !sf.IsDaypass {
sf.Errors["daypass"] = "daypass invalid"
}
}
if sf.Replacement != nil && *sf.Replacement != 0 {
song, err := ts.Get(*sf.Replacement)
if err != nil {
sf.Errors["replacement"] = "TrackID does not exist"
}
if !song.NeedReplacement {
sf.Errors["replacement"] = "TrackID does not need replacement"
}
}
return len(sf.Errors) == 0
}
// AllowedExtension returns if ext is an allowed extension for the uploaded audio files
func AllowedExtension(ext string) bool {
ext = strings.ToLower(ext)
switch ext {
case ".mp3":
return true
case ".flac":
return true
case ".ogg":
return true
}
return false
}