forked from bertbaron/intravatar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.go
209 lines (184 loc) · 5.56 KB
/
upload.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
package main
import (
"bytes"
"crypto/rand"
"crypto/tls"
"encoding/hex"
"errors"
"fmt"
"gopkg.in/gomail.v1"
"io"
"io/ioutil"
"log"
"net/http"
"net/smtp"
"os"
"strings"
"time"
)
func validateAndResize(file io.Reader) (*Avatar, error) {
avatar, err := strictReadImage(file)
if err != nil {
return nil, err
}
err = cropAndScale(avatar)
if err != nil {
return nil, err
}
return avatar, nil
}
func sendMessage(msg *gomail.Message) error {
config := tls.Config{}
if *noTls {
config.InsecureSkipVerify = true
}
var auth smtp.Auth
if *smtpUser != "" {
auth = gomail.LoginAuth(*smtpUser, *smtpPassword, *smtpHost)
config.ServerName = *smtpHost
}
address := fmt.Sprintf("%v:%v", *smtpHost, *smtpPort)
mailer := gomail.NewCustomMailer(address, auth, gomail.SetTLSConfig(&config))
if err := mailer.Send(msg); err != nil {
log.Printf("Error sending configuration email: %v", err)
return errors.New("Failed to send confirmation email")
}
return nil
}
// Sends a test email so that it can be verified that email is working correctly.
func sendTestMail(email string) error {
// There is still too much duplicate code
log.Printf("Sending test email to %s to verify that email is configured correctly", email)
from := *sender
to := email
title := "Intravatar is up and running"
body := "If you receive this message, intravatar is up and running and able to send confirmation emails"
msg := gomail.NewMessage()
msg.SetHeader("From", from)
msg.SetHeader("To", to)
msg.SetHeader("Subject", title)
msg.SetBody("text/plain", body)
return sendMessage(msg)
}
func sendConfirmationEmail(email string, token string) error {
log.Printf("Sending confiration email to %v with confirmation token %v", email, token)
from := *sender
to := email
title := "Please confirm your avatar upload"
url := getServiceUrl() + "confirm/" + token
link := fmt.Sprintf("<a href=\"%s\">%s</a>", url, url)
body := "Thank you for uploading your avatar. You can confirm your upload by clicking this link: " + link
msg := gomail.NewMessage()
msg.SetHeader("From", from)
msg.SetHeader("To", to)
msg.SetHeader("Subject", title)
msg.SetBody("text/html", body)
if err := sendMessage(msg); err != nil {
log.Printf("Error sending configuration email: %v", err)
return errors.New("Failed to send confirmation email")
}
return nil
}
func createToken() (string, error) {
b := make([]byte, 16)
_, err := rand.Read(b)
if err != nil {
return "", err
}
return hex.EncodeToString(b), nil
}
func renderSaveError(w http.ResponseWriter, message string, err error) {
log.Printf("Error: %v (%v)", message, err)
errMsg := fmt.Sprintf("%v", err)
renderTemplate(w, "saveError", map[string]string{"Message": message, "Error": errMsg})
}
func getConfirmationFile(token string) (_ string, hash string, err error) {
files, err := ioutil.ReadDir(getUnconfirmedDir())
if err != nil {
log.Fatal(err)
}
for _, file := range files {
filename := file.Name()
if strings.HasPrefix(filename, token) {
splitted := strings.Split(filename, "-")
if len(splitted) < 2 {
log.Printf("Invalid confirmation file name: %v", filename)
return "", "", errors.New("Internal error")
}
hash = splitted[1] // FIXME perform range check!
return getUnconfirmedDir() + "/" + filename, hash, nil
}
}
return "", "", errors.New("Confirmation period expired")
}
func confirm(w http.ResponseWriter, r *http.Request, token string) {
log.Printf("Confirming uploaded avatar with token %v", token)
filepath, hash, err := getConfirmationFile(token)
log.Printf("Found confirmation file %v (hash=%v)", filepath, hash)
if err != nil {
renderSaveError(w, "Error confirming upload", err)
return
}
err = os.Rename(filepath, createAvatarPath(hash))
if err != nil {
renderSaveError(w, "Error confirming upload", err)
return
}
// cache breaker to force website to reload the avatar
ns := time.Now().UnixNano()
uniq := fmt.Sprintf("%d", ns)
// thank you for uploading your avatar...
renderTemplate(w, "confirm", map[string]string{"Avatar": fmt.Sprintf("/avatar/%s", hash), "Uniq": uniq})
}
func uploadHandler(w http.ResponseWriter, r *http.Request, title string) {
renderTemplate(w, "upload", map[string]string{})
}
func confirmHandler(w http.ResponseWriter, r *http.Request, token string) {
confirm(w, r, token)
}
func writeToFile(filename string, avatar *Avatar) error {
f, err := os.Create(filename)
if err != nil { return err }
defer f.Close()
b := bytes.NewBuffer(avatar.data)
_, err = io.Copy(f, b)
return err
}
func saveHandler(w http.ResponseWriter, r *http.Request, ignored string) {
email := r.FormValue("email")
log.Printf("Saving image for email address: %v", email)
file, _, err := r.FormFile("image")
if err != nil {
renderSaveError(w, "Please chooce a file to upload", err)
return
}
avatar, err := validateAndResize(file)
if err != nil {
renderSaveError(w, "Failed to read image file. Note that only jpeg, png and gif images are supported", err)
return
}
token, err := createToken()
if err != nil {
renderSaveError(w, "Failed to generate random token", err)
return
}
hash := createHash(email)
filename := createUnconfirmedAvatarPath(hash, token)
err = writeToFile(filename, avatar)
if err != nil {
renderSaveError(w, "Error while creating file", err)
return
}
if *smtpHost == "" {
// skip e-mail confirmation
confirm(w, r, token)
return
}
err = sendConfirmationEmail(email, token)
if err != nil {
renderSaveError(w, "Failed to send confirmation email", err)
return
}
// a confirmation email has ben send...
renderTemplate(w, "save", map[string]string{"Email": email})
}