/
image.go
220 lines (176 loc) · 4.86 KB
/
image.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright 2012 - 2014 Alex Palaistras. All rights reserved.
// Use of this source code is governed by the MIT License, the
// full text of which can be found in the LICENSE file.
// Package image contains methods for manipulating images, including
// resizing, cropping etc.
package image
import (
"encoding/base64"
"fmt"
"image"
"image/draw"
"image/jpeg"
"image/png"
"io"
"math"
"net/http"
"os"
"strconv"
"github.com/nfnt/resize"
"github.com/deuill/sleepy/core/config"
"github.com/deuill/sleepy/core/server"
"github.com/deuill/sleepy/core/user"
)
type Image struct {
// Contains private or unexported fields.
conf *config.Config
id map[string]string
}
type Request struct {
Auth string
Remote string
Checksum string
Filename string
W int64
H int64
X int64
Y int64
Aspect float64
}
func (i *Image) Crop(p Request) (string, error) {
datadir := i.conf.S("directories", "data")
address := i.conf.S("http", "address")
port := i.conf.S("http", "port")
path, err := i.filepath(fmt.Sprintf("op=crop;x=%d;y=%d;w=%d;h=%d", p.X, p.Y, p.W, p.H), &p)
if err != nil {
return "", nil
}
// Check for cached file.
if _, err = os.Stat(datadir + "/serve" + path + p.Filename); err == nil {
os.Remove(os.TempDir() + "/sleepy/" + i.id[p.Auth] + "/" + p.Checksum)
return address + ":" + port + path + p.Filename, nil
}
// Upload and process image.
img, format, err := i.upload(&p)
if err != nil {
return "", nil
}
b := img.Bounds()
maxX := int(p.X + p.W)
maxY := int(p.Y + p.H)
// Crop only if the sub-image is inside the bounds of the source image.
if b.Max.X >= maxX && b.Max.Y >= maxY {
m := image.NewRGBA(b)
draw.Draw(m, b, img, b.Min, draw.Src)
t := m.SubImage(image.Rect(int(p.X), int(p.Y), maxX, maxY))
err = generate(t, format, datadir+"/serve"+path, p.Filename)
if err != nil {
return "", nil
}
return address + ":" + port + path + p.Filename, nil
}
return "", nil
}
func (i *Image) Resize(p Request) (string, error) {
datadir, _ := i.conf.String("directories", "data")
address, _ := i.conf.String("http", "address")
port, _ := i.conf.String("http", "port")
path, err := i.filepath(fmt.Sprintf("op=resize;w=%d;h=%d;a=%f", p.W, p.H, p.Aspect), &p)
if err != nil {
return "", nil
}
// Check for cached file.
if _, err = os.Stat(datadir + "/serve" + path + p.Filename); err == nil {
os.Remove(os.TempDir() + "/sleepy/" + i.id[p.Auth] + "/" + p.Checksum)
return address + ":" + port + path + p.Filename, nil
}
// Upload and process image.
img, format, err := i.upload(&p)
if err != nil {
return "", nil
}
var t image.Image
b := img.Bounds()
if p.Aspect == 0 {
factor := math.Min(float64(p.W)/float64(b.Max.X), float64(p.H)/float64(b.Max.Y))
t = resize.Resize(uint(factor*float64(b.Max.X)), uint(factor*float64(b.Max.Y)), img, resize.Bicubic)
} else {
t = resize.Resize(uint(p.W), uint(p.H), img, resize.Bicubic)
}
err = generate(t, format, datadir+"/serve"+path, p.Filename)
if err != nil {
return "", nil
}
return address + ":" + port + path + p.Filename, nil
}
func (i *Image) filepath(options string, p *Request) (string, error) {
if len(p.Checksum) != 40 {
return "", fmt.Errorf("checksum does not appear to be an SHA-1 hash.")
}
if _, exists := i.id[p.Auth]; !exists {
u, err := user.Auth(p.Auth)
if err != nil {
return "", err
}
i.id[p.Auth] = strconv.FormatInt(int64(u.Id), 10)
}
c := p.Checksum
hash := c[:2] + "/" + c[2:6] + "/" + c[6:14] + "/" + c[14:27] + "/" + c[27:]
opts := base64.StdEncoding.EncodeToString([]byte(options))
path := "/" + i.id[p.Auth] + "/" + hash + "/" + opts + "/"
return path, nil
}
func (i *Image) upload(p *Request) (image.Image, string, error) {
var err error
var src io.ReadCloser
if p.Remote != "" {
resp, err := http.Get(p.Remote)
if err != nil {
return nil, "", err
} else if resp.StatusCode != 200 {
resp.Body.Close()
return nil, "", fmt.Errorf("receiving image failed with code: %d", resp.StatusCode)
}
src = resp.Body
} else {
tmpfile := os.TempDir() + "/sleepy/" + i.id[p.Auth] + "/" + p.Checksum
if src, err = os.Open(tmpfile); err != nil {
return nil, "", err
}
defer os.Remove(tmpfile)
}
defer src.Close()
img, format, err := image.Decode(src)
if err != nil {
return nil, "", fmt.Errorf("image could not be decoded: %s", err)
}
return img, format, nil
}
func generate(img image.Image, format, path, filename string) error {
err := os.MkdirAll(path, 0755)
if err != nil {
return err
}
out, err := os.Create(path + filename)
if err != nil {
return err
}
defer out.Close()
switch format {
case "jpeg":
jpeg.Encode(out, img, &jpeg.Options{90})
case "png":
png.Encode(out, img)
}
return nil
}
func (i *Image) Setup(config *config.Config) error {
i.conf = config
return nil
}
func init() {
server.Register(&Image{
&config.Config{},
make(map[string]string),
})
}