-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
93 lines (76 loc) · 1.92 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
package main
import (
"bytes"
"fmt"
"github.com/buckket/go-blurhash"
"github.com/qiangxue/fasthttp-routing"
"github.com/valyala/fasthttp"
"image"
_ "image/jpeg"
_ "image/png"
"strconv"
"encoding/json"
)
type BlurHashResponse struct {
Message string `json:"blur_hash"`
}
func convertToInt(str string) (int, error) {
num, err := strconv.Atoi(str)
if err != nil {
return 0, fmt.Errorf("error converting string to integer: %v", err)
}
return num, nil
}
func encode(c *routing.Context) error {
imageURL := string(c.QueryArgs().Peek("image_url"))
x := 9
y := 9
xConverted, err := convertToInt(string(c.QueryArgs().Peek("x")))
if err != nil {
fmt.Println("Error converting string to integer:", err)
} else {
x = xConverted
}
yConverted, err := convertToInt(string(c.QueryArgs().Peek("y")))
if err != nil {
fmt.Println("Error converting string to integer:", err)
} else {
y = yConverted
}
fmt.Println("Image URL:", imageURL)
statusCode, body, err := fasthttp.Get(nil, imageURL)
if err != nil {
fmt.Println("Error downloading image:", err)
return err
}
if statusCode != fasthttp.StatusOK {
fmt.Println("Error downloading image, status code:", statusCode)
return fmt.Errorf("error downloading image, status code: %d", statusCode)
}
img, _, err := image.Decode(bytes.NewReader(body))
if err != nil {
fmt.Println("Error decoding image:", err)
return err
}
blurHash, err := blurhash.Encode(x, y, img)
if err != nil {
fmt.Println("Error generating BlurHash:", err)
return err
}
jsonResponse := BlurHashResponse{
Message: blurHash,
}
jsonBytes, err := json.Marshal(jsonResponse)
if err != nil {
fmt.Println("Error marshaling JSON:", err)
return err
}
c.Response.Header.SetContentType("application/json")
c.Response.SetBodyRaw(jsonBytes)
return nil
}
func main() {
router := routing.New()
router.Get("/", encode)
fasthttp.ListenAndServe(":3333", router.HandleRequest)
}