-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.go
77 lines (65 loc) · 1.69 KB
/
test.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
package main
import (
"bytes"
_ "embed"
"errors"
"fmt"
"image"
"io"
"net/http"
"strconv"
spinhttp "github.com/fermyon/spin/sdk/go/http"
"github.com/sunshineplan/imgconv"
)
func init() {
spinhttp.Handle(func(w http.ResponseWriter, r *http.Request) {
req, err := http.NewRequest("HEAD", r.URL.RequestURI()[1:], nil)
if err != nil {
errorResponse(w, errors.New("Invalid URL"))
return
}
res, err := spinhttp.Send(req)
if err != nil {
errorResponse(w, errors.New("Invalid URL"))
return
}
//Print Content-Type to Response
contentType := res.Header.Get("Content-Type")
fmt.Fprintln(w, contentType)
//TODO return wrong content type
//Print Content-Length
contentLength := res.Header.Get("Content-Length")
i, _ := strconv.Atoi(contentLength)
if i == 0 {
errorResponse(w, errors.New("No Content"))
return
}
fmt.Fprintln(w, i)
//Make a new request to get the content
req, err = http.NewRequest("GET", r.URL.RequestURI()[1:], nil)
if err != nil {
errorResponse(w, errors.New("Something went wrong"))
return
}
res, err = spinhttp.Send(req)
if err != nil {
errorResponse(w, errors.New("Something went wrong"))
return
}
//Convert the content to image.Image
img, _, err := image.Decode(res.Body)
if err != nil {
errorResponse(w, errors.New("Something went wrong"))
return
}
// var imgRes io.Writer
var imgBuffer bytes.Buffer
imgRes := io.MultiWriter(&imgBuffer)
// imgRes := new(bytes.Buffer)
opts := imgconv.Options{Format: &imgconv.FormatOption{Format: imgconv.PNG}}
opts.Convert(imgRes, img)
w.Header().Set("Content-Type", "image/png")
imgReader := bytes.NewReader(imgBuffer.Bytes())
io.Copy(w, imgReader)
})
}