Skip to content

Commit 4606614

Browse files
committed
Refactored code, added IsBase64Encoded
1 parent 76dfaca commit 4606614

File tree

11 files changed

+111
-118
lines changed

11 files changed

+111
-118
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
main
2+
resize
3+
*.zip
4+
nohup.out
5+
.vim
6+
.vscode
7+
*.png
File renamed without changes.

bindata.go

Lines changed: 12 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
GOOS=linux
4+
GOARCH=amd64
5+
CGO_ENABLED=0
6+
7+
go build -o main
8+
[ $? = '1' ] && echo "Error building module" && exit 1
9+
zip dist.zip main
10+
rm main
11+
[ $? = '1' ] && echo "Error zipping" && exit 1
12+
13+
echo "Build finished"

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ go 1.16
44

55
require (
66
github.com/andybalholm/brotli v1.0.3 // indirect
7-
github.com/aws/aws-lambda-go v1.26.0 // indirect
8-
github.com/disintegration/imaging v1.6.2 // indirect
7+
github.com/aws/aws-lambda-go v1.26.0
8+
github.com/disintegration/imaging v1.6.2
99
github.com/go-bindata/go-bindata v3.1.2+incompatible // indirect
1010
github.com/klauspost/compress v1.13.5 // indirect
1111
github.com/valyala/fasthttp v1.29.0 // indirect

lambda.go

Lines changed: 10 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,21 @@
11
package main
22

33
import (
4-
"bytes"
54
"context"
6-
"crypto/tls"
7-
"encoding/base64"
85
"fmt"
6+
97
"github.com/aws/aws-lambda-go/events"
10-
"github.com/aws/aws-lambda-go/lambda"
11-
"github.com/disintegration/imaging"
12-
"image"
13-
"image/color"
14-
"net/http"
15-
"time"
168
)
179

18-
const width = 1200
19-
const height = 630
20-
const minHeight = 250
21-
22-
func convertImage(url string) image.Image {
23-
fmt.Printf("====> Should convert url: %s.\n", url)
24-
asset, err := Asset("logo.png")
25-
if err != nil {
26-
return imaging.New(1, 1, color.Black)
27-
}
28-
background, _ := imaging.Decode(bytes.NewReader(asset))
29-
30-
fmt.Printf("Started request.\n")
31-
tr := &http.Transport{}
32-
tr.TLSClientConfig = &tls.Config{
33-
NextProtos: []string{"h1"},
34-
}
35-
36-
client := &http.Client{
37-
Timeout: 5 * time.Second,
38-
Transport: tr,
39-
}
40-
imageResp, err := client.Get(url)
41-
if err != nil {
42-
panic(err)
43-
}
44-
fmt.Println(imageResp)
45-
//imageResp, _ := http.Get(url)
46-
//defer imageResp.Body.Close()
47-
48-
fmt.Printf("Finished request.\n")
49-
image, _ := imaging.Decode(imageResp.Body)
50-
image_h := image.Bounds().Max.Y
51-
52-
if image_h < minHeight {
53-
image = imaging.Resize(image, 0, minHeight, imaging.Lanczos)
54-
image_h = image.Bounds().Max.Y
55-
}
56-
57-
output := imaging.Resize(background, 0, image_h, imaging.Lanczos)
58-
output = imaging.PasteCenter(output, image)
59-
fmt.Printf("====> Returning request.\n")
60-
return image
61-
}
62-
63-
func encodeImage(image image.Image) string {
64-
var buf bytes.Buffer
65-
fmt.Printf("====> Start encoding.\n")
66-
b64encoder := base64.NewEncoder(base64.StdEncoding, &buf)
67-
imaging.Encode(b64encoder, image, imaging.JPEG)
68-
b64encoder.Close()
69-
encoded := buf.String()
70-
fmt.Printf("====> Ended encoding.\n")
71-
return encoded
72-
}
73-
7410
func HandleRequest(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
7511
fmt.Printf("Processing request data for request %s.\n", request.RequestContext.RequestID)
76-
body := encodeImage(convertImage(request.QueryStringParameters["image"]))
77-
headers := map[string]string{"content-type": "image/png"}
78-
79-
return events.APIGatewayProxyResponse{Body: body, StatusCode: 200, Headers: headers}, nil
80-
}
81-
82-
func main() {
83-
lambda.Start(HandleRequest)
12+
body := EncodeImage(ConvertImage(request.QueryStringParameters["image"]))
13+
headers := map[string]string{"Content-Type": "image/png"}
14+
15+
return events.APIGatewayProxyResponse{
16+
Body: body,
17+
StatusCode: 200,
18+
Headers: headers,
19+
IsBase64Encoded: true,
20+
}, nil
8421
}

main

-8.75 MB
Binary file not shown.

main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
import (
4+
"github.com/aws/aws-lambda-go/lambda"
5+
)
6+
7+
func main() {
8+
lambda.Start(HandleRequest)
9+
}

main.zip

-4.58 MB
Binary file not shown.

other/resise_file.go

Lines changed: 0 additions & 33 deletions
This file was deleted.

resize.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"encoding/base64"
6+
"fmt"
7+
"image"
8+
"image/color"
9+
"net/http"
10+
11+
"github.com/disintegration/imaging"
12+
)
13+
14+
const width = 1200
15+
const height = 630
16+
const minHeight = 250
17+
18+
func ConvertImage(url string) image.Image {
19+
fmt.Printf("====> Should convert url: %s.\n", url)
20+
asset, err := Asset("assets/logo.png")
21+
if err != nil {
22+
fmt.Println("Can't load asset, falling back to black square")
23+
return imaging.New(width, height, color.Black)
24+
}
25+
background, _ := imaging.Decode(bytes.NewReader(asset))
26+
27+
fmt.Printf("Started request.\n")
28+
imageResp, err := http.Get(url)
29+
if err != nil {
30+
panic(err)
31+
}
32+
defer imageResp.Body.Close()
33+
34+
fmt.Printf("Finished request.\n")
35+
image, _ := imaging.Decode(imageResp.Body)
36+
image_h := image.Bounds().Max.Y
37+
38+
if image_h < minHeight {
39+
image = imaging.Resize(image, 0, minHeight, imaging.Lanczos)
40+
image_h = image.Bounds().Max.Y
41+
}
42+
43+
output := imaging.Resize(background, 0, image_h, imaging.Lanczos)
44+
output = imaging.PasteCenter(output, image)
45+
fmt.Printf("====> Returning image %s.\n", output.Bounds().Max)
46+
return output
47+
}
48+
49+
func EncodeImage(image image.Image) string {
50+
var buf bytes.Buffer
51+
fmt.Printf("====> Start encoding.\n")
52+
b64encoder := base64.NewEncoder(base64.StdEncoding, &buf)
53+
imaging.Encode(b64encoder, image, imaging.JPEG)
54+
b64encoder.Close()
55+
encoded := buf.String()
56+
fmt.Printf("====> Ended encoding.\n")
57+
return encoded
58+
}

0 commit comments

Comments
 (0)