-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworker.go
158 lines (145 loc) · 3.89 KB
/
worker.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"fmt"
"image"
"image/gif"
"image/jpeg"
"image/png"
"io"
"os"
"path/filepath"
"time"
"github.com/thraxil/resize"
"github.com/go-kit/kit/log"
)
type resizeRequest struct {
Path string
Extension string
Size string
Response chan resizeResponse
}
type resizeResponse struct {
OutputImage *image.Image
Success bool
Magick bool
}
var decoders = map[string](func(io.Reader) (image.Image, error)){
"jpg": jpeg.Decode,
"gif": gif.Decode,
"png": png.Decode,
}
func resizeWorker(requests chan resizeRequest, sl log.Logger, s *siteConfig) {
for req := range requests {
if !s.Writeable {
// node is not writeable, so we should never handle a resize
req.Response <- resizeResponse{nil, false, false}
continue
}
sl.Log("level", "INFO", "msg", "handling a resize request", "path", req.Path)
t0 := time.Now()
fi, err := os.Stat(req.Path)
if err != nil {
sl.Log("level", "ERR", "msg", "resize worker couldn't stat path",
"path", req.Path, "error", err)
req.Response <- resizeResponse{nil, false, false}
continue
}
if fi.IsDir() {
sl.Log("level", "ERR", "msg", "can't resize a directory",
"path", req.Path)
req.Response <- resizeResponse{nil, false, false}
continue
}
origFile, err := os.Open(req.Path)
if err != nil {
origFile.Close()
sl.Log("level", "ERR", "msg", "resize worker could not open image",
"image", req.Path, "error", err.Error())
req.Response <- resizeResponse{nil, false, false}
continue
} else {
origFile.Close()
}
_, err = imageMagickResize(req.Path, req.Size, sl, s)
if err != nil {
// imagemagick couldn't handle it either
sl.Log("level", "ERR", "msg", "imagemagick couldn't handle it", "error", err.Error())
req.Response <- resizeResponse{nil, false, false}
} else {
// imagemagick saved the day
sl.Log("level", "INFO", "msg", "rescued by imagemagick")
req.Response <- resizeResponse{nil, true, true}
t1 := time.Now()
sl.Log("level", "INFO", "msg", "finished resize", "time", t1.Sub(t0))
}
}
}
// Go's built-in image/jpeg can't load progressive jpegs
// so sometimes we need to bail and have imagemagick do the work
// this sucks, is redundant, and i'd rather not have this external dependency
// so this will be removed as soon as Go can handle it all itself
func imageMagickResize(path, size string, sl log.Logger,
s *siteConfig) (string, error) {
args := convertArgs(size, path, s.ImageMagickConvertPath)
fds := []*os.File{os.Stdin, os.Stdout, os.Stderr}
p, err := os.StartProcess(args[0], args, &os.ProcAttr{Files: fds})
defer p.Release()
if err != nil {
sl.Log("level", "ERR", "msg", "imagemagick failed to start",
"error", err.Error())
return "", err
}
_, err = p.Wait()
if err != nil {
sl.Log("level", "ERR", "msg", "imagemagick failed",
"error", err.Error())
return "", err
}
return resizedPath(path, size), nil
}
func resizedPath(path, size string) string {
d := filepath.Dir(path)
extension := filepath.Ext(path)
return d + "/" + size + extension
}
func convertArgs(size, path, convertBin string) []string {
// need to convert our size spec to what convert expects
// we can ignore 'full' since that will never trigger
// a resize_worker request
s := resize.MakeSizeSpec(size)
var maxDim int
if s.Width() > s.Height() {
maxDim = s.Width()
} else {
maxDim = s.Height()
}
var args []string
if s.IsSquare() {
args = []string{
convertBin,
"-resize",
s.ToImageMagickSpec(),
"-auto-orient",
"-gravity",
"center",
"-extent",
fmt.Sprintf("%dx%d", maxDim, maxDim),
path,
resizedPath(path, size),
}
} else {
// BUG(thraxil): this auto orients properly
// but doesn't switch width/height in that case
// so 90 or 270 degree rotations come out the wrong
// size
args = []string{
convertBin,
"-auto-orient",
"-resize",
s.ToImageMagickSpec(),
path,
resizedPath(path, size),
}
}
return args
}