-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathapp.go
102 lines (88 loc) · 2.02 KB
/
app.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
package main
import (
"bytes"
"image"
_ "image/gif"
_ "image/jpeg"
"io"
"net/http"
// _ "image/png"
"fmt"
"image/png"
"log"
"os"
"strings"
"github.com/nfnt/resize"
)
// https://www.smashingmagazine.com/2018/01/drag-drop-file-uploader-vanilla-js/
func MakeThumbnail(inBuf []byte) ([]byte, error) {
inPicture, _, err := image.Decode(bytes.NewReader(inBuf))
if err != nil {
return nil, fmt.Errorf("Error decoding picture: %s", err)
}
buf := &bytes.Buffer{}
// resize to width using Lanczos resampling
// and preserve aspect ratio
thumb := resize.Resize(50, 50, inPicture, resize.Lanczos3)
err = png.Encode(buf, thumb)
if err != nil {
return nil, fmt.Errorf("Error shrinking picture: %s", err)
}
return buf.Bytes(), nil
}
func HandleHTTP(w http.ResponseWriter, r *http.Request) {
for strings.HasPrefix(r.URL.Path, "//") {
r.URL.Path = r.URL.Path[1:]
}
log.Printf("Got: %s", r.URL.Path)
if r.URL.Path == "/" {
page, err := os.ReadFile("page.html")
if err != nil {
http.Error(w, "Error reading page:"+err.Error(), 503)
return
}
w.Write(page)
return
}
if r.URL.Path == "/thumbnail" {
body, err := io.ReadAll(r.Body)
if err != nil {
log.Printf("Error reading body:" + err.Error())
http.Error(w, "Error reading body:"+err.Error(), 503)
return
}
thumb, err := MakeThumbnail(body)
if err != nil {
log.Printf(err.Error())
http.Error(w, err.Error(), 503)
return
}
w.Header().Add("Content-Type", "image/png")
w.Write(thumb)
return
}
path := r.URL.Path
if path[0] == '/' {
path = path[1:]
}
if strings.Index(path, "..") >= 0 {
http.Error(w, "Bad path: "+path, 404)
return
}
buf, err := os.ReadFile(path)
if err != nil {
http.Error(w, "Error reading file:"+err.Error(), 404)
return
}
w.Write(buf)
}
func main() {
if jobIndex := os.Getenv("JOB_INDEX"); jobIndex != "" {
// Do batch job
} else {
// Debug the http handler for all requests
http.HandleFunc("/", HandleHTTP)
log.Println("Listening on port 8080")
http.ListenAndServe(":8080", nil)
}
}