moved photo processing from model to resque job, added tests
removed Photo#not_processed
This commit is contained in:
parent
da851881a7
commit
3eaa1c0584
6 changed files with 90 additions and 40 deletions
|
|
@ -6,8 +6,15 @@
|
||||||
module Jobs
|
module Jobs
|
||||||
class ProcessPhoto < Base
|
class ProcessPhoto < Base
|
||||||
@queue = :photos
|
@queue = :photos
|
||||||
def self.perform(photo_id)
|
def self.perform(id)
|
||||||
Photo.find(photo_id).process
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -48,12 +48,8 @@ class Photo < Post
|
||||||
photo
|
photo
|
||||||
end
|
end
|
||||||
|
|
||||||
def not_processed?
|
|
||||||
processed_image.path.nil?
|
|
||||||
end
|
|
||||||
|
|
||||||
def processed?
|
def processed?
|
||||||
!processed_image.path.nil?
|
processed_image.path.present?
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_remote_path
|
def update_remote_path
|
||||||
|
|
@ -74,10 +70,10 @@ class Photo < Post
|
||||||
if remote_photo_path
|
if remote_photo_path
|
||||||
name = name.to_s + '_' if name
|
name = name.to_s + '_' if name
|
||||||
remote_photo_path + name.to_s + remote_photo_name
|
remote_photo_path + name.to_s + remote_photo_name
|
||||||
elsif not_processed?
|
elsif processed?
|
||||||
unprocessed_image.url(name)
|
|
||||||
else
|
|
||||||
processed_image.url(name)
|
processed_image.url(name)
|
||||||
|
else
|
||||||
|
unprocessed_image.url(name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -97,12 +93,6 @@ class Photo < Post
|
||||||
Resque.enqueue(Jobs::ProcessPhoto, self.id)
|
Resque.enqueue(Jobs::ProcessPhoto, self.id)
|
||||||
end
|
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?
|
def mutable?
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -86,7 +86,6 @@ Factory.define(:photo) do |p|
|
||||||
p.sequence(:random_string) {|n| ActiveSupport::SecureRandom.hex(10) }
|
p.sequence(:random_string) {|n| ActiveSupport::SecureRandom.hex(10) }
|
||||||
p.after_build do |p|
|
p.after_build do |p|
|
||||||
p.unprocessed_image.store! File.open(File.join(File.dirname(__FILE__), 'fixtures', 'button.png'))
|
p.unprocessed_image.store! File.open(File.join(File.dirname(__FILE__), 'fixtures', 'button.png'))
|
||||||
p.process
|
|
||||||
p.update_remote_path
|
p.update_remote_path
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
BIN
spec/fixtures/button.gif
vendored
Normal file
BIN
spec/fixtures/button.gif
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 235 B |
|
|
@ -2,9 +2,63 @@ require 'spec_helper'
|
||||||
|
|
||||||
describe Jobs::ProcessPhoto do
|
describe Jobs::ProcessPhoto do
|
||||||
it 'calls process on the photo' do
|
it 'calls process on the photo' do
|
||||||
photo = mock()
|
#photo = mock()
|
||||||
photo.should_receive(:process)
|
#photo.should_receive(:process)
|
||||||
Photo.should_receive(:find).with(1).and_return(photo)
|
#Photo.should_receive(:find).with(1).and_return(photo)
|
||||||
Jobs::ProcessPhoto.perform(1)
|
#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
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -13,13 +13,10 @@ describe Photo do
|
||||||
@fixture_name = File.join(File.dirname(__FILE__), '..', 'fixtures', @fixture_filename)
|
@fixture_name = File.join(File.dirname(__FILE__), '..', 'fixtures', @fixture_filename)
|
||||||
@fail_fixture_name = File.join(File.dirname(__FILE__), '..', 'fixtures', 'msg.xml')
|
@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)
|
@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)
|
@photo2 = @user.build_post(:photo, :user_file => File.open(@fixture_name), :to => @aspect.id)
|
||||||
end
|
@saved_photo = @user.build_post(:photo, :user_file => File.open(@fixture_name), :to => @aspect.id)
|
||||||
|
@saved_photo.save
|
||||||
|
|
||||||
describe "#process" do
|
|
||||||
it "should do something awesome"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "protected attributes" do
|
describe "protected attributes" do
|
||||||
|
|
@ -136,38 +133,41 @@ describe Photo do
|
||||||
|
|
||||||
describe 'serialization' do
|
describe 'serialization' do
|
||||||
before do
|
before do
|
||||||
@photo.process
|
Jobs::ProcessPhoto.perform(@saved_photo.id)
|
||||||
@photo.save!
|
@xml = @saved_photo.to_xml.to_s
|
||||||
@xml = @photo.to_xml.to_s
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'serializes the url' do
|
it 'serializes the url' do
|
||||||
@xml.include?(@photo.remote_photo_path).should be true
|
@xml.include?(@saved_photo.remote_photo_path).should be true
|
||||||
@xml.include?(@photo.remote_photo_name).should be true
|
@xml.include?(@saved_photo.remote_photo_name).should be true
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'serializes the diaspora_handle' do
|
it 'serializes the diaspora_handle' do
|
||||||
@xml.include?(@user.diaspora_handle).should be true
|
@xml.include?(@user.diaspora_handle).should be true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'remote photos' do
|
describe 'remote photos' do
|
||||||
|
before do
|
||||||
|
Jobs::ProcessPhoto.perform(@saved_photo.id)
|
||||||
|
end
|
||||||
|
|
||||||
it 'should set the remote_photo on marshalling' do
|
it 'should set the remote_photo on marshalling' do
|
||||||
@photo.process
|
|
||||||
@photo.save!
|
|
||||||
#security hax
|
#security hax
|
||||||
user2 = Factory.create(:user)
|
user2 = Factory.create(:user)
|
||||||
aspect2 = user2.aspects.create(:name => "foobars")
|
aspect2 = user2.aspects.create(:name => "foobars")
|
||||||
connect_users(@user, @aspect, user2, aspect2)
|
connect_users(@user, @aspect, user2, aspect2)
|
||||||
|
|
||||||
url = @photo.url
|
url = @saved_photo.url
|
||||||
thumb_url = @photo.url :thumb_medium
|
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 = Postzord::Receiver::Private.new(user2, :person => @photo.author)
|
||||||
zord.parse_and_receive(xml)
|
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.nil?.should be false
|
||||||
new_photo.url.include?(url).should be true
|
new_photo.url.include?(url).should be true
|
||||||
new_photo.url(:thumb_medium).include?(thumb_url).should be true
|
new_photo.url(:thumb_medium).include?(thumb_url).should be true
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue