fastimage 1.7.0 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.textile +6 -0
- data/lib/fastimage.rb +25 -13
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7228e29b8a52ce589113f051bc142a9e55fefe86
|
4
|
+
data.tar.gz: 6b50be8521f450dd9da08ab018e9a2467c66e62c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 279f60c5b465c44af94619671f803bac088e7b457a41b4b4c203dcbfbf0c0a44a08fd70cafa0740686f00b1b3f01f4efe1f272faac9add9602234aba804be95a
|
7
|
+
data.tar.gz: b8bc0f0ed9d6fb0944f5c28121a3f7d17a70f16eeee80bd4fb3a46525cd2153b59df5c959f8817a70cec395d50c5b61e599595d6f706fe9b56f094a139c88bff
|
data/README.textile
CHANGED
@@ -34,6 +34,8 @@ FastImage normally replies will nil if it encounters an error, but you can pass
|
|
34
34
|
|
35
35
|
FastImage also provides a reader for the content length header provided in HTTP. This may be useful to assess the file size of an image, but do not rely on it exclusively - it will not be present in chunked responses for instance.
|
36
36
|
|
37
|
+
FastImage accepts additional HTTP headers. This can be used to set a user agent or referrer which some servers require. Pass an :http_header argument to specify headers, e.g., :http_header => {'User-Agent' => 'Fake Browser'}.
|
38
|
+
|
37
39
|
h2. Security
|
38
40
|
|
39
41
|
As of v1.6.7 FastImage no longer uses openuri to open files, but directly calls File.open. But take care to sanitise the strings passed to FastImage; it will try to read from whatever is passed.
|
@@ -55,6 +57,8 @@ FastImage.size("http://upload.wikimedia.org/wikipedia/commons/b/b4/Mardin_135066
|
|
55
57
|
=> [9545, 6623]
|
56
58
|
FastImage.new("http://stephensykes.com/images/pngimage").content_length
|
57
59
|
=> 432
|
60
|
+
FastImage.size("http://stephensykes.com/images/ss.com_x.gif", :http_header => {'User-Agent' => 'Fake Browser'})
|
61
|
+
=> [266, 56]
|
58
62
|
</code></pre>
|
59
63
|
|
60
64
|
h2. Installation
|
@@ -67,6 +71,8 @@ h4. Rails
|
|
67
71
|
|
68
72
|
Add fastimage to your Gemfile, and bundle.
|
69
73
|
|
74
|
+
bc. gem 'fastimage'
|
75
|
+
|
70
76
|
Then you're off - just use @FastImage.size()@ and @FastImage.type()@ in your code as in the examples.
|
71
77
|
|
72
78
|
h2. Documentation
|
data/lib/fastimage.rb
CHANGED
@@ -24,6 +24,10 @@
|
|
24
24
|
# This may be useful to assess the file size of an image, but do not rely on it exclusively -
|
25
25
|
# it will not be present in chunked responses for instance.
|
26
26
|
#
|
27
|
+
# FastImage accepts additional HTTP headers. This can be used to set a user agent
|
28
|
+
# or referrer which some servers require. Pass an :http_header argument to specify headers,
|
29
|
+
# e.g., :http_header => {'User-Agent' => 'Fake Browser'}.
|
30
|
+
#
|
27
31
|
# === Examples
|
28
32
|
# require 'fastimage'
|
29
33
|
#
|
@@ -162,10 +166,16 @@ class FastImage
|
|
162
166
|
end
|
163
167
|
|
164
168
|
def initialize(uri, options={})
|
165
|
-
@property = options[:type_only] ? :type : :size
|
166
|
-
@timeout = options[:timeout] || DefaultTimeout
|
167
|
-
@proxy_url = options[:proxy]
|
168
169
|
@uri = uri
|
170
|
+
@options = {
|
171
|
+
:type_only => false,
|
172
|
+
:timeout => DefaultTimeout,
|
173
|
+
:raise_on_failure => false,
|
174
|
+
:proxy => nil,
|
175
|
+
:http_header => {}
|
176
|
+
}.merge(options)
|
177
|
+
|
178
|
+
@property = @options[:type_only] ? :type : :size
|
169
179
|
|
170
180
|
if uri.respond_to?(:read)
|
171
181
|
fetch_using_read(uri)
|
@@ -185,17 +195,17 @@ class FastImage
|
|
185
195
|
|
186
196
|
uri.rewind if uri.respond_to?(:rewind)
|
187
197
|
|
188
|
-
raise SizeNotFound if options[:raise_on_failure] && @property == :size && !@size
|
198
|
+
raise SizeNotFound if @options[:raise_on_failure] && @property == :size && !@size
|
189
199
|
|
190
200
|
rescue Timeout::Error, SocketError, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ECONNRESET,
|
191
201
|
ImageFetchFailure, Net::HTTPBadResponse, EOFError, Errno::ENOENT
|
192
|
-
raise ImageFetchFailure if options[:raise_on_failure]
|
202
|
+
raise ImageFetchFailure if @options[:raise_on_failure]
|
193
203
|
rescue NoMethodError # 1.8.7p248 can raise this due to a net/http bug
|
194
|
-
raise ImageFetchFailure if options[:raise_on_failure]
|
204
|
+
raise ImageFetchFailure if @options[:raise_on_failure]
|
195
205
|
rescue UnknownImageType
|
196
|
-
raise UnknownImageType if options[:raise_on_failure]
|
206
|
+
raise UnknownImageType if @options[:raise_on_failure]
|
197
207
|
rescue CannotParseImage
|
198
|
-
if options[:raise_on_failure]
|
208
|
+
if @options[:raise_on_failure]
|
199
209
|
if @property == :size
|
200
210
|
raise SizeNotFound
|
201
211
|
else
|
@@ -214,8 +224,10 @@ class FastImage
|
|
214
224
|
end
|
215
225
|
|
216
226
|
def fetch_using_http_from_parsed_uri
|
227
|
+
http_header = {'Accept-Encoding' => 'identity'}.merge(@options[:http_header])
|
228
|
+
|
217
229
|
setup_http
|
218
|
-
@http.request_get(@parsed_uri.request_uri,
|
230
|
+
@http.request_get(@parsed_uri.request_uri, http_header) do |res|
|
219
231
|
if res.is_a?(Net::HTTPRedirection) && @redirect_count < 4
|
220
232
|
@redirect_count += 1
|
221
233
|
begin
|
@@ -266,8 +278,8 @@ class FastImage
|
|
266
278
|
|
267
279
|
def proxy_uri
|
268
280
|
begin
|
269
|
-
if @
|
270
|
-
proxy = Addressable::URI.parse(@
|
281
|
+
if @options[:proxy]
|
282
|
+
proxy = Addressable::URI.parse(@options[:proxy])
|
271
283
|
else
|
272
284
|
proxy = ENV['http_proxy'] && ENV['http_proxy'] != "" ? Addressable::URI.parse(ENV['http_proxy']) : nil
|
273
285
|
end
|
@@ -287,8 +299,8 @@ class FastImage
|
|
287
299
|
end
|
288
300
|
@http.use_ssl = (@parsed_uri.scheme == "https")
|
289
301
|
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
290
|
-
@http.open_timeout = @timeout
|
291
|
-
@http.read_timeout = @timeout
|
302
|
+
@http.open_timeout = @options[:timeout]
|
303
|
+
@http.read_timeout = @options[:timeout]
|
292
304
|
end
|
293
305
|
|
294
306
|
def fetch_using_read(readable)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fastimage
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stephen Sykes
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-11-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: addressable
|