keep uploads reverse index up to date

This commit is contained in:
Régis Hanol 2013-06-17 22:46:48 +02:00
parent ae3543872c
commit 7bdc616040
4 changed files with 34 additions and 1 deletions

View File

@ -36,6 +36,8 @@ class CookedPostProcessor
# retrieve the associated upload, if any # retrieve the associated upload, if any
upload = get_upload_from_url(img['src']) upload = get_upload_from_url(img['src'])
if upload.present? if upload.present?
# update reverse index
associate_to_post upload
# create a thumbnail # create a thumbnail
upload.create_thumbnail! upload.create_thumbnail!
# optimize image # optimize image
@ -87,6 +89,13 @@ class CookedPostProcessor
end end
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) def optimize_image(img)
return img["src"] return img["src"]
# 1) optimize using image_optim # 1) optimize using image_optim

View File

@ -57,6 +57,23 @@ describe CookedPostProcessor do
end 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 context 'with unsized images in the post' do
let(:user) { Fabricate(:user) } let(:user) { Fabricate(:user) }
let(:topic) { Fabricate(:topic, user: user) } let(:topic) { Fabricate(:topic, user: user) }

View File

@ -40,6 +40,13 @@ Fabricator(:post_with_s3_image_url, from: :post) do
" "
end end
Fabricator(:post_with_uploads, from: :post) do
cooked "
<img src='/uploads/default/1/1234567890123456.jpg' height='100' width='100'>
"
end
Fabricator(:basic_reply, from: :post) do Fabricator(:basic_reply, from: :post) do
user(:coding_horror) user(:coding_horror)
reply_to_post_number 1 reply_to_post_number 1

View File

@ -4,5 +4,5 @@ Fabricator(:upload) do
filesize 1234 filesize 1234
width 100 width 100
height 200 height 200
url "/uploads/default/123456789.jpg" url "/uploads/default/1/1234567890123456.jpg"
end end