diff --git a/app/models/jobs/process_photo.rb b/app/models/jobs/process_photo.rb index 014620e91d1777453dfd207424c7543e7c3d87c5..048e13c88fb676fbbacfc5a4fe3d7eee9c5f947a 100644 --- a/app/models/jobs/process_photo.rb +++ b/app/models/jobs/process_photo.rb @@ -6,8 +6,15 @@ module Jobs class ProcessPhoto < Base @queue = :photos - def self.perform(photo_id) - Photo.find(photo_id).process + def self.perform(id) + photo = Photo.find(id) + unprocessed_image = photo.unprocessed_image + + return false if photo.processed? || unprocessed_image.path.try(:include?, ".gif") + + photo.processed_image.store!(unprocessed_image) + + photo.save! end end end diff --git a/app/models/photo.rb b/app/models/photo.rb index 2062cdf0cbb9a508da35041079ff2e78b98d0914..75b644276c0d9d0a3fbf617038de8f70109a5f81 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -48,12 +48,8 @@ class Photo < Post photo end - def not_processed? - processed_image.path.nil? - end - def processed? - !processed_image.path.nil? + processed_image.path.present? end def update_remote_path @@ -74,10 +70,10 @@ class Photo < Post if remote_photo_path name = name.to_s + '_' if name remote_photo_path + name.to_s + remote_photo_name - elsif not_processed? - unprocessed_image.url(name) - else + elsif processed? processed_image.url(name) + else + unprocessed_image.url(name) end end @@ -97,12 +93,6 @@ class Photo < Post Resque.enqueue(Jobs::ProcessPhoto, self.id) end - def process - return false if self.processed? || (!unprocessed_image.path.nil? && unprocessed_image.path.include?('.gif')) - processed_image.store!(unprocessed_image) #Ultra naive - save! - end - def mutable? true end diff --git a/spec/factories.rb b/spec/factories.rb index 017ccaad67981ea33019d76749cf771880a500b9..74578cf6ecb341eece1f2ac89e6174ce8c7076c9 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -86,7 +86,6 @@ Factory.define(:photo) do |p| p.sequence(:random_string) {|n| ActiveSupport::SecureRandom.hex(10) } p.after_build do |p| p.unprocessed_image.store! File.open(File.join(File.dirname(__FILE__), 'fixtures', 'button.png')) - p.process p.update_remote_path end end diff --git a/spec/fixtures/button.gif b/spec/fixtures/button.gif new file mode 100644 index 0000000000000000000000000000000000000000..71ef2d9f42d821ef14615f3876572a0884deb832 Binary files /dev/null and b/spec/fixtures/button.gif differ diff --git a/spec/models/jobs/process_photo_spec.rb b/spec/models/jobs/process_photo_spec.rb index 674aea8dfd3bea5554139c18a5621392e7ade513..0a760f0d154d81cf07890da99af7dbdfea8abf65 100644 --- a/spec/models/jobs/process_photo_spec.rb +++ b/spec/models/jobs/process_photo_spec.rb @@ -2,9 +2,63 @@ require 'spec_helper' describe Jobs::ProcessPhoto do it 'calls process on the photo' do - photo = mock() - photo.should_receive(:process) - Photo.should_receive(:find).with(1).and_return(photo) - Jobs::ProcessPhoto.perform(1) + #photo = mock() + #photo.should_receive(:process) + #Photo.should_receive(:find).with(1).and_return(photo) + #Jobs::ProcessPhoto.perform(1) + end + + before do + @user = alice + @aspect = @user.aspects.first + + @fixture_name = File.join(File.dirname(__FILE__), '..', '..', 'fixtures', 'button.png') + + @saved_photo = @user.build_post(:photo, :user_file => File.open(@fixture_name), :to => @aspect.id) + @saved_photo.save + end + + it 'saves the processed image' do + @saved_photo.processed_image.path.should be_nil + + result = Jobs::ProcessPhoto.perform(@saved_photo.id) + + @saved_photo.reload + + @saved_photo.processed_image.path.should_not be_nil + result.should be true + end + + context 'when trying to process a photo that has already been processed' do + before do + Jobs::ProcessPhoto.perform(@saved_photo.id) + @saved_photo.reload + end + + it 'does not process the photo' do + processed_image_path = @saved_photo.processed_image.path + + result = Jobs::ProcessPhoto.perform(@saved_photo.id) + + @saved_photo.reload + + @saved_photo.processed_image.path.should == processed_image_path + result.should be false + end + end + + context 'when a gif is uploaded' do + before do + @fixture_name = File.join(File.dirname(__FILE__), '..', '..', 'fixtures', 'button.gif') + @saved_gif = @user.build_post(:photo, :user_file => File.open(@fixture_name), :to => @aspect.id) + @saved_gif.save + end + + it 'does not process the gif' do + result = Jobs::ProcessPhoto.perform(@saved_gif.id) + + @saved_gif.reload.processed_image.path.should be_nil + result.should be false + end end end diff --git a/spec/models/photo_spec.rb b/spec/models/photo_spec.rb index d506c52b2a063851a264420b417c1e4d3356343c..92e331254a466c57a95772f44c66c900b067d664 100644 --- a/spec/models/photo_spec.rb +++ b/spec/models/photo_spec.rb @@ -13,13 +13,10 @@ describe Photo do @fixture_name = File.join(File.dirname(__FILE__), '..', 'fixtures', @fixture_filename) @fail_fixture_name = File.join(File.dirname(__FILE__), '..', 'fixtures', 'msg.xml') - @photo = @user.build_post(:photo, :user_file=> File.open(@fixture_name), :to => @aspect.id) - @photo2 = @user.build_post(:photo, :user_file=> File.open(@fixture_name), :to => @aspect.id) - end - - - describe "#process" do - it "should do something awesome" + @photo = @user.build_post(:photo, :user_file => File.open(@fixture_name), :to => @aspect.id) + @photo2 = @user.build_post(:photo, :user_file => File.open(@fixture_name), :to => @aspect.id) + @saved_photo = @user.build_post(:photo, :user_file => File.open(@fixture_name), :to => @aspect.id) + @saved_photo.save end describe "protected attributes" do @@ -136,38 +133,41 @@ describe Photo do describe 'serialization' do before do - @photo.process - @photo.save! - @xml = @photo.to_xml.to_s + Jobs::ProcessPhoto.perform(@saved_photo.id) + @xml = @saved_photo.to_xml.to_s end + it 'serializes the url' do - @xml.include?(@photo.remote_photo_path).should be true - @xml.include?(@photo.remote_photo_name).should be true + @xml.include?(@saved_photo.remote_photo_path).should be true + @xml.include?(@saved_photo.remote_photo_name).should be true end + it 'serializes the diaspora_handle' do @xml.include?(@user.diaspora_handle).should be true end end describe 'remote photos' do + before do + Jobs::ProcessPhoto.perform(@saved_photo.id) + end + it 'should set the remote_photo on marshalling' do - @photo.process - @photo.save! #security hax user2 = Factory.create(:user) aspect2 = user2.aspects.create(:name => "foobars") connect_users(@user, @aspect, user2, aspect2) - url = @photo.url - thumb_url = @photo.url :thumb_medium + url = @saved_photo.url + thumb_url = @saved_photo.url :thumb_medium - xml = @photo.to_diaspora_xml + xml = @saved_photo.to_diaspora_xml - @photo.destroy + @saved_photo.destroy zord = Postzord::Receiver::Private.new(user2, :person => @photo.author) zord.parse_and_receive(xml) - new_photo = Photo.where(:guid => @photo.guid).first + new_photo = Photo.where(:guid => @saved_photo.guid).first new_photo.url.nil?.should be false new_photo.url.include?(url).should be true new_photo.url(:thumb_medium).include?(thumb_url).should be true