-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (65 loc) · 1.95 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
package main
import (
"encoding/json"
"image"
_ "image/jpeg"
_ "image/png"
"io"
"net/http"
"os"
"path/filepath"
)
type ImageInfo struct {
Width int
Height int
}
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
imageUrl := r.URL.Query().Get("url")
response, err := http.Get(imageUrl)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer response.Body.Close()
// Create a temporary file to store the image
tempFile, err := os.Create(filepath.Base(imageUrl))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer tempFile.Close()
// Copy the image data to the temporary file
_, err = io.Copy(tempFile, response.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Open the temporary file and get the image dimensions
file, err := os.Open(tempFile.Name())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer file.Close()
image, _, err := image.DecodeConfig(file)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Create the JSON response with the image dimensions
imageInfo := ImageInfo{
Width: image.Width,
Height: image.Height,
}
jsonResponse, err := json.Marshal(imageInfo)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Write the JSON response to the response writer
w.Header().Set("Content-Type", "application/json")
w.Write(jsonResponse)
})
http.ListenAndServe(":8080", nil)
}