fastimage 1.8.1 → 1.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.textile +4 -0
- data/lib/fastimage.rb +23 -6
- metadata +3 -37
- data/test/fixtures/bad.jpg +0 -0
- data/test/fixtures/exif_orientation.jpg +0 -0
- data/test/fixtures/faulty.jpg +0 -0
- data/test/fixtures/folder with spaces/test.bmp +0 -0
- data/test/fixtures/gzipped.jpg +0 -0
- data/test/fixtures/infinite.jpg +0 -0
- data/test/fixtures/man.ico +0 -0
- data/test/fixtures/orient_2.jpg +0 -0
- data/test/fixtures/test.bmp +0 -0
- data/test/fixtures/test.cur +0 -0
- data/test/fixtures/test.gif +0 -0
- data/test/fixtures/test.jpg +0 -0
- data/test/fixtures/test.png +0 -0
- data/test/fixtures/test.psd +0 -0
- data/test/fixtures/test.svg +0 -43
- data/test/fixtures/test.tiff +0 -0
- data/test/fixtures/test2.bmp +0 -0
- data/test/fixtures/test2.jpg +0 -0
- data/test/fixtures/test2.tiff +0 -0
- data/test/fixtures/test3.jpg +0 -0
- data/test/fixtures/test4.jpg +0 -0
- data/test/fixtures/test_partial_viewport.svg +0 -9
- data/test/fixtures/truncated_gzipped.jpg +0 -0
- data/test/fixtures/webp_vp8.webp +0 -0
- data/test/fixtures/webp_vp8l.webp +0 -0
- data/test/fixtures/webp_vp8x.webp +0 -0
- data/test/test.rb +0 -323
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4139140af1d4da8561ce783a2e83c8aae8add72b
|
4
|
+
data.tar.gz: 0a34175bccf3480f36e25e1501f6618ab7a50036
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9ec7651d2df372e19087299a18893522037507c92412c5456d2f96e2ab61e53dafb7f69ad06b79d2c80c9da35d785c4c29754a7b9255d1664f7f2bb7dbe0c4b8
|
7
|
+
data.tar.gz: 10981e201c0eb9a651678fb111ad0a503d73fccbecd09e57d752c4508e6a9d30508abc2da8c53184d3b1ee48883bee2c81b003e57696048de1b200ef7be93c89
|
data/README.textile
CHANGED
@@ -36,6 +36,8 @@ FastImage also provides a reader for the content length header provided in HTTP.
|
|
36
36
|
|
37
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
38
|
|
39
|
+
FastImage can give you information about the parsed display orientation of an image with Exif data (jpeg or tiff).
|
40
|
+
|
39
41
|
h2. Security
|
40
42
|
|
41
43
|
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.
|
@@ -59,6 +61,8 @@ FastImage.new("http://stephensykes.com/images/pngimage").content_length
|
|
59
61
|
=> 432
|
60
62
|
FastImage.size("http://stephensykes.com/images/ss.com_x.gif", :http_header => {'User-Agent' => 'Fake Browser'})
|
61
63
|
=> [266, 56]
|
64
|
+
FastImage.new("http://stephensykes.com/images/ExifOrientation3.jpg").orientation
|
65
|
+
=> 3
|
62
66
|
</code></pre>
|
63
67
|
|
64
68
|
h2. Installation
|
data/lib/fastimage.rb
CHANGED
@@ -28,6 +28,9 @@
|
|
28
28
|
# or referrer which some servers require. Pass an :http_header argument to specify headers,
|
29
29
|
# e.g., :http_header => {'User-Agent' => 'Fake Browser'}.
|
30
30
|
#
|
31
|
+
# FastImage can give you information about the parsed display orientation of an image with Exif
|
32
|
+
# data (jpeg or tiff).
|
33
|
+
#
|
31
34
|
# === Examples
|
32
35
|
# require 'fastimage'
|
33
36
|
#
|
@@ -39,6 +42,10 @@
|
|
39
42
|
# => :gif
|
40
43
|
# File.open("/some/local/file.gif", "r") {|io| FastImage.type(io)}
|
41
44
|
# => :gif
|
45
|
+
# FastImage.new("http://stephensykes.com/images/pngimage").content_length
|
46
|
+
# => 432
|
47
|
+
# FastImage.new("http://stephensykes.com/images/ExifOrientation3.jpg").orientation
|
48
|
+
# => 3
|
42
49
|
#
|
43
50
|
# === References
|
44
51
|
# * http://snippets.dzone.com/posts/show/805
|
@@ -56,7 +63,7 @@ require 'pathname'
|
|
56
63
|
require 'zlib'
|
57
64
|
|
58
65
|
class FastImage
|
59
|
-
attr_reader :size, :type, :content_length
|
66
|
+
attr_reader :size, :type, :content_length, :orientation
|
60
67
|
|
61
68
|
attr_reader :bytes_read
|
62
69
|
|
@@ -339,6 +346,13 @@ class FastImage
|
|
339
346
|
begin
|
340
347
|
result = send("parse_#{@property}")
|
341
348
|
if result
|
349
|
+
# extract exif orientation if it was found
|
350
|
+
if @property == :size && result.size == 3
|
351
|
+
@orientation = result.pop
|
352
|
+
else
|
353
|
+
@orientation = 1
|
354
|
+
end
|
355
|
+
|
342
356
|
instance_variable_set("@#{@property}", result)
|
343
357
|
else
|
344
358
|
raise CannotParseImage
|
@@ -499,7 +513,7 @@ class FastImage
|
|
499
513
|
height = @stream.read_int
|
500
514
|
width = @stream.read_int
|
501
515
|
width, height = height, width if @exif && @exif.rotated?
|
502
|
-
return [width, height]
|
516
|
+
return [width, height, @exif ? @exif.orientation : 1]
|
503
517
|
end
|
504
518
|
end
|
505
519
|
end
|
@@ -558,14 +572,15 @@ class FastImage
|
|
558
572
|
end
|
559
573
|
|
560
574
|
class Exif # :nodoc:
|
561
|
-
attr_reader :width, :height
|
575
|
+
attr_reader :width, :height, :orientation
|
576
|
+
|
562
577
|
def initialize(stream)
|
563
578
|
@stream = stream
|
564
579
|
parse_exif
|
565
580
|
end
|
566
581
|
|
567
582
|
def rotated?
|
568
|
-
@orientation
|
583
|
+
@orientation >= 5
|
569
584
|
end
|
570
585
|
|
571
586
|
private
|
@@ -614,6 +629,8 @@ class FastImage
|
|
614
629
|
@stream.read(offset - 8)
|
615
630
|
|
616
631
|
parse_exif_ifd
|
632
|
+
|
633
|
+
@orientation ||= 1
|
617
634
|
end
|
618
635
|
|
619
636
|
end
|
@@ -621,9 +638,9 @@ class FastImage
|
|
621
638
|
def parse_size_for_tiff
|
622
639
|
exif = Exif.new(@stream)
|
623
640
|
if exif.rotated?
|
624
|
-
[exif.height, exif.width]
|
641
|
+
[exif.height, exif.width, exif.orientation]
|
625
642
|
else
|
626
|
-
[exif.width, exif.height]
|
643
|
+
[exif.width, exif.height, exif.orientation]
|
627
644
|
end
|
628
645
|
end
|
629
646
|
|
metadata
CHANGED
@@ -1,23 +1,20 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fastimage
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.9.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:
|
11
|
+
date: 2016-02-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: addressable
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '2.3'
|
20
|
-
- - ">="
|
21
18
|
- !ruby/object:Gem::Version
|
22
19
|
version: 2.3.5
|
23
20
|
type: :runtime
|
@@ -25,9 +22,6 @@ dependencies:
|
|
25
22
|
version_requirements: !ruby/object:Gem::Requirement
|
26
23
|
requirements:
|
27
24
|
- - "~>"
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
version: '2.3'
|
30
|
-
- - ">="
|
31
25
|
- !ruby/object:Gem::Version
|
32
26
|
version: 2.3.5
|
33
27
|
- !ruby/object:Gem::Dependency
|
@@ -98,33 +92,6 @@ files:
|
|
98
92
|
- README.textile
|
99
93
|
- lib/fastimage.rb
|
100
94
|
- lib/fastimage/fbr.rb
|
101
|
-
- test/fixtures/bad.jpg
|
102
|
-
- test/fixtures/exif_orientation.jpg
|
103
|
-
- test/fixtures/faulty.jpg
|
104
|
-
- test/fixtures/folder with spaces/test.bmp
|
105
|
-
- test/fixtures/gzipped.jpg
|
106
|
-
- test/fixtures/infinite.jpg
|
107
|
-
- test/fixtures/man.ico
|
108
|
-
- test/fixtures/orient_2.jpg
|
109
|
-
- test/fixtures/test.bmp
|
110
|
-
- test/fixtures/test.cur
|
111
|
-
- test/fixtures/test.gif
|
112
|
-
- test/fixtures/test.jpg
|
113
|
-
- test/fixtures/test.png
|
114
|
-
- test/fixtures/test.psd
|
115
|
-
- test/fixtures/test.svg
|
116
|
-
- test/fixtures/test.tiff
|
117
|
-
- test/fixtures/test2.bmp
|
118
|
-
- test/fixtures/test2.jpg
|
119
|
-
- test/fixtures/test2.tiff
|
120
|
-
- test/fixtures/test3.jpg
|
121
|
-
- test/fixtures/test4.jpg
|
122
|
-
- test/fixtures/test_partial_viewport.svg
|
123
|
-
- test/fixtures/truncated_gzipped.jpg
|
124
|
-
- test/fixtures/webp_vp8.webp
|
125
|
-
- test/fixtures/webp_vp8l.webp
|
126
|
-
- test/fixtures/webp_vp8x.webp
|
127
|
-
- test/test.rb
|
128
95
|
homepage: http://github.com/sdsykes/fastimage
|
129
96
|
licenses:
|
130
97
|
- MIT
|
@@ -150,5 +117,4 @@ rubygems_version: 2.4.5
|
|
150
117
|
signing_key:
|
151
118
|
specification_version: 4
|
152
119
|
summary: FastImage - Image info fast
|
153
|
-
test_files:
|
154
|
-
- test/test.rb
|
120
|
+
test_files: []
|
data/test/fixtures/bad.jpg
DELETED
Binary file
|
Binary file
|
data/test/fixtures/faulty.jpg
DELETED
Binary file
|
Binary file
|
data/test/fixtures/gzipped.jpg
DELETED
Binary file
|
data/test/fixtures/infinite.jpg
DELETED
Binary file
|
data/test/fixtures/man.ico
DELETED
Binary file
|
data/test/fixtures/orient_2.jpg
DELETED
Binary file
|
data/test/fixtures/test.bmp
DELETED
Binary file
|
data/test/fixtures/test.cur
DELETED
Binary file
|
data/test/fixtures/test.gif
DELETED
Binary file
|
data/test/fixtures/test.jpg
DELETED
Binary file
|
data/test/fixtures/test.png
DELETED
Binary file
|
data/test/fixtures/test.psd
DELETED
Binary file
|
data/test/fixtures/test.svg
DELETED
@@ -1,43 +0,0 @@
|
|
1
|
-
<?xml version="1.0"?>
|
2
|
-
|
3
|
-
<svg version="1.1"
|
4
|
-
xmlns="http://www.w3.org/2000/svg"
|
5
|
-
xmlns:xlink="http://www.w3.org/1999/xlink"
|
6
|
-
width="200" height="300"
|
7
|
-
style="background-color: #D2B48C;"
|
8
|
-
onload="loaded()">
|
9
|
-
<script type="text/javascript"><![CDATA[
|
10
|
-
function loaded() {
|
11
|
-
// change onloadFunc to point to your real onload function that you
|
12
|
-
// want called when the page is truly ready
|
13
|
-
var onloadFunc = doload;
|
14
|
-
|
15
|
-
if (top.svgweb) {
|
16
|
-
top.svgweb.addOnLoad(onloadFunc, true, window);
|
17
|
-
} else {
|
18
|
-
onloadFunc();
|
19
|
-
}
|
20
|
-
}
|
21
|
-
|
22
|
-
function doload() {
|
23
|
-
// developers original onload handler
|
24
|
-
|
25
|
-
// add an event listener to our circle; on* style events added right
|
26
|
-
// to the markup are not yet supported
|
27
|
-
var circle = document.getElementById('myCircle');
|
28
|
-
circle.addEventListener('mousedown', function(evt) {
|
29
|
-
alert('You pressed the mouse button on our circle: ' + evt.target.id);
|
30
|
-
}, false);
|
31
|
-
}
|
32
|
-
]]></script>
|
33
|
-
<g id="myGroup"
|
34
|
-
fill="blue"
|
35
|
-
style="font-size: 18px; text-anchor: middle; font-family: serif;">
|
36
|
-
<circle id="myCircle"
|
37
|
-
cx="100" cy="75" r="50"
|
38
|
-
stroke="firebrick"
|
39
|
-
stroke-width="3" />
|
40
|
-
<text x="100" y="155">Hello World</text>
|
41
|
-
<text x="100" y="175">From An SVG File!</text>
|
42
|
-
</g>
|
43
|
-
</svg>
|
data/test/fixtures/test.tiff
DELETED
Binary file
|
data/test/fixtures/test2.bmp
DELETED
Binary file
|
data/test/fixtures/test2.jpg
DELETED
Binary file
|
data/test/fixtures/test2.tiff
DELETED
Binary file
|
data/test/fixtures/test3.jpg
DELETED
Binary file
|
data/test/fixtures/test4.jpg
DELETED
Binary file
|
@@ -1,9 +0,0 @@
|
|
1
|
-
<svg xmlns="http://www.w3.org/2000/svg" version="1.0" width="860" viewBox="0 0 215 100">
|
2
|
-
<path d="M4.8 72.6C8.4 77.2 40.3 78.8 48 73.8 64.9 77.7 98.6 75.9 97.7 69.2 97 63.2 -16.4 46.1 4.8 72.6zM114.8 69.3C130.6 78.8 162 75.7 167.9 73.8 173.6 80.6 209.4 79.6 211.3 74.3 213.2 68.8 90.7 18.1 114.8 69.3z" style="fill:#cfcfcf"/>
|
3
|
-
<path d="M67.9 31.1C42.1 30.4 27.6 58.1 34.8 66.4 44.4 77.6 89.3 73.7 95.7 67.1 105.3 57.4 93.5 31.7 67.9 31.1z" style="fill:#cca776;stroke:#715835"/>
|
4
|
-
<path d="M25.7 43.9C2.8 43.8 -0.2 64.2 4.5 71.6 7.4 76.3 42.2 76.8 47.1 70.9 52.5 64.4 49 44 25.7 43.9z" style="fill:#ffe2af;stroke:#daa240"/>
|
5
|
-
<path d="M141.8 33.1C108.7 32.1 105.2 59.2 112.9 67.3 121.1 76 163.9 75.9 170.9 67.9 177.5 60.2 175.1 34.2 141.8 33.1z" style="fill:#a7dcc1;stroke:#5ba67e"/>
|
6
|
-
<path d="M188.8 43.6C166.1 43.1 160 64.7 165.9 71.8 171.6 78.6 203.4 78 209.3 72.3 215.6 66.1 211 44.1 188.8 43.6z" style="fill:#ffd1ba;stroke:#cc7f58"/>
|
7
|
-
<path d="M141.6 2.5C118.4 2 105.7 25 115 34.5 120.9 40.5 160.2 41.3 166.4 34.3 174.1 25.6 164.6 3 141.6 2.5z" style="fill:#fbf499;stroke:#cbc238"/>
|
8
|
-
<path d="M31.3 54.6C32.6 56.7 32.6 58.1 33 60.9M37.4 60.2C37.5 57.4 37 56.4 36 54.5M84.1 56.3C83.8 53.4 83.3 50.4 81.7 47.5M90.5 56.3C90.3 53.3 89.9 50.2 88.4 47.2M121.5 53C121.1 50.2 121.7 47.3 123 44.4M127.3 53.5C126.8 50.7 127.7 47.8 129.3 44.9M125.1 15.9C125.1 13.3 125.9 11.4 126.9 8.8M132.8 8.6C131.9 11 131 13.5 131.1 15.9M174.8 53.3C173.5 55.9 172.5 58.6 172.6 61.6M181.4 53.5C180 55.4 178.6 60.4 179.7 62.1" style="fill:none;stroke:#000000;stroke-width:1.25;stroke-linecap:round"/>
|
9
|
-
</svg>
|
Binary file
|
data/test/fixtures/webp_vp8.webp
DELETED
Binary file
|
Binary file
|
Binary file
|
data/test/test.rb
DELETED
@@ -1,323 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
|
3
|
-
require 'test/unit'
|
4
|
-
|
5
|
-
PathHere = File.dirname(__FILE__)
|
6
|
-
$LOAD_PATH.unshift File.join(PathHere, "..", "lib")
|
7
|
-
|
8
|
-
require 'fastimage'
|
9
|
-
require 'fakeweb'
|
10
|
-
|
11
|
-
FixturePath = File.join(PathHere, "fixtures")
|
12
|
-
|
13
|
-
GoodFixtures = {
|
14
|
-
"test.bmp"=>[:bmp, [40, 27]],
|
15
|
-
"test2.bmp"=>[:bmp, [1920, 1080]],
|
16
|
-
"test.gif"=>[:gif, [17, 32]],
|
17
|
-
"test.jpg"=>[:jpeg, [882, 470]],
|
18
|
-
"test.png"=>[:png, [30, 20]],
|
19
|
-
"test2.jpg"=>[:jpeg, [250, 188]],
|
20
|
-
"test3.jpg"=>[:jpeg, [630, 367]],
|
21
|
-
"test4.jpg"=>[:jpeg, [1485, 1299]],
|
22
|
-
"test.tiff"=>[:tiff, [85, 67]],
|
23
|
-
"test2.tiff"=>[:tiff, [333, 225]],
|
24
|
-
"test.psd"=>[:psd, [17, 32]],
|
25
|
-
"exif_orientation.jpg"=>[:jpeg, [600, 450]],
|
26
|
-
"infinite.jpg"=>[:jpeg, [160,240]],
|
27
|
-
"orient_2.jpg"=>[:jpeg, [230,408]],
|
28
|
-
"favicon.ico" => [:ico, [16, 16]],
|
29
|
-
"man.ico" => [:ico, [48, 48]],
|
30
|
-
"test.cur" => [:cur, [32, 32]],
|
31
|
-
"webp_vp8x.webp" => [:webp, [386, 395]],
|
32
|
-
"webp_vp8l.webp" => [:webp, [386, 395]],
|
33
|
-
"webp_vp8.webp" => [:webp, [550, 368]],
|
34
|
-
"test.svg" => [:svg, [200, 300]],
|
35
|
-
"test_partial_viewport.svg" => [:svg, [860, 400]]
|
36
|
-
}
|
37
|
-
|
38
|
-
BadFixtures = [
|
39
|
-
"faulty.jpg",
|
40
|
-
"test_rgb.ct",
|
41
|
-
"test.xml"
|
42
|
-
]
|
43
|
-
# man.ico courtesy of http://www.iconseeker.com/search-icon/artists-valley-sample/business-man-blue.html
|
44
|
-
# test_rgb.ct courtesy of http://fileformats.archiveteam.org/wiki/Scitex_CT
|
45
|
-
# test.cur courtesy of http://mimidestino.deviantart.com/art/Clash-Of-Clans-Dragon-Cursor-s-Punteros-489070897
|
46
|
-
|
47
|
-
TestUrl = "http://example.nowhere/"
|
48
|
-
|
49
|
-
# this image fetch allows me to really test that fastimage is truly fast
|
50
|
-
# but it's not ideal relying on external resources and connectivity speed
|
51
|
-
LargeImage = "http://upload.wikimedia.org/wikipedia/commons/b/b4/Mardin_1350660_1350692_33_images.jpg"
|
52
|
-
LargeImageInfo = [:jpeg, [9545, 6623]]
|
53
|
-
LargeImageFetchLimit = 2 # seconds
|
54
|
-
|
55
|
-
HTTPSImage = "https://upload.wikimedia.org/wikipedia/commons/b/b4/Mardin_1350660_1350692_33_images.jpg"
|
56
|
-
HTTPSImageInfo = [:jpeg, [9545, 6623]]
|
57
|
-
|
58
|
-
GoodFixtures.each do |fn, info|
|
59
|
-
FakeWeb.register_uri(:get, "#{TestUrl}#{fn}", :body => File.join(FixturePath, fn))
|
60
|
-
end
|
61
|
-
BadFixtures.each do |fn|
|
62
|
-
FakeWeb.register_uri(:get, "#{TestUrl}#{fn}", :body => File.join(FixturePath, fn))
|
63
|
-
end
|
64
|
-
|
65
|
-
GzipTestImg = "gzipped.jpg"
|
66
|
-
FakeWeb.register_uri(:get, "#{TestUrl}#{GzipTestImg}", :body => File.join(FixturePath, GzipTestImg), :content_encoding => "gzip")
|
67
|
-
GzipTestImgTruncated = "truncated_gzipped.jpg"
|
68
|
-
FakeWeb.register_uri(:get, "#{TestUrl}#{GzipTestImgTruncated}", :body => File.join(FixturePath, GzipTestImgTruncated), :content_encoding => "gzip")
|
69
|
-
GzipTestImgSize = [970, 450]
|
70
|
-
|
71
|
-
class FastImageTest < Test::Unit::TestCase
|
72
|
-
def test_should_report_type_correctly
|
73
|
-
GoodFixtures.each do |fn, info|
|
74
|
-
assert_equal info[0], FastImage.type(TestUrl + fn)
|
75
|
-
assert_equal info[0], FastImage.type(TestUrl + fn, :raise_on_failure=>true)
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
def test_should_report_size_correctly
|
80
|
-
GoodFixtures.each do |fn, info|
|
81
|
-
assert_equal info[1], FastImage.size(TestUrl + fn)
|
82
|
-
assert_equal info[1], FastImage.size(TestUrl + fn, :raise_on_failure=>true)
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
def test_should_return_nil_on_fetch_failure
|
87
|
-
assert_nil FastImage.size(TestUrl + "does_not_exist")
|
88
|
-
end
|
89
|
-
|
90
|
-
def test_should_return_nil_for_faulty_jpeg_where_size_cannot_be_found
|
91
|
-
assert_nil FastImage.size(TestUrl + "faulty.jpg")
|
92
|
-
end
|
93
|
-
|
94
|
-
def test_should_return_nil_when_image_type_not_known
|
95
|
-
assert_nil FastImage.size(TestUrl + "test_rgb.ct")
|
96
|
-
end
|
97
|
-
|
98
|
-
def test_should_return_nil_if_timeout_occurs
|
99
|
-
assert_nil FastImage.size("http://example.com/does_not_exist", :timeout=>0.001)
|
100
|
-
end
|
101
|
-
|
102
|
-
def test_should_raise_when_asked_to_when_size_cannot_be_found
|
103
|
-
assert_raises(FastImage::SizeNotFound) do
|
104
|
-
FastImage.size(TestUrl + "faulty.jpg", :raise_on_failure=>true)
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
def test_should_raise_when_asked_to_when_timeout_occurs
|
109
|
-
assert_raises(FastImage::ImageFetchFailure) do
|
110
|
-
FastImage.size("http://example.com/does_not_exist", :timeout=>0.001, :raise_on_failure=>true)
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
def test_should_raise_when_asked_to_when_file_does_not_exist
|
115
|
-
assert_raises(FastImage::ImageFetchFailure) do
|
116
|
-
FastImage.size("http://www.google.com/does_not_exist_at_all", :raise_on_failure=>true)
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
def test_should_raise_when_asked_when_image_type_not_known
|
121
|
-
assert_raises(FastImage::UnknownImageType) do
|
122
|
-
FastImage.size(TestUrl + "test_rgb.ct", :raise_on_failure=>true)
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
def test_should_raise_unknown_image_typ_when_file_is_non_svg_xml
|
127
|
-
assert_raises(FastImage::UnknownImageType) do
|
128
|
-
FastImage.size(TestUrl + "test.xml", :raise_on_failure=>true)
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
def test_should_report_type_correctly_for_local_files
|
133
|
-
GoodFixtures.each do |fn, info|
|
134
|
-
assert_equal info[0], FastImage.type(File.join(FixturePath, fn))
|
135
|
-
end
|
136
|
-
end
|
137
|
-
|
138
|
-
def test_should_report_size_correctly_for_local_files
|
139
|
-
GoodFixtures.each do |fn, info|
|
140
|
-
assert_equal info[1], FastImage.size(File.join(FixturePath, fn))
|
141
|
-
end
|
142
|
-
end
|
143
|
-
|
144
|
-
def test_should_report_type_correctly_for_ios
|
145
|
-
GoodFixtures.each do |fn, info|
|
146
|
-
File.open(File.join(FixturePath, fn), "r") do |io|
|
147
|
-
assert_equal info[0], FastImage.type(io)
|
148
|
-
end
|
149
|
-
end
|
150
|
-
end
|
151
|
-
|
152
|
-
def test_should_report_size_correctly_for_ios
|
153
|
-
GoodFixtures.each do |fn, info|
|
154
|
-
File.open(File.join(FixturePath, fn), "r") do |io|
|
155
|
-
assert_equal info[1], FastImage.size(io)
|
156
|
-
end
|
157
|
-
end
|
158
|
-
end
|
159
|
-
|
160
|
-
def test_should_report_size_correctly_on_io_object_twice
|
161
|
-
GoodFixtures.each do |fn, info|
|
162
|
-
File.open(File.join(FixturePath, fn), "r") do |io|
|
163
|
-
assert_equal info[1], FastImage.size(io)
|
164
|
-
assert_equal info[1], FastImage.size(io)
|
165
|
-
end
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
def test_should_report_size_correctly_for_local_files_with_path_that_has_spaces
|
170
|
-
Dir.chdir(PathHere) do
|
171
|
-
assert_equal GoodFixtures["test.bmp"][1], FastImage.size(File.join("fixtures", "folder with spaces", "test.bmp"))
|
172
|
-
end
|
173
|
-
end
|
174
|
-
|
175
|
-
def test_should_return_nil_on_fetch_failure_for_local_path
|
176
|
-
assert_nil FastImage.size("does_not_exist")
|
177
|
-
end
|
178
|
-
|
179
|
-
def test_should_return_nil_for_faulty_jpeg_where_size_cannot_be_found_for_local_file
|
180
|
-
assert_nil FastImage.size(File.join(FixturePath, "faulty.jpg"))
|
181
|
-
end
|
182
|
-
|
183
|
-
def test_should_return_nil_when_image_type_not_known_for_local_file
|
184
|
-
assert_nil FastImage.size(File.join(FixturePath, "test_rgb.ct"))
|
185
|
-
end
|
186
|
-
|
187
|
-
def test_should_raise_when_asked_to_when_size_cannot_be_found_for_local_file
|
188
|
-
assert_raises(FastImage::SizeNotFound) do
|
189
|
-
FastImage.size(File.join(FixturePath, "faulty.jpg"), :raise_on_failure=>true)
|
190
|
-
end
|
191
|
-
end
|
192
|
-
|
193
|
-
def test_should_handle_permanent_redirect
|
194
|
-
url = "http://example.com/foo.jpeg"
|
195
|
-
register_redirect(url, TestUrl + GoodFixtures.keys.first)
|
196
|
-
assert_equal GoodFixtures[GoodFixtures.keys.first][1], FastImage.size(url, :raise_on_failure=>true)
|
197
|
-
end
|
198
|
-
|
199
|
-
def test_should_handle_permanent_redirect_4_times
|
200
|
-
first_url = "http://example.com/foo.jpeg"
|
201
|
-
register_redirect(first_url, "http://example.com/foo2.jpeg")
|
202
|
-
register_redirect("http://example.com/foo2.jpeg", "http://example.com/foo3.jpeg")
|
203
|
-
register_redirect("http://example.com/foo3.jpeg", "http://example.com/foo4.jpeg")
|
204
|
-
register_redirect("http://example.com/foo4.jpeg", TestUrl + GoodFixtures.keys.first)
|
205
|
-
assert_equal GoodFixtures[GoodFixtures.keys.first][1], FastImage.size(first_url, :raise_on_failure=>true)
|
206
|
-
end
|
207
|
-
|
208
|
-
def test_should_raise_on_permanent_redirect_5_times
|
209
|
-
first_url = "http://example.com/foo.jpeg"
|
210
|
-
register_redirect(first_url, "http://example.com/foo2.jpeg")
|
211
|
-
register_redirect("http://example.com/foo2.jpeg", "http://example.com/foo3.jpeg")
|
212
|
-
register_redirect("http://example.com/foo3.jpeg", "http://example.com/foo4.jpeg")
|
213
|
-
register_redirect("http://example.com/foo4.jpeg", "http://example.com/foo5.jpeg")
|
214
|
-
register_redirect("http://example.com/foo5.jpeg", TestUrl + GoodFixtures.keys.first)
|
215
|
-
assert_raises(FastImage::ImageFetchFailure) do
|
216
|
-
FastImage.size(first_url, :raise_on_failure=>true)
|
217
|
-
end
|
218
|
-
end
|
219
|
-
|
220
|
-
def test_should_handle_permanent_redirect_with_relative_url
|
221
|
-
url = "http://example.nowhere/foo.jpeg"
|
222
|
-
register_redirect(url, "/" + GoodFixtures.keys.first)
|
223
|
-
assert_equal GoodFixtures[GoodFixtures.keys.first][1], FastImage.size(url, :raise_on_failure=>true)
|
224
|
-
end
|
225
|
-
|
226
|
-
def register_redirect(from, to)
|
227
|
-
resp = Net::HTTPMovedPermanently.new(1.0, 302, "Moved")
|
228
|
-
resp['Location'] = to
|
229
|
-
FakeWeb.register_uri(:get, from, :response=>resp)
|
230
|
-
end
|
231
|
-
|
232
|
-
def test_should_fetch_info_of_large_image_faster_than_downloading_the_whole_thing
|
233
|
-
time = Time.now
|
234
|
-
size = FastImage.size(LargeImage)
|
235
|
-
size_time = Time.now
|
236
|
-
assert size_time - time < LargeImageFetchLimit
|
237
|
-
assert_equal LargeImageInfo[1], size
|
238
|
-
time = Time.now
|
239
|
-
type = FastImage.type(LargeImage)
|
240
|
-
type_time = Time.now
|
241
|
-
assert type_time - time < LargeImageFetchLimit
|
242
|
-
assert_equal LargeImageInfo[0], type
|
243
|
-
end
|
244
|
-
|
245
|
-
# This test doesn't actually test the proxy function, but at least
|
246
|
-
# it excercises the code. You could put anything in the http_proxy and it would still pass.
|
247
|
-
# Any ideas on how to actually test this?
|
248
|
-
def test_should_fetch_via_proxy
|
249
|
-
file = "test.gif"
|
250
|
-
actual_size = GoodFixtures[file][1]
|
251
|
-
ENV['http_proxy'] = "http://my.proxy.host:8080"
|
252
|
-
size = FastImage.size(TestUrl + file)
|
253
|
-
ENV['http_proxy'] = nil
|
254
|
-
assert_equal actual_size, size
|
255
|
-
end
|
256
|
-
|
257
|
-
def test_should_fetch_via_proxy_option
|
258
|
-
file = "test.gif"
|
259
|
-
actual_size = GoodFixtures[file][1]
|
260
|
-
size = FastImage.size(TestUrl + file, :proxy => "http://my.proxy.host:8080")
|
261
|
-
assert_equal actual_size, size
|
262
|
-
end
|
263
|
-
|
264
|
-
def test_should_handle_https_image
|
265
|
-
size = FastImage.size(HTTPSImage)
|
266
|
-
assert_equal HTTPSImageInfo[1], size
|
267
|
-
end
|
268
|
-
|
269
|
-
require 'pathname'
|
270
|
-
def test_should_handle_pathname
|
271
|
-
# bad.jpg does not have the size info in the first 256 bytes
|
272
|
-
# so this tests if we are able to read past that using a
|
273
|
-
# Pathname (which has a different API from an IO).
|
274
|
-
path = Pathname.new(File.join(FixturePath, "bad.jpg"))
|
275
|
-
assert_equal([500,500], FastImage.size(path))
|
276
|
-
end
|
277
|
-
|
278
|
-
def test_should_report_type_and_size_correctly_for_stringios
|
279
|
-
GoodFixtures.each do |fn, info|
|
280
|
-
string = File.read(File.join(FixturePath, fn))
|
281
|
-
stringio = StringIO.new(string)
|
282
|
-
assert_equal info[0], FastImage.type(stringio)
|
283
|
-
assert_equal info[1], FastImage.size(stringio)
|
284
|
-
end
|
285
|
-
end
|
286
|
-
|
287
|
-
def test_gzipped_file
|
288
|
-
url = "http://example.nowhere/#{GzipTestImg}"
|
289
|
-
assert_equal([970, 450], FastImage.size(url))
|
290
|
-
end
|
291
|
-
|
292
|
-
def test_truncated_gzipped_file
|
293
|
-
url = "http://example.nowhere/#{GzipTestImgTruncated}"
|
294
|
-
assert_raises(FastImage::SizeNotFound) do
|
295
|
-
FastImage.size(url, :raise_on_failure => true)
|
296
|
-
end
|
297
|
-
end
|
298
|
-
|
299
|
-
def test_cant_access_shell
|
300
|
-
url = "|echo>shell_test"
|
301
|
-
%x{rm -f shell_test}
|
302
|
-
FastImage.size(url)
|
303
|
-
assert_raises(Errno::ENOENT) do
|
304
|
-
File.open("shell_test")
|
305
|
-
end
|
306
|
-
ensure
|
307
|
-
%x{rm -f shell_test}
|
308
|
-
end
|
309
|
-
|
310
|
-
def test_content_length
|
311
|
-
url = "#{TestUrl}with_content_length.gif"
|
312
|
-
FakeWeb.register_uri(:get, url, :body => File.join(FixturePath, "test.jpg"), :content_length => 52)
|
313
|
-
|
314
|
-
assert_equal 52, FastImage.new(url).content_length
|
315
|
-
end
|
316
|
-
|
317
|
-
def test_content_length_not_provided
|
318
|
-
url = "#{TestUrl}without_content_length.gif"
|
319
|
-
FakeWeb.register_uri(:get, url, :body => File.join(FixturePath, "test.jpg"))
|
320
|
-
|
321
|
-
assert_equal nil, FastImage.new(url).content_length
|
322
|
-
end
|
323
|
-
end
|