/
workers_images.go
64 lines (52 loc) · 1.33 KB
/
workers_images.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
package app
import (
"io"
"net/http"
"os"
"path/filepath"
"github.com/anthonynsimon/bild/imgio"
"github.com/anthonynsimon/bild/transform"
"github.com/pkg/errors"
)
func imageDownload(source, destination string) error {
base := filepath.Dir(destination)
if _, err := os.Stat(base); os.IsNotExist(err) {
if err := os.MkdirAll(base, 0755); err != nil {
return errors.Wrap(err, "creating directory")
}
}
out, err := os.Create(destination)
if err != nil {
return errors.Wrap(err, "creating file")
}
defer out.Close()
resp, err := http.Get(source)
if err != nil {
return errors.Wrap(err, "getting image")
}
defer resp.Body.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
return errors.Wrap(err, "copying image")
}
err = os.Chown(destination, 1001, 1001)
if err != nil {
return errors.Wrap(err, "chowning image")
}
return nil
}
func imageResize(source, destination string, width, height int) error {
img, err := imgio.Open(source)
if err != nil {
return errors.Wrap(err, "opening image")
}
resized := transform.Resize(img, width, height, transform.Lanczos)
if err := imgio.Save(destination, resized, imgio.JPEGEncoder(80)); err != nil {
return errors.Wrap(err, "saving image")
}
err = os.Chown(destination, 1001, 1001)
if err != nil {
return errors.Wrap(err, "chowning image")
}
return nil
}