-
Notifications
You must be signed in to change notification settings - Fork 1
/
utterance.go
124 lines (105 loc) · 2.89 KB
/
utterance.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
package main
import (
"crypto/rand"
"fmt"
"html/template"
"math/big"
"net/http"
"os"
"path/filepath"
"strconv"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/joho/godotenv"
)
// Parse the upload form template
var form = template.Must(template.New("form").ParseFiles("upload.html"))
// Remove ambiguous characters
var defaultChars = []byte(`abcdefghijkmnpqrstuvwxyz0123456789`)
// RandomString returns random characters from either the given
// variadic bytes, or the default characters
func RandomString(desired int, chars ...byte) string {
if len(chars) == 0 {
chars = defaultChars
}
possibilities := big.NewInt(int64(len(chars)))
out := make([]byte, desired)
for i, _ := range out {
var index int64
for {
r, err := rand.Int(rand.Reader, possibilities)
if err == nil {
index = r.Int64()
break
}
}
out[i] = chars[index]
}
return string(out)
}
// server extends the s3 service
type server struct {
*s3.S3
bucket string
}
func (srv server) uploadHandler(w http.ResponseWriter, r *http.Request) {
// Display the upload form for anything no
if r.Method != http.MethodPost {
if err := form.ExecuteTemplate(w, "upload.html", nil); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}
// the FormFile function takes in the POST input id file
file, header, err := r.FormFile("file")
if err != nil {
http.Error(w, "form file:"+err.Error(), http.StatusInternalServerError)
return
}
defer file.Close()
// If the filename has a ending, add that to the name
name := RandomString(32) + filepath.Ext(header.Filename)
out, err := srv.PutObject(&s3.PutObjectInput{
Bucket: aws.String(srv.bucket),
Key: aws.String(name),
ContentType: aws.String(header.Header.Get("Content-Type")),
Body: file,
})
if err != nil {
http.Error(w, "put:"+err.Error(), http.StatusInternalServerError)
return
}
// TODO anyway to build a bucket URL?
fmt.Fprintf(w, "name: %s\n", name)
fmt.Fprintf(w, out.String())
}
// NewServer creates a new server
func NewServer() (srv server) {
// By default, the ENV credentials provider will look for
// AWS_ACCESS_KEY(_ID)? and AWS_SECRET(_ACCESS)?_KEY
credentials := session.New()
// TODO standard ENV for AWS region? default region?
region := os.Getenv("AWS_REGION")
if region == "" {
region = "us-west-2"
}
srv.S3 = s3.New(credentials, aws.NewConfig().WithRegion(region))
srv.bucket = os.Getenv("S3_BUCKET")
// TODO error if the bucket was not set
return
}
func main() {
if err := godotenv.Load(); err != nil {
fmt.Printf("WARNING: %s\n", err)
}
port, _ := strconv.Atoi(os.Getenv("PORT"))
if port == 0 {
port = 8080
}
host := os.Getenv("HOST")
fmt.Println("Booting server")
server := NewServer()
http.HandleFunc("/", server.uploadHandler)
http.ListenAndServe(fmt.Sprintf("%s:%d", host, port), nil)
}