/
main.go
182 lines (152 loc) · 3.73 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"github.com/julienschmidt/httprouter"
"github.com/noahgoldman/dotaprofiles/config"
"github.com/noahgoldman/dotaprofiles/upload"
"html/template"
"io"
"log"
"net/http"
"strconv"
)
var Config *config.Configuration
func main() {
log.Print("starting")
Config = config.LoadConfig(config.GetDefaultFile())
InitDB()
upload.AWSInit(Config.AWS_AccessKey, Config.AWS_SecretKey)
router := httprouter.New()
router.GET("/", Index)
router.GET("/static/:file", StaticFiles)
router.POST("/make_images", MakeImageHandler)
router.POST("/upload", Upload)
log.Printf("Started webserver on port %s", Config.HttpPort)
http.ListenAndServe(Config.HttpPort, router)
}
func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
t, err := template.ParseFiles("templates/index.html")
if err != nil {
panic(err)
}
t.Execute(w, nil)
}
func StaticFiles(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
http.ServeFile(w, r, "static/"+params.ByName("file"))
}
func MakeImageHandler(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
r.ParseForm()
id, err := strconv.Atoi(r.FormValue("id"))
if err != nil {
sendInternalError(w, err)
return
}
ps, err := getPictureSet(id)
if err != nil {
log.Print(err)
http.NotFound(w, r)
return
}
rect, err := GetRect(r)
if err != nil {
sendInternalError(w, err)
return
}
file, err := GetImageFile(ps.original, upload.Download_S3)
if err != nil {
sendInternalError(w, err)
return
}
images, err := MakeImages(rect, file)
if err != nil {
sendInternalError(w, err)
return
}
err = ps.AddSet()
if err != nil {
sendInternalError(w, err)
return
}
urls := make([]string, 5, 5)
for i := 0; i < len(images); i++ {
err = upload.Upload_S3(images[i], ps.set[i])
if err != nil {
log.Print(err)
}
urls[i] = upload.GetURL(ps.set[i])
}
output, err := json.Marshal(urls)
if err != nil {
sendInternalError(w, err)
return
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, string(output))
}
func Upload(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
err := r.ParseMultipartForm(100000000)
if err != nil {
panic(err)
}
m := r.MultipartForm
files := m.File["picture"]
if len(files) != 1 {
log.Printf("there are %d files", len(files))
sendHTTPError(w, "Only 1 file should ever be uploaded")
return
}
filename := files[0].Filename
file, err := files[0].Open()
defer file.Close()
if err != nil {
err_string := fmt.Sprintf("Failed to open uploaded file %s", files[0].Filename)
sendHTTPError(w, err_string)
return
}
ps, err := newPictureSet(filename)
if err != nil {
sendInternalError(w, err)
return
}
if ps == nil {
fmt.Printf("wow err")
}
fmt.Printf("%s", ps.original)
// Create the local destination file
dest, err := CreateImageFile(ps.original)
if err != nil {
sendInternalError(w, err)
return
}
defer dest.Close()
// Copy the file to the local image store directory
_, err = io.Copy(dest, file)
if err != nil {
sendInternalError(w, err)
return
}
// Seek back to the start of the file so that we can read it again
_, err = file.Seek(0, 0)
if err != nil {
sendInternalError(w, err)
return
}
// Upload the file to Amazon S3
err = upload.Upload_S3(file, ps.original)
if err != nil {
sendInternalError(w, err)
return
}
// Make the JSON to return
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, JSONResponse{"id": ps.id, "url": upload.GetURL(ps.original)})
}
func sendHTTPError(w http.ResponseWriter, err_string string) {
log.Print(err_string)
http.Error(w, err_string, 500)
}
func sendInternalError(w http.ResponseWriter, err error) {
log.Print(err)
http.Error(w, "Internal server error", 500)
}