Commit d5ddc762b730aa8b2034043200de2925752ec3d5
Committed by
Antonio Terceiro
1 parent
b7642d12
Exists in
master
and in
29 other branches
Added libs to create thumbnails in background
* added delayed_attachment_fu lib to process thumbnails in background * added create_thumbnails_job lib to create thumbnails (ActionItem1661)
Showing
4 changed files
with
87 additions
and
0 deletions
Show diff stats
@@ -0,0 +1 @@ | @@ -0,0 +1 @@ | ||
1 | +require 'delayed_attachment_fu' |
@@ -0,0 +1,53 @@ | @@ -0,0 +1,53 @@ | ||
1 | +module DelayedAttachmentFu | ||
2 | + | ||
3 | + module ClassMethods | ||
4 | + def delay_attachment_fu_thumbnails | ||
5 | + include DelayedAttachmentFu::InstanceMethods | ||
6 | + after_create do |file| | ||
7 | + if file.image? | ||
8 | + Delayed::Job.enqueue CreateThumbnailsJob.new(file.class.name, file.id) | ||
9 | + end | ||
10 | + end | ||
11 | + end | ||
12 | + end | ||
13 | + | ||
14 | + module InstanceMethods | ||
15 | + # skip processing with RMagick | ||
16 | + def process_attachment | ||
17 | + end | ||
18 | + | ||
19 | + def after_process_attachment | ||
20 | + save_to_storage | ||
21 | + @temp_paths.clear | ||
22 | + @saved_attachment = nil | ||
23 | + callback :after_attachment_saved | ||
24 | + end | ||
25 | + | ||
26 | + def create_thumbnails | ||
27 | + if thumbnailable? | ||
28 | + self.class.with_image(full_filename) do |img| | ||
29 | + self.width = img.columns | ||
30 | + self.height = img.rows | ||
31 | + self.save! | ||
32 | + end | ||
33 | + self.class.attachment_options[:thumbnails].each do |suffix, size| | ||
34 | + self.create_or_update_thumbnail(self.full_filename, suffix, size) | ||
35 | + end | ||
36 | + self.update_attributes!(:thumbnails_processed => true) | ||
37 | + end | ||
38 | + end | ||
39 | + | ||
40 | + def public_filename(size=nil) | ||
41 | + if self.thumbnails_processed | ||
42 | + super(size) | ||
43 | + else | ||
44 | + size ||= 'thumb' | ||
45 | + '/images/icons-app/image-loading-%s.png' % size | ||
46 | + end | ||
47 | + end | ||
48 | + | ||
49 | + | ||
50 | + end | ||
51 | +end | ||
52 | + | ||
53 | +ActiveRecord::Base.send(:extend, DelayedAttachmentFu::ClassMethods) |
@@ -0,0 +1,27 @@ | @@ -0,0 +1,27 @@ | ||
1 | +require File.dirname(__FILE__) + '/../test_helper' | ||
2 | + | ||
3 | +class CreateThumbnailsJobTest < ActiveSupport::TestCase | ||
4 | + | ||
5 | + should 'create thumbnails to uploaded files' do | ||
6 | + person = create_user('test_user').person | ||
7 | + file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :profile => person) | ||
8 | + | ||
9 | + assert_equal [], file.thumbnails | ||
10 | + job = CreateThumbnailsJob.new(file.class.name, file.id) | ||
11 | + job.perform | ||
12 | + file.reload | ||
13 | + assert_not_equal [], file.thumbnails | ||
14 | + end | ||
15 | + | ||
16 | + should 'set thumbnails_processed to true after finished' do | ||
17 | + person = create_user('test_user').person | ||
18 | + file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :profile => person) | ||
19 | + | ||
20 | + job = CreateThumbnailsJob.new(file.class.name, file.id) | ||
21 | + job.perform | ||
22 | + | ||
23 | + file.reload | ||
24 | + assert file.thumbnails_processed | ||
25 | + end | ||
26 | + | ||
27 | +end |