ires 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 573e3e3d26bbe6a54df1d21e5445287b94eb628e
4
- data.tar.gz: a29209459ec9bb7dea651db214c3440db9cbd09c
3
+ metadata.gz: 33c82f6045075a2fdb97d1ad5dc19194b899792d
4
+ data.tar.gz: 3152843aadd1e7fff8882e64b62f9f9a730bcc29
5
5
  SHA512:
6
- metadata.gz: 7dfe086ce436074e319636b71eafa3736721c71a0c3c7bb175723e4279c15f6282d3aa72bc181d080798440612f2f70ba8ccaa3b40b2deebaad40f58a86ea3dc
7
- data.tar.gz: f10ae44af632f4ed0fae261a3e26cba54985006e95913350fcde7cbba1e618037bccd5e0c69775c28cc9c2648f1498a37d51f78d115c061d87f440211feb1cde
6
+ metadata.gz: 8581fcad0dab4c6a373104b5eb06eb017b8dad2bd8e6f420cbfb045e4c611fe148412fdb583df0d534d0f16e05ceebda06eeb758c6021228695f2dd313f3590d
7
+ data.tar.gz: 377f504cbe190e9f5df79e6dfc7ffc6b1f29d02060afd3df70e6596cea9be3d8f013ad6928cdd9abad17d4f825825a814e9d6966d59d30ccd24c2d3695feddd9
@@ -19,21 +19,30 @@ import (
19
19
  )
20
20
 
21
21
  // Check expiration date
22
- func (i *Ires) DeleteExpireImage() {
22
+ func (i *Ires) DeleteExpireImage() error {
23
23
  today := time.Now().Format("20060102")
24
- dir := i.readImageDir()
24
+ dir, err := i.readImageDir()
25
+ if err != nil {
26
+ return err
27
+ }
25
28
  files, err := ioutil.ReadDir(dir)
26
29
  if err != nil {
27
- return
30
+ return err
28
31
  }
29
32
 
30
33
  for _, file := range files {
31
34
  findName := file.Name()
32
- matched, _ := path.Match(today+"_*", findName)
35
+ matched, err := path.Match(today+"_*", findName)
36
+ if err != nil {
37
+ return err
38
+ }
33
39
  if matched {
34
- deleteImage(path.Join(dir, findName))
40
+ if err := deleteImage(path.Join(dir, findName)); err != nil {
41
+ return err
42
+ }
35
43
  }
36
44
  }
45
+ return nil
37
46
  }
38
47
 
39
48
  // Input image type is Local or HTTP
@@ -46,7 +55,7 @@ func (i *Ires) CheckLocal() {
46
55
  }
47
56
 
48
57
  // Input image
49
- func (i *Ires) inputImage() bool {
58
+ func (i *Ires) inputImage() error {
50
59
  if i.IsLocal {
51
60
  img, format := localImage(i.URI)
52
61
  i.InputImage = &InputImage{
@@ -55,45 +64,53 @@ func (i *Ires) inputImage() bool {
55
64
  URI: i.URI,
56
65
  }
57
66
  i.setConfig()
58
- return true
67
+ return nil
59
68
  }
60
69
  return i.downloadImage()
61
70
  }
62
71
 
63
72
  // Save http image
64
- func (i *Ires) downloadImage() bool {
73
+ func (i *Ires) downloadImage() error {
65
74
  res, err := http.Get(i.URI)
66
75
  if err != nil {
67
- return false
76
+ return err
68
77
  }
69
78
  defer res.Body.Close()
70
79
 
71
- header, r := copyReader(res.Body)
72
- format := formatSearch(r)
80
+ buf := new(bytes.Buffer)
81
+ io.Copy(buf, res.Body)
82
+ format := formatSearch(res.Body)
73
83
 
74
- img, _, err := image.Decode(io.MultiReader(header, res.Body))
84
+ img, _, err := image.Decode(buf)
75
85
  if err != nil {
76
- return false
86
+ return err
87
+ }
88
+
89
+ var distURI string
90
+ if distURI, err = i.imageURI(true); err != nil {
91
+ return err
77
92
  }
78
93
 
79
- distURI := i.imageURI(true)
80
94
  i.InputImage = &InputImage{
81
95
  Image: img,
82
96
  Format: format,
83
97
  URI: distURI,
84
98
  }
85
99
 
86
- if createImage(img, distURI, format) {
87
- i.setConfig()
88
- return true
100
+ if createImage(img, distURI, format) != nil {
101
+ return err
102
+ } else {
103
+ if err := i.setConfig(); err != nil {
104
+ return err
105
+ }
89
106
  }
90
- return false
107
+ return nil
91
108
  }
92
109
 
93
- func createImage(img image.Image, path, format string) bool {
110
+ func createImage(img image.Image, path, format string) error {
94
111
  file, err := os.Create(path)
95
112
  if err != nil {
96
- return false
113
+ return err
97
114
  }
98
115
  defer file.Close()
99
116
 
@@ -107,7 +124,7 @@ func createImage(img image.Image, path, format string) bool {
107
124
  default:
108
125
  jpeg.Encode(file, img, nil)
109
126
  }
110
- return true
127
+ return nil
111
128
  }
112
129
 
113
130
  // Load image
@@ -118,11 +135,11 @@ func localImage(uri string) (image.Image, string) {
118
135
  }
119
136
  defer file.Close()
120
137
 
121
- // Decode jpeg into image.Image
122
- header, r := copyReader(file)
123
- format := formatSearch(r)
138
+ buf := new(bytes.Buffer)
139
+ io.Copy(buf, file)
140
+ format := formatSearch(file)
124
141
 
125
- img, _, err := image.Decode(io.MultiReader(header, file))
142
+ img, _, err := image.Decode(buf)
126
143
  if err != nil {
127
144
  return nil, ""
128
145
  }
@@ -130,22 +147,26 @@ func localImage(uri string) (image.Image, string) {
130
147
  }
131
148
 
132
149
  // Set image config
133
- func (i *Ires) setConfig() {
150
+ func (i *Ires) setConfig() error {
134
151
  file, err := os.Open(i.InputImage.URI)
135
152
  if err != nil {
136
- panic(err)
153
+ return err
137
154
  }
138
155
  defer file.Close()
139
156
 
140
- conf, _, err := image.DecodeConfig(file)
157
+ buf := new(bytes.Buffer)
158
+ io.Copy(buf, file)
159
+
160
+ conf, _, err := image.DecodeConfig(buf)
141
161
  if err != nil {
142
- panic(err)
162
+ return err
143
163
  }
144
164
  i.InputImage.Config = conf
165
+ return nil
145
166
  }
146
167
 
147
168
  // Resizing & Cropping
148
- func (i *Ires) resizeToCrop() image.Image {
169
+ func (i *Ires) resizeToCrop() (image.Image, error) {
149
170
  inputImg := i.InputImage.Image
150
171
  var outputImg image.Image
151
172
  isAsp, conf := i.isValidAspectRatio()
@@ -170,30 +191,36 @@ func (i *Ires) resizeToCrop() image.Image {
170
191
  }
171
192
 
172
193
  // Cropping
173
- outputImg, _ = cutter.Crop(resizeImg, cutter.Config{
194
+ var err error
195
+ if outputImg, err = cutter.Crop(resizeImg, cutter.Config{
174
196
  Width: width,
175
197
  Height: height,
176
198
  Mode: cutter.Centered,
177
199
  Options: cutter.Copy,
178
- })
200
+ }); err != nil {
201
+ return nil, err
202
+ }
179
203
  }
180
- return outputImg
204
+ return outputImg, nil
181
205
  }
182
206
 
183
207
  // Delete image
184
- func deleteImage(path string) {
185
- _, err := os.Stat(path)
186
- if err == nil {
208
+ func deleteImage(path string) error {
209
+ if _, err := os.Stat(path); err != nil {
210
+ return err
211
+ } else {
187
212
  if err := os.Remove(path); err != nil {
188
- panic(err)
213
+ return err
189
214
  }
190
215
  }
216
+ return nil
191
217
  }
192
218
 
193
219
  // Verify aspect ratio
194
220
  func (i *Ires) isValidAspectRatio() (bool, image.Config) {
195
221
  conf := i.InputImage.Config
196
222
  s := i.Size
223
+ println("\n")
197
224
  aspH := (conf.Height * s.Width) / conf.Width
198
225
  if aspH == s.Height {
199
226
  return true, conf
@@ -228,13 +255,6 @@ func formatSearch(r io.Reader) string {
228
255
  return format
229
256
  }
230
257
 
231
- // Copy Reader
232
- func copyReader(body io.Reader) (io.Reader, io.Reader) {
233
- header := bytes.NewBuffer(nil)
234
- r := io.TeeReader(body, header)
235
- return header, r
236
- }
237
-
238
258
  // Valid resize type
239
259
  func (i *Ires) validResizeType() bool {
240
260
  config := i.InputImage.Config
@@ -51,16 +51,19 @@ type (
51
51
  )
52
52
 
53
53
  // Resize is ...
54
- func (i *Ires) Resize() string {
54
+ func (i *Ires) Resize() (string, error) {
55
55
  i.Mode = Resize
56
- distURI := i.imageURI(false)
56
+ distURI, err := i.imageURI(false)
57
+ if err != nil {
58
+ return "", err
59
+ }
57
60
  // When the image exists, return the image path
58
61
  if isExistsImage(distURI) {
59
- return i.targetImageURI(distURI)
62
+ return i.targetImageURI(distURI), nil
60
63
  }
61
64
 
62
- if !i.inputImage() {
63
- return i.URI
65
+ if err := i.inputImage(); err != nil {
66
+ return "", err
64
67
  }
65
68
 
66
69
  var outputImg image.Image
@@ -70,20 +73,23 @@ func (i *Ires) Resize() string {
70
73
  outputImg = i.InputImage.Image
71
74
  }
72
75
  createImage(outputImg, distURI, i.InputImage.Format)
73
- return i.targetImageURI(distURI)
76
+ return i.targetImageURI(distURI), nil
74
77
  }
75
78
 
76
79
  // Crop is Crop ...
77
- func (i *Ires) Crop() string {
80
+ func (i *Ires) Crop() (string, error) {
78
81
  i.Mode = Crop
79
- distURI := i.imageURI(false)
82
+ distURI, err := i.imageURI(false)
83
+ if err != nil {
84
+ return "", err
85
+ }
80
86
  // When the image exists, return the image path
81
87
  if isExistsImage(distURI) {
82
- return i.targetImageURI(distURI)
88
+ return i.targetImageURI(distURI), nil
83
89
  }
84
90
 
85
- if !i.inputImage() {
86
- return i.URI
91
+ if err := i.inputImage(); err != nil {
92
+ return "", err
87
93
  }
88
94
 
89
95
  var outputImg image.Image
@@ -98,28 +104,35 @@ func (i *Ires) Crop() string {
98
104
  outputImg = i.InputImage.Image
99
105
  }
100
106
  createImage(outputImg, distURI, i.InputImage.Format)
101
- return i.targetImageURI(distURI)
107
+ return i.targetImageURI(distURI), nil
102
108
  }
103
109
 
104
110
  // ResizeToCrop is ...
105
- func (i *Ires) ResizeToCrop() string {
111
+ func (i *Ires) ResizeToCrop() (string, error) {
106
112
  i.Mode = ResizeToCrop
107
- distURI := i.imageURI(false)
113
+ distURI, err := i.imageURI(false)
114
+ if err != nil {
115
+ return "", err
116
+ }
108
117
  // When the image exists, return the image path
109
118
  if isExistsImage(distURI) {
110
- return i.targetImageURI(distURI)
119
+ return i.targetImageURI(distURI), nil
111
120
  }
112
121
 
113
- if !i.inputImage() {
114
- return i.URI
122
+ if err := i.inputImage(); err != nil {
123
+ return "", err
115
124
  }
116
125
 
117
126
  var outputImg image.Image
118
127
  if i.validResizeType() {
119
- outputImg = i.resizeToCrop()
128
+ if img, err := i.resizeToCrop(); err != nil {
129
+ return "", err
130
+ } else {
131
+ outputImg = img
132
+ }
120
133
  } else {
121
134
  outputImg = i.InputImage.Image
122
135
  }
123
136
  createImage(outputImg, distURI, i.InputImage.Format)
124
- return i.targetImageURI(distURI)
137
+ return i.targetImageURI(distURI), nil
125
138
  }
@@ -10,7 +10,7 @@ import (
10
10
  )
11
11
 
12
12
  // Generate image path
13
- func (i *Ires) imageURI(original bool) string {
13
+ func (i *Ires) imageURI(original bool) (string, error) {
14
14
  paths := []rune(i.Dir)
15
15
  pathsLastIndex := len(paths) - 1
16
16
  lastChar := string(paths[pathsLastIndex])
@@ -23,19 +23,23 @@ func (i *Ires) imageURI(original bool) string {
23
23
  if i.IsLocal {
24
24
  oDir = localPath(i.Mode)
25
25
  } else {
26
- oDir = remotePath(i.URI)
26
+ if d, err := remotePath(i.URI); err != nil {
27
+ return "", nil
28
+ } else {
29
+ oDir = d
30
+ }
27
31
  }
28
32
 
29
33
  // Create directory
30
34
  oPath := filepath.Join(dir, oDir)
31
35
  if _, err := os.Stat(oPath); err != nil {
32
36
  if err := os.MkdirAll(oPath, 0777); err != nil {
33
- panic(err)
37
+ return "", err
34
38
  }
35
39
  }
36
40
 
37
41
  name := i.imageName(original)
38
- return filepath.Join(oPath, name)
42
+ return filepath.Join(oPath, name), nil
39
43
  }
40
44
 
41
45
  // Generate image name
@@ -88,14 +92,18 @@ func isExistsImage(path string) bool {
88
92
  }
89
93
 
90
94
  // Read directory
91
- func (i *Ires) readImageDir() string {
95
+ func (i *Ires) readImageDir() (string, error) {
92
96
  var dir string
93
97
  if i.IsLocal {
94
98
  dir = localPath(i.Mode)
95
99
  } else {
96
- dir = remotePath(i.URI)
100
+ if d, err := remotePath(i.URI); err != nil {
101
+ return "", err
102
+ } else {
103
+ dir = d
104
+ }
97
105
  }
98
- return filepath.Join(i.Dir, dir)
106
+ return filepath.Join(i.Dir, dir), nil
99
107
  }
100
108
 
101
109
  // if local image, create ires directory
@@ -112,19 +120,19 @@ func localPath(mode Mode) string {
112
120
  return dir
113
121
  }
114
122
 
115
- // if http image, parse URL & make directory
116
- func remotePath(uri string) string {
123
+ // if http image, parse URI & make directory
124
+ func remotePath(uri string) (string, error) {
117
125
  u, err := urlx.Parse(uri)
118
- dir := []string{"ires"}
119
126
  if err != nil {
120
- panic(err)
127
+ return "", err
121
128
  }
129
+ dir := []string{"ires"}
122
130
 
123
131
  dir = append(dir, u.Host)
124
132
  path := strings.Split(u.Path, "/")
125
133
  dir = append(dir, path[1:len(path)-1]...)
126
134
 
127
- return strings.Join(dir, "/")
135
+ return strings.Join(dir, "/"), nil
128
136
  }
129
137
 
130
138
  // Optimize image path
@@ -2,6 +2,7 @@ package main
2
2
 
3
3
  import (
4
4
  "C"
5
+ "log"
5
6
 
6
7
  "github.com/endotakuya/ires/ext/ires"
7
8
  )
@@ -32,13 +33,19 @@ func iresImagePath(URI *C.char, width, height, rType, mode int, Dir, Expire *C.c
32
33
  r.DeleteExpireImage()
33
34
 
34
35
  var distURI string
36
+ var err error
35
37
  switch ires.Mode(mode) {
36
38
  case ires.Resize:
37
- distURI = r.Resize()
39
+ distURI, err = r.Resize()
38
40
  case ires.Crop:
39
- distURI = r.Crop()
41
+ distURI, err = r.Crop()
40
42
  case ires.ResizeToCrop:
41
- distURI = r.ResizeToCrop()
43
+ distURI, err = r.ResizeToCrop()
44
+ }
45
+
46
+ if err != nil {
47
+ log.Print(err)
48
+ return C.CString(r.URI)
42
49
  }
43
50
  return C.CString(distURI)
44
51
  }
@@ -1,3 +1,3 @@
1
1
  module Ires
2
- VERSION = '1.0.0'.freeze
2
+ VERSION = '1.1.0'.freeze
3
3
  end
Binary file
Binary file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ires
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - enta0701
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-27 00:00:00.000000000 Z
11
+ date: 2018-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport