-
Notifications
You must be signed in to change notification settings - Fork 0
/
add.go
125 lines (104 loc) · 3.05 KB
/
add.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
package main
import (
"context"
"fmt"
"html/template"
"net/http"
"os"
"strconv"
"time"
"github.com/apex/log"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
func (s *server) add() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
log.Info("showing upload form")
t, err := template.ParseFS(tmpl, "templates/*.html")
if err != nil {
log.WithError(err).Fatal("Failed to parse templates")
}
w.Header().Set("Content-Type", "text/html")
err = t.ExecuteTemplate(w, "add.html", struct {
Header http.Header
}{
Header: r.Header,
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.WithError(err).Fatal("Failed to execute templates")
}
return
}
log.Info("processing upload post")
// parse body to a record
var rec Record
// limit upload size to 1MB
r.Body = http.MaxBytesReader(w, r.Body, 1024*1024)
err := r.ParseForm()
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
audioFile, header, err := r.FormFile("audio")
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
defer audioFile.Close()
// parse title from form
rec.Title = r.Form.Get("title")
// parse longitude and latitude from form
rec.Longitude, err = strconv.ParseFloat(r.Form.Get("longitude"), 64)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
rec.Latitude, err = strconv.ParseFloat(r.Form.Get("latitude"), 64)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
rec.ID = r.RemoteAddr
rec.Created = time.Now()
// Data retention is one day
rec.Expires = rec.Created.Add(time.Hour * 24)
// https://aws.github.io/aws-sdk-go-v2/docs/sdk-utilities/s3/
// Upload the audio file to S3 client s.store and get the url
audioKey := fmt.Sprintf("%s/%s/%s", rec.ID, rec.Created.Format("2006-01-02"), header.Filename)
putResult, err := s.store.PutObject(context.TODO(), &s3.PutObjectInput{
Bucket: aws.String(os.Getenv("BUCKET_NAME")),
Key: aws.String(audioKey),
Body: audioFile,
// TODO: Figure this out for Android
ContentType: aws.String("audio/x-m4a"),
})
if err != nil {
log.WithError(err).Error("Failed to upload audio file")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rec.Audio = audioKey
log.WithFields(log.Fields{
"record": rec,
"putResult": putResult,
}).Info("uploaded audio file")
av, err := attributevalue.MarshalMap(rec)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_, err = s.db.PutItem(context.TODO(), &dynamodb.PutItemInput{
Item: av,
TableName: aws.String(os.Getenv("TABLE_NAME")),
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/", http.StatusFound)
}
}