/
imgi.go
100 lines (87 loc) · 2 KB
/
imgi.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
package imgi
import (
"errors"
"io/ioutil"
"net/http"
"path/filepath"
"strconv"
"strings"
log "github.com/sirupsen/logrus"
)
type Server struct {
config Config
}
func NewServer(config Config) *Server {
srv := &Server{
config: config,
}
return srv
}
func (srv *Server) Start() {
config := srv.config
addr := ":" + strconv.Itoa(config.General.Port)
log.Infof("imgi start listening on %s", addr)
http.ListenAndServe(addr, createHandler(config))
}
func createHandler(config Config) http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
defer func() {
if p := recover(); p != nil { // revover from unknown panicking and response with 500 error
log.Error(p)
replyError(w, r, errors.New("unknown error"), http.StatusInternalServerError)
}
}()
img, err := fetchImage(config, r)
if err != nil {
replyError(w, r, err, http.StatusNotFound)
return
}
query := r.URL.Query().Get("imgi")
ops, err := parseOperations(query)
if err != nil {
replyError(w, r, err, http.StatusBadRequest)
return
}
if len(ops) == 0 {
replyImage(w, r, img)
return
}
for _, op := range ops {
img, err = op.Action.Act(img, op.Options)
if err != nil {
replyError(w, r, err, http.StatusInternalServerError)
return
}
}
replyImage(w, r, img)
})
return mux
}
func replyImage(w http.ResponseWriter, r *http.Request, img Image) {
w.Header().Set("Content-Type", img.Mime)
w.Header().Set("Content-Length", strconv.Itoa(len(img.Buf)))
w.Write(img.Buf)
}
func fetchImage(config Config, r *http.Request) (Image, error) {
img := Image{}
path := r.URL.Path
var file string
for k, v := range config.Locations {
if strings.HasPrefix(path, k) {
path = strings.TrimPrefix(path, k)
file = filepath.Join(v, path)
break
}
}
if file == "" {
return img, ErrNotFound
}
buf, err := ioutil.ReadFile(file)
if err != nil {
return img, ErrNotFound
}
img.Buf = buf
img.Mime = http.DetectContentType(buf)
return img, nil
}