-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsave.go
164 lines (145 loc) · 4.16 KB
/
save.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
package main
import (
"context"
"crypto/md5"
"encoding/hex"
"encoding/json"
"image"
"image/jpeg"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"time"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/google/go-github/github"
"github.com/nfnt/resize"
"golang.org/x/oauth2"
)
// UserPayload is the shape of the inbound JSON in the request body
type UserPayload struct {
Event string `json:"event"`
InstanceID string `json:"instance_id"`
User struct {
ID string `json:"id"`
Aud string `json:"aud"`
Role string `json:"role"`
Email string `json:"email"`
AppMetadata struct {
Provider string `json:"provider"`
} `json:"app_metadata"`
UserMetadata struct {
FullName string `json:"full_name"`
} `json:"user_metadata"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
} `json:"user"`
}
func main() {
lambda.Start(handler)
}
func downloadDecode(filepath string, url string) (image.Image, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return jpeg.Decode(resp.Body)
}
func createResizedFiles(img image.Image, size int) (error, string) {
m := resize.Resize(uint(size), 0, img, resize.Lanczos3)
stringSize := strconv.Itoa(size)
fileName := "profilePicture_" + stringSize + ".jpeg"
filePath := "/tmp/" + fileName
out, err := os.Create(filePath)
if err != nil {
return err, ""
}
defer out.Close()
err = jpeg.Encode(out, m, nil)
if err != nil {
return err, ""
}
return nil, fileName
}
func createGHClient() (*github.Client, error) {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
return client, nil
}
func uploadFiles(client *github.Client, files []string, id string) error {
for _, file := range files {
path := "profile_pictures/" + id + "/" + file
fileToUpload, err := ioutil.ReadFile("/tmp/" + file)
if err != nil {
return err
}
opts := &github.RepositoryContentFileOptions{
Message: github.String("Uploading profile pictures"),
Content: fileToUpload,
Branch: github.String("master"),
Committer: &github.CommitAuthor{Name: github.String(os.Getenv("GITHUB_COMMITTER_NAME")), Email: github.String(os.Getenv("GITHUB_COMMITTER_EMAIL"))},
}
if _, _, err := client.Repositories.CreateFile(context.Background(), os.Getenv("GITHUB_OWNER"), os.Getenv("GITHUB_REPO_NAME"), path, opts); err != nil {
return err
}
}
return nil
}
func handler(ctx context.Context, request events.APIGatewayProxyRequest) (*events.APIGatewayProxyResponse, error) {
var eventBody UserPayload
var err error
if err = json.Unmarshal([]byte(request.Body), &eventBody); err != nil {
return &events.APIGatewayProxyResponse{
StatusCode: 500,
Headers: map[string]string{"Content-Type": "text/plain"},
Body: "Unable to unmarshal inbound payload",
IsBase64Encoded: false,
}, nil
}
log.Print(eventBody)
email := eventBody.User.Email
hashedEmail := md5.Sum([]byte(email))
stringHashedEmail := hex.EncodeToString(hashedEmail[:])
var files []string
gravatarURL := "https://www.gravatar.com/avatar/" + stringHashedEmail + "?s=400"
log.Print(gravatarURL)
img, err := downloadDecode("/tmp/profilePicture.jpeg", gravatarURL)
if err != nil {
log.Print(err, "unable to download file")
}
if err, file := createResizedFiles(img, 80); err != nil {
log.Print(err)
} else {
files = append(files, file)
}
if err, file := createResizedFiles(img, 200); err != nil {
log.Print(err)
} else {
files = append(files, file)
}
if err, file := createResizedFiles(img, 400); err != nil {
log.Print(err)
} else {
files = append(files, file)
}
var client *github.Client
if client, err = createGHClient(); err != nil {
log.Print(err)
}
if err := uploadFiles(client, files, eventBody.User.ID); err != nil {
log.Print(err)
}
return &events.APIGatewayProxyResponse{
StatusCode: 200,
Headers: map[string]string{"Content-Type": "text/plain"},
Body: "Files uploaded!",
IsBase64Encoded: false,
}, nil
}