fastimage_discourse 1.6.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,67 @@
1
+ # Poor Man's Fiber (API compatible Thread based Fiber implementation for Ruby 1.8)
2
+ # (c) 2008 Aman Gupta (tmm1)
3
+
4
+ unless defined? Fiber
5
+ require 'thread'
6
+
7
+ class FiberError < StandardError; # :nodoc:
8
+ end
9
+
10
+ class Fiber # :nodoc:
11
+ def initialize
12
+ raise ArgumentError, 'new Fiber requires a block' unless block_given?
13
+
14
+ @yield = Queue.new
15
+ @resume = Queue.new
16
+
17
+ @thread = Thread.new{ @yield.push [yield(*@resume.pop)] }
18
+ @thread.abort_on_exception = true
19
+ @thread[:fiber] = self
20
+ end
21
+ attr_reader :thread
22
+
23
+ def resume *args
24
+ raise FiberError, 'dead fiber called' unless @thread.alive?
25
+ @resume.push(args)
26
+ result = @yield.pop
27
+ result.size > 1 ? result : result.first
28
+ end
29
+
30
+ def yield *args
31
+ @yield.push(args)
32
+ result = @resume.pop
33
+ result.size > 1 ? result : result.first
34
+ end
35
+
36
+ def self.yield *args
37
+ if fiber = Thread.current[:fiber]
38
+ fiber.yield(*args)
39
+ else
40
+ raise FiberError, 'not inside a fiber'
41
+ end
42
+ end
43
+
44
+ def self.current
45
+ if Thread.current == Thread.main
46
+ return Thread.main[:fiber] ||= RootFiber.new
47
+ end
48
+
49
+ Thread.current[:fiber] or raise FiberError, 'not inside a fiber'
50
+ end
51
+
52
+ def inspect
53
+ "#<#{self.class}:0x#{self.object_id.to_s(16)}>"
54
+ end
55
+ end
56
+
57
+ class RootFiber < Fiber # :nodoc:
58
+ def initialize
59
+ # XXX: what is a root fiber anyway?
60
+ end
61
+
62
+ def self.yield *args
63
+ raise FiberError, "can't yield from root fiber"
64
+ end
65
+ end
66
+ end
67
+
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,288 @@
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
+ }
35
+
36
+ BadFixtures = [
37
+ "faulty.jpg",
38
+ "test_rgb.ct"
39
+ ]
40
+ # man.ico courtesy of http://www.iconseeker.com/search-icon/artists-valley-sample/business-man-blue.html
41
+ # test_rgb.ct courtesy of http://fileformats.archiveteam.org/wiki/Scitex_CT
42
+ # test.cur courtesy of http://mimidestino.deviantart.com/art/Clash-Of-Clans-Dragon-Cursor-s-Punteros-489070897
43
+
44
+ TestUrl = "http://example.nowhere/"
45
+
46
+ # this image fetch allows me to really test that fastimage is truly fast
47
+ # but it's not ideal relying on external resources and connectivity speed
48
+ LargeImage = "http://upload.wikimedia.org/wikipedia/commons/b/b4/Mardin_1350660_1350692_33_images.jpg"
49
+ LargeImageInfo = [:jpeg, [9545, 6623]]
50
+ LargeImageFetchLimit = 2 # seconds
51
+
52
+ HTTPSImage = "https://upload.wikimedia.org/wikipedia/commons/b/b4/Mardin_1350660_1350692_33_images.jpg"
53
+ HTTPSImageInfo = [:jpeg, [9545, 6623]]
54
+
55
+ GoodFixtures.each do |fn, info|
56
+ FakeWeb.register_uri(:get, "#{TestUrl}#{fn}", :body => File.join(FixturePath, fn))
57
+ end
58
+ BadFixtures.each do |fn|
59
+ FakeWeb.register_uri(:get, "#{TestUrl}#{fn}", :body => File.join(FixturePath, fn))
60
+ end
61
+
62
+ GzipTestImg = "gzipped.jpg"
63
+ FakeWeb.register_uri(:get, "#{TestUrl}#{GzipTestImg}", :body => File.join(FixturePath, GzipTestImg), :content_encoding => "gzip")
64
+ GzipTestImgTruncated = "truncated_gzipped.jpg"
65
+ FakeWeb.register_uri(:get, "#{TestUrl}#{GzipTestImgTruncated}", :body => File.join(FixturePath, GzipTestImgTruncated), :content_encoding => "gzip")
66
+ GzipTestImgSize = [970, 450]
67
+
68
+ class FastImageTest < Test::Unit::TestCase
69
+ def test_should_report_type_correctly
70
+ GoodFixtures.each do |fn, info|
71
+ assert_equal info[0], FastImage.type(TestUrl + fn)
72
+ assert_equal info[0], FastImage.type(TestUrl + fn, :raise_on_failure=>true)
73
+ end
74
+ end
75
+
76
+ def test_should_report_size_correctly
77
+ GoodFixtures.each do |fn, info|
78
+ assert_equal info[1], FastImage.size(TestUrl + fn)
79
+ assert_equal info[1], FastImage.size(TestUrl + fn, :raise_on_failure=>true)
80
+ end
81
+ end
82
+
83
+ def test_should_return_nil_on_fetch_failure
84
+ assert_nil FastImage.size(TestUrl + "does_not_exist")
85
+ end
86
+
87
+ def test_should_return_nil_for_faulty_jpeg_where_size_cannot_be_found
88
+ assert_nil FastImage.size(TestUrl + "faulty.jpg")
89
+ end
90
+
91
+ def test_should_return_nil_when_image_type_not_known
92
+ assert_nil FastImage.size(TestUrl + "test_rgb.ct")
93
+ end
94
+
95
+ def test_should_return_nil_if_timeout_occurs
96
+ assert_nil FastImage.size("http://example.com/does_not_exist", :timeout=>0.001)
97
+ end
98
+
99
+ def test_should_raise_when_asked_to_when_size_cannot_be_found
100
+ assert_raises(FastImage::SizeNotFound) do
101
+ FastImage.size(TestUrl + "faulty.jpg", :raise_on_failure=>true)
102
+ end
103
+ end
104
+
105
+ def test_should_raise_when_asked_to_when_timeout_occurs
106
+ assert_raises(FastImage::ImageFetchFailure) do
107
+ FastImage.size("http://example.com/does_not_exist", :timeout=>0.001, :raise_on_failure=>true)
108
+ end
109
+ end
110
+
111
+ def test_should_raise_when_asked_to_when_file_does_not_exist
112
+ assert_raises(FastImage::ImageFetchFailure) do
113
+ FastImage.size("http://www.google.com/does_not_exist_at_all", :raise_on_failure=>true)
114
+ end
115
+ end
116
+
117
+ def test_should_raise_when_asked_when_image_type_not_known
118
+ assert_raises(FastImage::UnknownImageType) do
119
+ FastImage.size(TestUrl + "test_rgb.ct", :raise_on_failure=>true)
120
+ end
121
+ end
122
+
123
+ def test_should_report_type_correctly_for_local_files
124
+ GoodFixtures.each do |fn, info|
125
+ assert_equal info[0], FastImage.type(File.join(FixturePath, fn))
126
+ end
127
+ end
128
+
129
+ def test_should_report_size_correctly_for_local_files
130
+ GoodFixtures.each do |fn, info|
131
+ assert_equal info[1], FastImage.size(File.join(FixturePath, fn))
132
+ end
133
+ end
134
+
135
+ def test_should_report_type_correctly_for_ios
136
+ GoodFixtures.each do |fn, info|
137
+ File.open(File.join(FixturePath, fn), "r") do |io|
138
+ assert_equal info[0], FastImage.type(io)
139
+ end
140
+ end
141
+ end
142
+
143
+ def test_should_report_size_correctly_for_ios
144
+ GoodFixtures.each do |fn, info|
145
+ File.open(File.join(FixturePath, fn), "r") do |io|
146
+ assert_equal info[1], FastImage.size(io)
147
+ end
148
+ end
149
+ end
150
+
151
+ def test_should_report_size_correctly_on_io_object_twice
152
+ GoodFixtures.each do |fn, info|
153
+ File.open(File.join(FixturePath, fn), "r") do |io|
154
+ assert_equal info[1], FastImage.size(io)
155
+ assert_equal info[1], FastImage.size(io)
156
+ end
157
+ end
158
+ end
159
+
160
+ def test_should_report_size_correctly_for_local_files_with_path_that_has_spaces
161
+ Dir.chdir(PathHere) do
162
+ assert_equal GoodFixtures["test.bmp"][1], FastImage.size(File.join("fixtures", "folder with spaces", "test.bmp"))
163
+ end
164
+ end
165
+
166
+ def test_should_return_nil_on_fetch_failure_for_local_path
167
+ assert_nil FastImage.size("does_not_exist")
168
+ end
169
+
170
+ def test_should_return_nil_for_faulty_jpeg_where_size_cannot_be_found_for_local_file
171
+ assert_nil FastImage.size(File.join(FixturePath, "faulty.jpg"))
172
+ end
173
+
174
+ def test_should_return_nil_when_image_type_not_known_for_local_file
175
+ assert_nil FastImage.size(File.join(FixturePath, "test_rgb.ct"))
176
+ end
177
+
178
+ def test_should_raise_when_asked_to_when_size_cannot_be_found_for_local_file
179
+ assert_raises(FastImage::SizeNotFound) do
180
+ FastImage.size(File.join(FixturePath, "faulty.jpg"), :raise_on_failure=>true)
181
+ end
182
+ end
183
+
184
+ def test_should_handle_permanent_redirect
185
+ url = "http://example.com/foo.jpeg"
186
+ register_redirect(url, TestUrl + GoodFixtures.keys.first)
187
+ assert_equal GoodFixtures[GoodFixtures.keys.first][1], FastImage.size(url, :raise_on_failure=>true)
188
+ end
189
+
190
+ def test_should_handle_permanent_redirect_4_times
191
+ first_url = "http://example.com/foo.jpeg"
192
+ register_redirect(first_url, "http://example.com/foo2.jpeg")
193
+ register_redirect("http://example.com/foo2.jpeg", "http://example.com/foo3.jpeg")
194
+ register_redirect("http://example.com/foo3.jpeg", "http://example.com/foo4.jpeg")
195
+ register_redirect("http://example.com/foo4.jpeg", TestUrl + GoodFixtures.keys.first)
196
+ assert_equal GoodFixtures[GoodFixtures.keys.first][1], FastImage.size(first_url, :raise_on_failure=>true)
197
+ end
198
+
199
+ def test_should_raise_on_permanent_redirect_5_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", "http://example.com/foo5.jpeg")
205
+ register_redirect("http://example.com/foo5.jpeg", TestUrl + GoodFixtures.keys.first)
206
+ assert_raises(FastImage::ImageFetchFailure) do
207
+ FastImage.size(first_url, :raise_on_failure=>true)
208
+ end
209
+ end
210
+
211
+ def test_should_handle_permanent_redirect_with_relative_url
212
+ url = "http://example.nowhere/foo.jpeg"
213
+ register_redirect(url, "/" + GoodFixtures.keys.first)
214
+ assert_equal GoodFixtures[GoodFixtures.keys.first][1], FastImage.size(url, :raise_on_failure=>true)
215
+ end
216
+
217
+ def register_redirect(from, to)
218
+ resp = Net::HTTPMovedPermanently.new(1.0, 302, "Moved")
219
+ resp['Location'] = to
220
+ FakeWeb.register_uri(:get, from, :response=>resp)
221
+ end
222
+
223
+ def test_should_fetch_info_of_large_image_faster_than_downloading_the_whole_thing
224
+ time = Time.now
225
+ size = FastImage.size(LargeImage)
226
+ size_time = Time.now
227
+ assert size_time - time < LargeImageFetchLimit
228
+ assert_equal LargeImageInfo[1], size
229
+ time = Time.now
230
+ type = FastImage.type(LargeImage)
231
+ type_time = Time.now
232
+ assert type_time - time < LargeImageFetchLimit
233
+ assert_equal LargeImageInfo[0], type
234
+ end
235
+
236
+ # This test doesn't actually test the proxy function, but at least
237
+ # it excercises the code. You could put anything in the http_proxy and it would still pass.
238
+ # Any ideas on how to actually test this?
239
+ def test_should_fetch_via_proxy
240
+ file = "test.gif"
241
+ actual_size = GoodFixtures[file][1]
242
+ ENV['http_proxy'] = "http://my.proxy.host:8080"
243
+ size = FastImage.size(TestUrl + file)
244
+ ENV['http_proxy'] = nil
245
+ assert_equal actual_size, size
246
+ end
247
+
248
+ def test_should_handle_https_image
249
+ size = FastImage.size(HTTPSImage)
250
+ assert_equal HTTPSImageInfo[1], size
251
+ end
252
+
253
+ require 'pathname'
254
+ def test_should_handle_pathname
255
+ # bad.jpg does not have the size info in the first 256 bytes
256
+ # so this tests if we are able to read past that using a
257
+ # Pathname (which has a different API from an IO).
258
+ path = Pathname.new(File.join(FixturePath, "bad.jpg"))
259
+ assert_equal([500,500], FastImage.size(path))
260
+ end
261
+
262
+ def test_should_report_type_and_size_correctly_for_stringios
263
+ GoodFixtures.each do |fn, info|
264
+ string = File.read(File.join(FixturePath, fn))
265
+ stringio = StringIO.new(string)
266
+ assert_equal info[0], FastImage.type(stringio)
267
+ assert_equal info[1], FastImage.size(stringio)
268
+ end
269
+ end
270
+
271
+ def test_gzipped_file
272
+ url = "http://example.nowhere/#{GzipTestImg}"
273
+ assert_equal([970, 450], FastImage.size(url))
274
+ end
275
+
276
+ def test_truncated_gzipped_file
277
+ url = "http://example.nowhere/#{GzipTestImgTruncated}"
278
+ assert_raises(FastImage::SizeNotFound) do
279
+ FastImage.size(url, :raise_on_failure => true)
280
+ end
281
+ end
282
+ end
283
+
284
+
285
+ # repeat all tests with addressable
286
+ FastImage.use_addressable_uri_parser
287
+ class FastImageTestAddressable < FastImageTest
288
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fastimage_discourse
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.6.6
5
+ platform: ruby
6
+ authors:
7
+ - Stephen Sykes
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: addressable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.3'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.3.5
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '2.3'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 2.3.5
33
+ - !ruby/object:Gem::Dependency
34
+ name: fakeweb
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.3'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.3'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rdoc
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ description: FastImage finds the size or type of an image given its uri by fetching
76
+ as little as needed.
77
+ email: sdsykes@gmail.com
78
+ executables: []
79
+ extensions: []
80
+ extra_rdoc_files:
81
+ - README.textile
82
+ files:
83
+ - MIT-LICENSE
84
+ - README.textile
85
+ - lib/fastimage.rb
86
+ - lib/fastimage/fbr.rb
87
+ - test/fixtures/bad.jpg
88
+ - test/fixtures/exif_orientation.jpg
89
+ - test/fixtures/faulty.jpg
90
+ - test/fixtures/folder with spaces/test.bmp
91
+ - test/fixtures/gzipped.jpg
92
+ - test/fixtures/infinite.jpg
93
+ - test/fixtures/man.ico
94
+ - test/fixtures/orient_2.jpg
95
+ - test/fixtures/test.bmp
96
+ - test/fixtures/test.cur
97
+ - test/fixtures/test.gif
98
+ - test/fixtures/test.jpg
99
+ - test/fixtures/test.png
100
+ - test/fixtures/test.psd
101
+ - test/fixtures/test.tiff
102
+ - test/fixtures/test2.bmp
103
+ - test/fixtures/test2.jpg
104
+ - test/fixtures/test2.tiff
105
+ - test/fixtures/test3.jpg
106
+ - test/fixtures/test4.jpg
107
+ - test/fixtures/truncated_gzipped.jpg
108
+ - test/fixtures/webp_vp8.webp
109
+ - test/fixtures/webp_vp8l.webp
110
+ - test/fixtures/webp_vp8x.webp
111
+ - test/test.rb
112
+ homepage: http://github.com/sdsykes/fastimage
113
+ licenses:
114
+ - MIT
115
+ metadata: {}
116
+ post_install_message:
117
+ rdoc_options:
118
+ - "--charset=UTF-8"
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 2.2.2
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: FastImage - Image info fast
137
+ test_files:
138
+ - test/test.rb