phocoder-rails 0.0.33

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.
Files changed (116) hide show
  1. data/.autotest +46 -0
  2. data/.document +5 -0
  3. data/.rspec +1 -0
  4. data/Gemfile +44 -0
  5. data/LICENSE.txt +20 -0
  6. data/MIT-LICENSE +20 -0
  7. data/README.rdoc +3 -0
  8. data/Rakefile +56 -0
  9. data/VERSION +5 -0
  10. data/app/controllers/phocoder_controller.rb +118 -0
  11. data/app/helpers/phocoder_helper.rb +320 -0
  12. data/app/models/encodable_job.rb +91 -0
  13. data/app/views/phocoder/_offline_video_embed.html.erb +19 -0
  14. data/app/views/phocoder/_thumbnail_update.html.erb +3 -0
  15. data/app/views/phocoder/_video_embed.html.erb +23 -0
  16. data/app/views/phocoder/multi_thumbnail_update.json.erb +7 -0
  17. data/app/views/phocoder/thumbnail_update.js.erb +9 -0
  18. data/config/routes.rb +8 -0
  19. data/lib/generators/phocoder_rails/model_update_generator.rb +52 -0
  20. data/lib/generators/phocoder_rails/scaffold_generator.rb +94 -0
  21. data/lib/generators/phocoder_rails/setup_generator.rb +33 -0
  22. data/lib/generators/phocoder_rails/templates/controller.rb +71 -0
  23. data/lib/generators/phocoder_rails/templates/helper.rb +5 -0
  24. data/lib/generators/phocoder_rails/templates/migration.rb +24 -0
  25. data/lib/generators/phocoder_rails/templates/model.rb +20 -0
  26. data/lib/generators/phocoder_rails/templates/model_migration.rb +56 -0
  27. data/lib/generators/phocoder_rails/templates/model_thumbnail.rb +5 -0
  28. data/lib/generators/phocoder_rails/templates/model_update_migration.rb +64 -0
  29. data/lib/generators/phocoder_rails/templates/phocodable.yml +28 -0
  30. data/lib/generators/phocoder_rails/templates/views/_form.html.erb.tt +23 -0
  31. data/lib/generators/phocoder_rails/templates/views/index.html.erb.tt +26 -0
  32. data/lib/generators/phocoder_rails/templates/views/new.html.erb.tt +5 -0
  33. data/lib/generators/phocoder_rails/templates/views/show.html.erb.tt +12 -0
  34. data/lib/phocoder_rails.rb +12 -0
  35. data/lib/phocoder_rails/acts_as_phocodable.rb +1153 -0
  36. data/lib/phocoder_rails/engine.rb +24 -0
  37. data/lib/phocoder_rails/errors.rb +46 -0
  38. data/phocoder-rails.gemspec +219 -0
  39. data/public/images/building.gif +0 -0
  40. data/public/images/error.png +0 -0
  41. data/public/images/play_small.png +0 -0
  42. data/public/images/storing.gif +0 -0
  43. data/public/images/waiting.gif +0 -0
  44. data/public/javascripts/phocodable.js +110 -0
  45. data/public/javascripts/video-js-2.0.2/.DS_Store +0 -0
  46. data/public/javascripts/video-js-2.0.2/LICENSE.txt +165 -0
  47. data/public/javascripts/video-js-2.0.2/README.markdown +202 -0
  48. data/public/javascripts/video-js-2.0.2/demo-subtitles.srt +13 -0
  49. data/public/javascripts/video-js-2.0.2/demo.html +101 -0
  50. data/public/javascripts/video-js-2.0.2/skins/hu.css +116 -0
  51. data/public/javascripts/video-js-2.0.2/skins/tube.css +111 -0
  52. data/public/javascripts/video-js-2.0.2/skins/vim.css +89 -0
  53. data/public/javascripts/video-js-2.0.2/video-js.css +242 -0
  54. data/public/javascripts/video-js-2.0.2/video.js +1758 -0
  55. data/public/stylesheets/phocodable.css +19 -0
  56. data/spec/controllers/phocoder_controller_spec.rb +123 -0
  57. data/spec/dummy/Rakefile +7 -0
  58. data/spec/dummy/app/controllers/application_controller.rb +7 -0
  59. data/spec/dummy/app/controllers/images_controller.rb +72 -0
  60. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  61. data/spec/dummy/app/helpers/images_helper.rb +5 -0
  62. data/spec/dummy/app/models/image.rb +20 -0
  63. data/spec/dummy/app/models/image_thumbnail.rb +5 -0
  64. data/spec/dummy/app/models/image_upload.rb +11 -0
  65. data/spec/dummy/app/views/images/_form.html.erb +23 -0
  66. data/spec/dummy/app/views/images/index.html.erb +26 -0
  67. data/spec/dummy/app/views/images/new.html.erb +5 -0
  68. data/spec/dummy/app/views/images/show.html.erb +12 -0
  69. data/spec/dummy/app/views/layouts/application.html.erb +18 -0
  70. data/spec/dummy/config.ru +4 -0
  71. data/spec/dummy/config/application.rb +45 -0
  72. data/spec/dummy/config/boot.rb +10 -0
  73. data/spec/dummy/config/database.yml +25 -0
  74. data/spec/dummy/config/environment.rb +8 -0
  75. data/spec/dummy/config/environments/development.rb +26 -0
  76. data/spec/dummy/config/environments/production.rb +49 -0
  77. data/spec/dummy/config/environments/test.rb +40 -0
  78. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  79. data/spec/dummy/config/initializers/inflections.rb +10 -0
  80. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  81. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  82. data/spec/dummy/config/initializers/session_store.rb +8 -0
  83. data/spec/dummy/config/locales/en.yml +5 -0
  84. data/spec/dummy/config/routes.rb +60 -0
  85. data/spec/dummy/db/migrate/001_create_image_uploads.rb +37 -0
  86. data/spec/dummy/db/migrate/20110523165213_add_parent_type_to_image_uploads.rb +11 -0
  87. data/spec/dummy/db/migrate/20110523165522_create_encodable_jobs.rb +24 -0
  88. data/spec/dummy/db/migrate/20111101024507_create_images.rb +56 -0
  89. data/spec/dummy/db/schema.rb +99 -0
  90. data/spec/dummy/public/404.html +26 -0
  91. data/spec/dummy/public/422.html +26 -0
  92. data/spec/dummy/public/500.html +26 -0
  93. data/spec/dummy/public/favicon.ico +0 -0
  94. data/spec/dummy/public/index.html +239 -0
  95. data/spec/dummy/public/javascripts/application.js +2 -0
  96. data/spec/dummy/public/javascripts/controls.js +965 -0
  97. data/spec/dummy/public/javascripts/dragdrop.js +974 -0
  98. data/spec/dummy/public/javascripts/effects.js +1123 -0
  99. data/spec/dummy/public/javascripts/jquery-1.6.4.js +9046 -0
  100. data/spec/dummy/public/javascripts/prototype.js +6001 -0
  101. data/spec/dummy/public/javascripts/rails.js +175 -0
  102. data/spec/dummy/public/stylesheets/.gitkeep +0 -0
  103. data/spec/dummy/script/rails +6 -0
  104. data/spec/engine_spec.rb +12 -0
  105. data/spec/fixtures/big_eye_tiny.jpg +0 -0
  106. data/spec/fixtures/octologo.png +0 -0
  107. data/spec/fixtures/test.txt +2 -0
  108. data/spec/fixtures/video-test.mov +0 -0
  109. data/spec/helpers/phocoder_helper_spec.rb +421 -0
  110. data/spec/integration/navigation_spec.rb +10 -0
  111. data/spec/models/acts_as_phocodable_spec.rb +664 -0
  112. data/spec/models/encodable_job_spec.rb +50 -0
  113. data/spec/phocoder_rails_spec.rb +8 -0
  114. data/spec/routing/phocoder_routing_spec.rb +19 -0
  115. data/spec/spec_helper.rb +75 -0
  116. metadata +375 -0
@@ -0,0 +1,10 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+
4
+ describe "Navigation" do
5
+ include Capybara
6
+
7
+ it "should be a valid app" do
8
+ ::Rails.application.should be_a(Dummy::Application)
9
+ end
10
+ end
@@ -0,0 +1,664 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ include ActionDispatch::TestProcess
4
+
5
+
6
+
7
+
8
+ #dummy migration for testing.
9
+ #class TestMigration < ActiveRecord::Migration
10
+ # def self.up
11
+ # create_table :images, :force => true do |t|
12
+ # t.string "filename"
13
+ # t.datetime "created_at"
14
+ # t.datetime "updated_at"
15
+ # t.string "content_type"
16
+ # t.integer "phocoder_job_id"
17
+ # t.integer "phocoder_input_id"
18
+ # t.integer "phocoder_output_id"
19
+ # t.integer "width"
20
+ # t.integer "height"
21
+ # t.integer "file_size"
22
+ # t.string "thumbnail"
23
+ # t.integer "parent_id"
24
+ # t.string "status"
25
+ # t.string "upload_host"
26
+ # end
27
+ # end
28
+ #
29
+ # def self.down
30
+ # drop_table :images
31
+ # end
32
+ #end
33
+ #
34
+ ##dummy class that can become phocodeable
35
+ #class Image < ActiveRecord::Base
36
+ # acts_as_phocodable
37
+ #end
38
+
39
+
40
+ describe ActsAsPhocodable do
41
+
42
+ # the ImageUpload class in the dummy app
43
+ # is wired up with acts_as_phocodable
44
+
45
+ #before(:all){ TestMigration.up }
46
+ #after(:all){ TestMigration.up }
47
+ before(:each) do
48
+ # @attr = {
49
+ # :file => ActionDispatch::Http::UploadedFile.new(
50
+ # :tempfile=> Rack::Test::UploadedFile.new(fixture_path + '/big_eye_tiny.jpg', 'image/jpeg'),
51
+ # :filename=>"big_eye_tiny.jpg"
52
+ # )
53
+ # }
54
+ ImageUpload.destroy_all
55
+ @attr = {
56
+ :file => fixture_file_upload(fixture_path + '/big_eye_tiny.jpg','image/jpeg')
57
+ }
58
+ @vid_attr = {
59
+ :file => fixture_file_upload(fixture_path + '/video-test.mov', 'video/quicktime')
60
+ }
61
+ @txt_attr = {
62
+ :file => fixture_file_upload(fixture_path + '/test.txt', 'text/plain')
63
+ }
64
+ end
65
+
66
+
67
+
68
+
69
+ it "should default into local mode" do
70
+ ActsAsPhocodable.storeage_mode.should == "local"
71
+ end
72
+
73
+ it "should be able to go into offline mode" do
74
+ ActsAsPhocodable.storeage_mode = "offline"
75
+ ActsAsPhocodable.storeage_mode.should == "offline"
76
+ end
77
+
78
+ it "should default into automatic processing mode" do
79
+ ActsAsPhocodable.processing_mode.should == "automatic"
80
+ end
81
+
82
+
83
+ it "should be able to go into offline mode" do
84
+ ActsAsPhocodable.processing_mode = "resque"
85
+ ActsAsPhocodable.processing_mode.should == "resque"
86
+ #reset for later tests
87
+ ActsAsPhocodable.processing_mode = "automatic"
88
+ end
89
+
90
+ it "should default to the url in the config" do
91
+ ActsAsPhocodable.base_url.should == "http://actsasphocodableexample.chaos.webapeel.com"
92
+ end
93
+
94
+ it "should take a new base_url" do
95
+ ActsAsPhocodable.base_url = "http://new-domain.com"
96
+ ActsAsPhocodable.base_url.should == "http://new-domain.com"
97
+ end
98
+
99
+
100
+ it "should default to the normal config file" do
101
+ ActsAsPhocodable.config_file.should == "config/phocodable.yml"
102
+ end
103
+
104
+ it "should take a new config file" do
105
+ ActsAsPhocodable.config_file = "new_config/phocodable.yml"
106
+ ActsAsPhocodable.config_file.should == "new_config/phocodable.yml"
107
+ #reset it so we don't screw up other tests
108
+ ActsAsPhocodable.config_file = "config/phocodable.yml"
109
+ ActsAsPhocodable.config_file.should == "config/phocodable.yml"
110
+ end
111
+
112
+
113
+ it "should read the config file" do
114
+ ActsAsPhocodable.config_file == "config/phocodable.yml"
115
+ iu = ImageUpload.new
116
+ iu.phocodable_config.should_not be_nil
117
+ end
118
+
119
+
120
+ it "should read actual configs" do
121
+ ActsAsPhocodable.config_file == "config/phocodable.yml"
122
+ iu = ImageUpload.new()
123
+ iu.phocodable_config.should_not be_nil
124
+ # these values are based on a config in spec/dummy/config/phocodable.yml
125
+ # it is currently excluded from git since it contains an API key
126
+ # this will fail for any one other than me.
127
+ # what to do?
128
+ iu.phocodable_config[:phocoder_url].should == "http://photoapi.chaos.webapeel.com"
129
+ iu.phocodable_config[:base_url].should == "http://actsasphocodableexample.chaos.webapeel.com"
130
+ end
131
+
132
+
133
+ describe "create_label_from_size_string" do
134
+ it "should transform 50x50! => 50x50_crop" do
135
+ ImageUpload.create_label_from_size_string("50x50!").should == "50x50crop"
136
+ end
137
+ it "should transform 50x50> => 50x50_preserve" do
138
+ ImageUpload.create_label_from_size_string("50x50>").should == "50x50preserve"
139
+ end
140
+ end
141
+
142
+ describe "create_atts_from_size_string" do
143
+ it "should return the right hash" do
144
+ atts = ImageUpload.create_atts_from_size_string("100x200crop")
145
+ atts[:width].should == "100"
146
+ atts[:height].should == "200"
147
+ atts[:aspect_mode].should == "crop"
148
+ end
149
+ end
150
+
151
+
152
+ it "should create phocoder params based on the acts_as_phocodable :thumbnail options" do
153
+ iu = ImageUpload.new(@attr)
154
+ phorams = iu.phocoder_params
155
+ #puts phorams.to_json
156
+ phorams.should_not be_nil
157
+ phorams[:input][:url].should == iu.public_url
158
+ phorams[:thumbnails].size.should == 2
159
+ end
160
+
161
+ it "should create phocoder params based on input thumbs that are passed in" do
162
+ iu = ImageUpload.new(@attr)
163
+ phorams = iu.phocoder_params([{:label=>"test",:width=>60,:height=>60}])
164
+ #puts phorams.to_json
165
+ phorams.should_not be_nil
166
+ phorams[:input][:url].should == iu.public_url
167
+ phorams[:thumbnails].size.should == 1
168
+ end
169
+
170
+
171
+ it "should create zencooder params based on the acts_as_phocodable :videos options" do
172
+ iu = ImageUpload.new(@vid_attr)
173
+ zenrams = iu.zencoder_params
174
+ puts zenrams.to_json
175
+ zenrams.should_not be_nil
176
+ zenrams[:input].should == iu.public_url
177
+ zenrams[:outputs].size.should == 2
178
+ #the first thumbnail variant :thumbnails => { :number => 2, ...}
179
+ zenrams[:outputs][0][:thumbnails][:base_url].should be_nil
180
+ #the second thumbnail variant :thumbnails => [{...},{...}]
181
+ #we removed this for now - only 1 thumb is being generated, automatically
182
+ #zenrams[:outputs][1][:thumbnails][0][:base_url].should be_nil
183
+ end
184
+
185
+
186
+ it "should create zencooder params with base_urls in s3 mode based on the acts_as_phocodable :videos options" do
187
+ ActsAsPhocodable.storeage_mode = "s3"
188
+ iu = ImageUpload.new(@vid_attr)
189
+ zenrams = iu.zencoder_params
190
+ puts zenrams.to_json
191
+ zenrams.should_not be_nil
192
+ zenrams[:input].should == iu.public_url
193
+ zenrams[:outputs].size.should == 2
194
+ #the first thumbnail variant :thumbnails => { :number => 2, ...}
195
+ zenrams[:outputs][0][:thumbnails][:base_url].should_not be_nil
196
+ #the second thumbnail variant :thumbnails => [{...},{...}]
197
+ #we removed this for now - only 1 thumb is being generated, automatically
198
+ #zenrams[:outputs][1][:thumbnails][0][:base_url].should_not be_nil
199
+ ActsAsPhocodable.storeage_mode = "local"
200
+ end
201
+
202
+ it "should return some thumbnail options" do
203
+ ImageUpload.phocoder_thumbnails.should_not be_nil
204
+ ImageUpload.phocoder_thumbnails.size.should == 2
205
+ end
206
+
207
+ it "should return attributes for a specific thumbnail" do
208
+ ImageUpload.thumbnail_attributes_for("small").should_not be_nil
209
+ end
210
+
211
+
212
+
213
+ it "should be phocodable" do
214
+ @image = ImageUpload.new
215
+ @image.phocodable?.should be_true
216
+ end
217
+
218
+ it "should get the right file name" do
219
+ iu = ImageUpload.new(@attr)
220
+ iu.filename.should == "big_eye_tiny.jpg"
221
+ end
222
+
223
+ it "should save the file to a local storage location" do
224
+ iu = ImageUpload.new(@txt_attr)
225
+ Phocoder::Job.should_not_receive(:create)
226
+ iu.save
227
+
228
+ expected_resource_dir = "image_uploads/1"
229
+ iu.resource_dir.should == expected_resource_dir
230
+
231
+ expected_local_url = "/image_uploads/1/test.txt"
232
+ iu.local_url.should == expected_local_url
233
+
234
+ expected_local_path = File.join('/tmp','image_uploads',iu.id.to_s,iu.filename)
235
+ iu.local_path.should == expected_local_path
236
+ iu.local_url.should == "/image_uploads/#{iu.id}/#{iu.filename}"
237
+ File.exists?(expected_local_path).should be_true
238
+ iu.destroy
239
+ File.exists?(expected_local_path).should_not be_true
240
+ end
241
+ #
242
+ it "should call phocoder for images" do
243
+ iu = ImageUpload.new(@attr)
244
+
245
+ Phocoder::Job.should_receive(:create).and_return(mock(Phocoder::Response,:body=>{
246
+ "job"=>{
247
+ "id"=>1,
248
+ "inputs"=>["id"=>1],
249
+ "thumbnails"=>[{"label"=>"small","filename"=>"small-test-file.jpg","id"=>1}]
250
+ }
251
+ }))
252
+ iu.save
253
+ expected_local_path = File.join('/tmp','image_uploads',iu.id.to_s,iu.filename)
254
+ File.exists?(expected_local_path).should be_true
255
+ #iu.phocode
256
+ ImageUpload.count.should == 2 #it should have created a thumbnail record
257
+ iu.destroy
258
+ ImageUpload.count.should == 0
259
+ File.exists?(expected_local_path).should_not be_true
260
+ end
261
+
262
+
263
+ it "should call phocoder for images and be able to add new thumbs" do
264
+ iu = ImageUpload.new(@attr)
265
+
266
+ Phocoder::Job.should_receive(:create).and_return(mock(Phocoder::Response,:body=>{
267
+ "job"=>{
268
+ "id"=>1,
269
+ "inputs"=>["id"=>1],
270
+ "thumbnails"=>[{"label"=>"small","filename"=>"small-test-file.jpg","id"=>1}]
271
+ }
272
+ }))
273
+ iu.save
274
+ expected_local_path = File.join('/tmp','image_uploads',iu.id.to_s,iu.filename)
275
+ File.exists?(expected_local_path).should be_true
276
+ # phocode is called after save in this mode
277
+ #iu.phocode
278
+ ImageUpload.count.should == 2 #it should have created a thumbnail record
279
+
280
+ Phocoder::Job.should_receive(:create).and_return(mock(Phocoder::Response,:body=>{
281
+ "job"=>{
282
+ "id"=>2,
283
+ "inputs"=>["id"=>2],
284
+ "thumbnails"=>[{"label"=>"test","filename"=>"test-test-file.jpg","id"=>2}]
285
+ }
286
+ }))
287
+
288
+ iu.clear_phocoding
289
+ iu.phocode([{:label=>"test",:width=>60,:height=>60}])
290
+ ImageUpload.count.should == 3
291
+
292
+ # now to make sure that it doesn't try to recode exsiting thumbs
293
+ iu.clear_phocoding
294
+ iu.phocode([{:label=>"test",:width=>60,:height=>60}])
295
+ ImageUpload.count.should == 3
296
+
297
+
298
+ iu.destroy
299
+ ImageUpload.count.should == 0
300
+ File.exists?(expected_local_path).should_not be_true
301
+ end
302
+
303
+
304
+
305
+ it "should call phocoder for images and be able to add new thumbs on the fly" do
306
+ iu = ImageUpload.new(@attr)
307
+
308
+ Phocoder::Job.should_receive(:create).and_return(mock(Phocoder::Response,:body=>{
309
+ "job"=>{
310
+ "id"=>1,
311
+ "inputs"=>["id"=>1],
312
+ "thumbnails"=>[{"label"=>"small","filename"=>"small-test-file.jpg","id"=>1}]
313
+ }
314
+ }))
315
+ iu.save
316
+ expected_local_path = File.join('/tmp','image_uploads',iu.id.to_s,iu.filename)
317
+ File.exists?(expected_local_path).should be_true
318
+ # phocode is called after save in this mode
319
+ #iu.phocode
320
+ ImageUpload.count.should == 2 #it should have created a thumbnail record
321
+
322
+
323
+ #this thumbnail doesn't exist yet
324
+ lambda{
325
+ nt = iu.thumbnail_for("new_test")
326
+ }.should raise_error(Exception)
327
+
328
+
329
+ Phocoder::Job.should_receive(:create).and_return(mock(Phocoder::Response,:body=>{
330
+ "job"=>{
331
+ "id"=>2,
332
+ "inputs"=>["id"=>2],
333
+ "thumbnails"=>[{"label"=>"anothertest","filename"=>"test-test-file.jpg","id"=>2}]
334
+ }
335
+ }))
336
+
337
+ iu.clear_phocoding
338
+ at = iu.thumbnail_for({:label=>"anothertest",:width=>60,:height=>60})
339
+ at.should_not be_nil
340
+ ImageUpload.count.should == 3
341
+
342
+ # now to make sure that it doesn't try to recode exsiting thumbs
343
+ iu.clear_phocoding
344
+ at = iu.thumbnail_for({:label=>"anothertest",:width=>60,:height=>60})
345
+ at.should_not be_nil
346
+ ImageUpload.count.should == 3
347
+
348
+
349
+ iu.destroy
350
+ ImageUpload.count.should == 0
351
+ File.exists?(expected_local_path).should_not be_true
352
+ end
353
+
354
+
355
+
356
+ it "should call phocoder for images and be able to add new thumbs on the fly with no label" do
357
+ iu = ImageUpload.new(@attr)
358
+
359
+ Phocoder::Job.should_receive(:create).and_return(mock(Phocoder::Response,:body=>{
360
+ "job"=>{
361
+ "id"=>1,
362
+ "inputs"=>["id"=>1],
363
+ "thumbnails"=>[{"label"=>"small","filename"=>"small-test-file.jpg","id"=>1}]
364
+ }
365
+ }))
366
+ iu.save
367
+ expected_local_path = File.join('/tmp','image_uploads',iu.id.to_s,iu.filename)
368
+ File.exists?(expected_local_path).should be_true
369
+ # phocode is called after save in this mode
370
+ #iu.phocode
371
+ ImageUpload.count.should == 2 #it should have created a thumbnail record
372
+
373
+
374
+ #this thumbnail doesn't exist yet
375
+ lambda{
376
+ nt = iu.thumbnail_for("new_test")
377
+ }.should raise_error(Exception)
378
+
379
+
380
+ Phocoder::Job.should_receive(:create).and_return(mock(Phocoder::Response,:body=>{
381
+ "job"=>{
382
+ "id"=>2,
383
+ "inputs"=>["id"=>2],
384
+ "thumbnails"=>[{"label"=>"60x60","filename"=>"test-test-file.jpg","id"=>2}]
385
+ }
386
+ }))
387
+
388
+ iu.clear_phocoding
389
+ at = iu.thumbnail_for({:width=>60,:height=>60})
390
+ at.should_not be_nil
391
+ ImageUpload.count.should == 3
392
+
393
+ # now to make sure that it doesn't try to recode exsiting thumbs
394
+ iu.clear_phocoding
395
+ at = iu.thumbnail_for({:width=>60,:height=>60})
396
+ at.should_not be_nil
397
+ ImageUpload.count.should == 3
398
+
399
+ iu.destroy
400
+ ImageUpload.count.should == 0
401
+ File.exists?(expected_local_path).should_not be_true
402
+ end
403
+
404
+
405
+ it "should call phocoder for images and be able to add new thumbs on the fly with just a size string label" do
406
+ iu = ImageUpload.new(@attr)
407
+
408
+ Phocoder::Job.should_receive(:create).and_return(mock(Phocoder::Response,:body=>{
409
+ "job"=>{
410
+ "id"=>1,
411
+ "inputs"=>["id"=>1],
412
+ "thumbnails"=>[{"label"=>"small","filename"=>"small-test-file.jpg","id"=>1}]
413
+ }
414
+ }))
415
+ iu.save
416
+ expected_local_path = File.join('/tmp','image_uploads',iu.id.to_s,iu.filename)
417
+ File.exists?(expected_local_path).should be_true
418
+ # phocode is called after save in this mode
419
+ #iu.phocode
420
+ ImageUpload.count.should == 2 #it should have created a thumbnail record
421
+
422
+ #this thumbnail doesn't exist yet
423
+ lambda{
424
+ nt = iu.thumbnail_for("new_test")
425
+ }.should raise_error(Exception)
426
+
427
+
428
+ Phocoder::Job.should_receive(:create).and_return(mock(Phocoder::Response,:body=>{
429
+ "job"=>{
430
+ "id"=>2,
431
+ "inputs"=>["id"=>2],
432
+ "thumbnails"=>[{"label"=>"60x60","filename"=>"test-test-file.jpg","id"=>2}]
433
+ }
434
+ }))
435
+
436
+ iu.clear_phocoding
437
+ at = iu.thumbnail_for("60x60")
438
+ at.should_not be_nil
439
+ ImageUpload.count.should == 3
440
+
441
+ Phocoder::Job.should_receive(:create).and_return(mock(Phocoder::Response,:body=>{
442
+ "job"=>{
443
+ "id"=>2,
444
+ "inputs"=>["id"=>2],
445
+ "thumbnails"=>[{"label"=>"80x","filename"=>"test-test-file.jpg","id"=>3}]
446
+ }
447
+ }))
448
+
449
+ iu.clear_phocoding
450
+ at = iu.thumbnail_for("80x")
451
+ at.should_not be_nil
452
+ ImageUpload.count.should == 4
453
+
454
+ Phocoder::Job.should_receive(:create).and_return(mock(Phocoder::Response,:body=>{
455
+ "job"=>{
456
+ "id"=>2,
457
+ "inputs"=>["id"=>2],
458
+ "thumbnails"=>[{"label"=>"x90","filename"=>"test-test-file.jpg","id"=>4}]
459
+ }
460
+ }))
461
+
462
+ iu.clear_phocoding
463
+ at = iu.thumbnail_for("x90")
464
+ at.should_not be_nil
465
+ ImageUpload.count.should == 5
466
+
467
+ Phocoder::Job.should_receive(:create).and_return(mock(Phocoder::Response,:body=>{
468
+ "job"=>{
469
+ "id"=>2,
470
+ "inputs"=>["id"=>2],
471
+ "thumbnails"=>[{"label"=>"","filename"=>"test.jpg","id"=>5}]
472
+ }
473
+ }))
474
+
475
+ iu.clear_phocoding
476
+ at = iu.thumbnail_for("100x100!")
477
+ at.should_not be_nil
478
+ ImageUpload.count.should == 6
479
+
480
+ iu.destroy
481
+ ImageUpload.count.should == 0
482
+ File.exists?(expected_local_path).should_not be_true
483
+ end
484
+
485
+
486
+
487
+
488
+ it "should call zencoder for videos" do
489
+ iu = ImageUpload.new(@vid_attr)
490
+ Zencoder::Job.should_receive(:create).and_return(mock(Zencoder::Response,:body=>{
491
+ "id"=>1,
492
+ "inputs"=>["id"=>1],
493
+ "outputs"=>[{"label"=>"mp4","url"=>"http://someurl/","filename"=>"small-test-file.mp4","id"=>1}]
494
+ }))
495
+ #iu.should_receive(:create_zencoder_image_thumb).and_return(nil)
496
+ iu.save
497
+ expected_local_path = File.join('/tmp','image_uploads',iu.id.to_s,iu.filename)
498
+ File.exists?(expected_local_path).should be_true
499
+ #iu.phocode
500
+ ImageUpload.count.should == 2 #it should have created a thumbnail record
501
+
502
+ #iu.reload #make sure it knows aobut the thumbnail
503
+ iu.destroy
504
+ puts "ImageUpload.first = #{ImageUpload.first.to_json}"
505
+ ImageUpload.count.should == 0
506
+ File.exists?(expected_local_path).should_not be_true
507
+ end
508
+
509
+
510
+
511
+
512
+ it "should update parent images from phocoder" do
513
+ iu = ImageUpload.new(@attr.merge :phocoder_input_id=>1)
514
+ Phocoder::Job.stub!(:create).and_return(mock(Phocoder::Response,:body=>{
515
+ "job"=>{
516
+ "id"=>1,
517
+ "inputs"=>["id"=>1],
518
+ "thumbnails"=>[{"label"=>"small","filename"=>"small-test-file.jpg","id"=>1}]
519
+ }
520
+ }))
521
+ iu.save
522
+ ImageUpload.update_from_phocoder({:input=>{:id=>1,:width=>10,:height=>20,:file_size=>30}})
523
+ iu.reload
524
+ iu.width.should == 10
525
+ iu.height.should == 20
526
+ iu.file_size.should == 30
527
+ iu.phocoder_status.should == "ready"
528
+ iu.destroy
529
+ end
530
+
531
+ it "should update thumbnail images from phocoder and donwload images in local mode" do
532
+ ActsAsPhocodable.storeage_mode = "local"
533
+ iu = ImageUpload.new(@attr.merge :phocoder_input_id=>1)
534
+ Phocoder::Job.stub!(:create).and_return(mock(Phocoder::Response,:body=>{
535
+ "job"=>{
536
+ "id"=>1,
537
+ "inputs"=>["id"=>1],
538
+ "thumbnails"=>[] # don't auto create thumbs since we'll make one below
539
+ }
540
+ }))
541
+ iu.save
542
+ thumb = iu.thumbnails.new(:phocoder_output_id=>1)
543
+ #Phocoder::Job.stub!(:create).and_return(mock(Phocoder::Response,:body=>{
544
+ # "job"=>{
545
+ # "id"=>1,
546
+ # "inputs"=>["id"=>1],
547
+ # "thumbnails"=>[]
548
+ # }
549
+ #}))
550
+ thumb.save
551
+
552
+ stub_request(:get, "http://production.webapeel.com/octolabs/themes/octolabs/images/octologo.png").
553
+ with(:headers => {'Accept'=>'*/*'}).
554
+ to_return(:status => 200, :body => webmock_file("octologo.png"), :headers => {})
555
+
556
+
557
+ ImageUpload.update_from_phocoder({:output=>{:id=>1,:width=>10,:height=>20,:file_size=>30,:url=>"http://production.webapeel.com/octolabs/themes/octolabs/images/octologo.png" }})
558
+
559
+ thumb.reload
560
+ #expected_local_path = File.expand_path(File.join(File.dirname(File.expand_path(__FILE__)),'..','dummy','public','ImageUpload',iu.id.to_s,thumb.filename))
561
+ puts "thumb.local_path = #{thumb.local_path} - #{thumb.id}"
562
+ File.exists?(thumb.local_path).should be_true
563
+ thumb.width.should == 10
564
+ thumb.height.should == 20
565
+ thumb.file_size.should == 30
566
+ thumb.filename.should == "octologo.png"
567
+ thumb.phocoder_status.should == "ready"
568
+ thumb.destroy
569
+ File.exists?(thumb.local_path).should_not be_true
570
+ iu.destroy
571
+ end
572
+
573
+
574
+ it "in delayed s3 mode it should save the file to an AWS S3 storage location, call phocoder, then destroy" do
575
+ ActsAsPhocodable.storeage_mode = "s3"
576
+ ActsAsPhocodable.processing_mode = "delayed"
577
+ ImageUpload.establish_aws_connection
578
+
579
+ iu = ImageUpload.new(@attr)
580
+ Phocoder::Job.stub!(:create).and_return(mock(Phocoder::Response,:body=>{
581
+ "job"=>{
582
+ "id"=>1,
583
+ "inputs"=>[{"id"=>1}],
584
+ "thumbnails"=>[{"label"=>"small","filename"=>"small-test-thumbnail.jpg","id"=>1}]
585
+ }
586
+ }))
587
+
588
+ #This save will work since we're in delayed mode
589
+ iu.save
590
+ # No thumbnail should be created yet
591
+ iu.thumbnails.size.should == 0
592
+ # Mock the AWS reqeust for storing
593
+ AWS::S3::S3Object.should_receive(:store).and_return(nil)
594
+ # Mock the AWS request for checking file size
595
+ AWS::S3::S3Object.should_receive(:find).and_return( mock(:size => 19494) )
596
+
597
+ iu.save_s3_file
598
+ # Now we should have a thumb
599
+ iu.thumbnails.size.should == 1
600
+ # Mock the AWS reqeust for deleting the file and it's thumbnail
601
+ AWS::S3::S3Object.should_receive(:delete).twice.and_return(nil)
602
+ iu.destroy
603
+ end
604
+
605
+
606
+ it "in automatic s3 mode it should save the file to an AWS S3 storage location, call phocoder, then destroy" do
607
+ ActsAsPhocodable.storeage_mode = "s3"
608
+ ActsAsPhocodable.processing_mode = "automatic"
609
+ ImageUpload.establish_aws_connection
610
+ puts "======================================="
611
+ iu = ImageUpload.new(@attr)
612
+ Phocoder::Job.stub!(:create).and_return(mock(Phocoder::Response,:body=>{
613
+ "job"=>{
614
+ "id"=>1,
615
+ "inputs"=>[{"id"=>1}],
616
+ "thumbnails"=>[{"label"=>"small","filename"=>"small-test-file.jpg","id"=>1}]
617
+ }
618
+ }))
619
+
620
+ # Mock the AWS reqeust for storing
621
+ AWS::S3::S3Object.should_receive(:store).and_return(nil)
622
+ # Mock the AWS request for checking file size
623
+ AWS::S3::S3Object.should_receive(:find).and_return( mock(:size => 19494) )
624
+ #now store in S3 + phocode
625
+ iu.save
626
+ puts "======================================="
627
+ puts iu.thumbnails.to_json
628
+ iu.thumbnails.size.should == 1
629
+
630
+ iu.destroy
631
+ end
632
+
633
+
634
+ it "in spawn s3 mode it should save the file to an AWS S3 storage location, call phocoder, then destroy" do
635
+ # In testing we use the spawn :yield strategy, which is really just the same as
636
+ # automatic (inline) processing. It works for testing, and hopefully works in production.
637
+ # Hopefully...
638
+ ActsAsPhocodable.storeage_mode = "s3"
639
+ ActsAsPhocodable.processing_mode = "spawn"
640
+ ImageUpload.establish_aws_connection
641
+
642
+ iu = ImageUpload.new(@attr)
643
+ Phocoder::Job.stub!(:create).and_return(mock(Phocoder::Response,:body=>{
644
+ "job"=>{
645
+ "id"=>1,
646
+ "inputs"=>[{"id"=>1}],
647
+ "thumbnails"=>[{"label"=>"small","filename"=>"small-test-file.jpg","id"=>1}]
648
+ }
649
+ }))
650
+
651
+ # Mock the AWS reqeust for storing
652
+ AWS::S3::S3Object.should_receive(:store).and_return(nil)
653
+ # Mock the AWS request for checking file size
654
+ AWS::S3::S3Object.should_receive(:find).and_return( mock(:size => 19494) )
655
+
656
+ #now store in S3 + phocode
657
+ iu.save
658
+ iu.thumbnails.size.should == 1
659
+
660
+ iu.destroy
661
+ end
662
+
663
+
664
+ end