diff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rb index 192502d4ae0..7ee2ec65ebf 100644 --- a/lib/cooked_post_processor.rb +++ b/lib/cooked_post_processor.rb @@ -36,6 +36,8 @@ class CookedPostProcessor # retrieve the associated upload, if any upload = get_upload_from_url(img['src']) if upload.present? + # update reverse index + associate_to_post upload # create a thumbnail upload.create_thumbnail! # optimize image @@ -87,6 +89,13 @@ class CookedPostProcessor end end + def associate_to_post(upload) + return if PostUpload.where(post_id: @post.id, upload_id: upload.id).count > 0 + PostUpload.create({ post_id: @post.id, upload_id: upload.id }) + rescue ActiveRecord::RecordNotUnique + # do not care if it's already associated + end + def optimize_image(img) return img["src"] # 1) optimize using image_optim diff --git a/spec/components/cooked_post_processor_spec.rb b/spec/components/cooked_post_processor_spec.rb index 8371999a685..6ace798c320 100644 --- a/spec/components/cooked_post_processor_spec.rb +++ b/spec/components/cooked_post_processor_spec.rb @@ -57,6 +57,23 @@ describe CookedPostProcessor do end + context 'with uploaded images in the post' do + before do + @topic = Fabricate(:topic) + @post = Fabricate(:post_with_uploads, topic: @topic, user: @topic.user) + @cpp = CookedPostProcessor.new(@post) + @cpp.expects(:get_upload_from_url).returns(Fabricate(:upload)) + @cpp.expects(:get_size).returns([100,200]) + end + + it "keeps reverse index up to date" do + @cpp.post_process_images + @post.uploads.reload + @post.uploads.count.should == 1 + end + + end + context 'with unsized images in the post' do let(:user) { Fabricate(:user) } let(:topic) { Fabricate(:topic, user: user) } diff --git a/spec/fabricators/post_fabricator.rb b/spec/fabricators/post_fabricator.rb index 80919d57889..132a942c559 100644 --- a/spec/fabricators/post_fabricator.rb +++ b/spec/fabricators/post_fabricator.rb @@ -40,6 +40,13 @@ Fabricator(:post_with_s3_image_url, from: :post) do " end +Fabricator(:post_with_uploads, from: :post) do + cooked " + + " +end + + Fabricator(:basic_reply, from: :post) do user(:coding_horror) reply_to_post_number 1 diff --git a/spec/fabricators/upload_fabricator.rb b/spec/fabricators/upload_fabricator.rb index 1435dc11c9e..16fed88372a 100644 --- a/spec/fabricators/upload_fabricator.rb +++ b/spec/fabricators/upload_fabricator.rb @@ -4,5 +4,5 @@ Fabricator(:upload) do filesize 1234 width 100 height 200 - url "/uploads/default/123456789.jpg" + url "/uploads/default/1/1234567890123456.jpg" end