From b5ffea8f155baa1f898a45485c0770cbc000e1c7 Mon Sep 17 00:00:00 2001 From: Daniela Soares Feitosa Date: Thu, 2 Sep 2010 15:44:52 -0300 Subject: [PATCH] Upload of files done in background --- app/models/image.rb | 3 +++ app/models/uploaded_file.rb | 3 ++- db/migrate/20100831193450_add_thumbnails_processed_to_articles.rb | 11 +++++++++++ db/migrate/20100901203836_add_thumbnails_processed_to_image.rb | 9 +++++++++ db/schema.rb | 5 ++++- public/images/icons-app/image-loading-big.png | Bin 0 -> 10950 bytes public/images/icons-app/image-loading-display.png | Bin 0 -> 43168 bytes public/images/icons-app/image-loading-icon.png | Bin 0 -> 960 bytes public/images/icons-app/image-loading-minor.png | Bin 0 -> 3074 bytes public/images/icons-app/image-loading-portrait.png | Bin 0 -> 4124 bytes public/images/icons-app/image-loading-slideshow.png | Bin 0 -> 18814 bytes public/images/icons-app/image-loading-thumb.png | Bin 0 -> 7066 bytes test/functional/cms_controller_test.rb | 39 ++++++++++++++++++++++++++++++++++----- test/functional/content_viewer_controller_test.rb | 46 ++++++++++++++++++++++++++++++++++++++++++++++ test/functional/search_controller_test.rb | 2 ++ test/unit/image_test.rb | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/unit/products_block_test.rb | 23 +++++++++++++++++++++++ test/unit/slideshow_block_test.rb | 8 ++++++++ test/unit/uploaded_file_test.rb | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 19 files changed, 284 insertions(+), 9 deletions(-) create mode 100644 db/migrate/20100831193450_add_thumbnails_processed_to_articles.rb create mode 100644 db/migrate/20100901203836_add_thumbnails_processed_to_image.rb create mode 100644 public/images/icons-app/image-loading-big.png create mode 100644 public/images/icons-app/image-loading-display.png create mode 100644 public/images/icons-app/image-loading-icon.png create mode 100644 public/images/icons-app/image-loading-minor.png create mode 100644 public/images/icons-app/image-loading-portrait.png create mode 100644 public/images/icons-app/image-loading-slideshow.png create mode 100644 public/images/icons-app/image-loading-thumb.png diff --git a/app/models/image.rb b/app/models/image.rb index fd7cea4..1d4a7aa 100644 --- a/app/models/image.rb +++ b/app/models/image.rb @@ -16,4 +16,7 @@ class Image < ActiveRecord::Base :max_size => 500.kilobytes # remember to update validate message below validates_attachment :size => N_("%{fn} of uploaded file was larger than the maximum size of 500.0 KB") + + delay_attachment_fu_thumbnails + end diff --git a/app/models/uploaded_file.rb b/app/models/uploaded_file.rb index 26195a8..e1ed33a 100644 --- a/app/models/uploaded_file.rb +++ b/app/models/uploaded_file.rb @@ -34,6 +34,8 @@ class UploadedFile < Article validates_attachment :size => N_("%{fn} of uploaded file was larger than the maximum size of 5.0 MB") + delay_attachment_fu_thumbnails + def icon_name self.image? ? public_filename(:icon) : self.content_type.gsub('/', '-') end @@ -60,7 +62,6 @@ class UploadedFile < Article File.read(self.full_filename) end - def to_html(options = {}) article = self if image? diff --git a/db/migrate/20100831193450_add_thumbnails_processed_to_articles.rb b/db/migrate/20100831193450_add_thumbnails_processed_to_articles.rb new file mode 100644 index 0000000..d04a658 --- /dev/null +++ b/db/migrate/20100831193450_add_thumbnails_processed_to_articles.rb @@ -0,0 +1,11 @@ +class AddThumbnailsProcessedToArticles < ActiveRecord::Migration + def self.up + add_column :articles, :thumbnails_processed, :boolean, :default => false + add_column :article_versions, :thumbnails_processed, :boolean, :default => false + end + + def self.down + remove_column :articles, :thumbnails_processed + remove_column :article_versions, :thumbnails_processed + end +end diff --git a/db/migrate/20100901203836_add_thumbnails_processed_to_image.rb b/db/migrate/20100901203836_add_thumbnails_processed_to_image.rb new file mode 100644 index 0000000..ecf72a4 --- /dev/null +++ b/db/migrate/20100901203836_add_thumbnails_processed_to_image.rb @@ -0,0 +1,9 @@ +class AddThumbnailsProcessedToImage < ActiveRecord::Migration + def self.up + add_column :images, :thumbnails_processed, :boolean, :default => false + end + + def self.down + remove_column :images, :thumbnails_processed, :boolean, :default => false + end +end diff --git a/db/schema.rb b/db/schema.rb index 71ab9f4..2dd8b8a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -9,7 +9,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20100823190348) do +ActiveRecord::Schema.define(:version => 20100901203836) do create_table "article_versions", :force => true do |t| t.integer "article_id" @@ -46,6 +46,7 @@ ActiveRecord::Schema.define(:version => 20100823190348) do t.string "source" t.boolean "highlighted", :default => false t.string "external_link" + t.boolean "thumbnails_processed", :default => false end create_table "articles", :force => true do |t| @@ -81,6 +82,7 @@ ActiveRecord::Schema.define(:version => 20100823190348) do t.string "source" t.boolean "highlighted", :default => false t.string "external_link" + t.boolean "thumbnails_processed", :default => false end create_table "articles_categories", :id => false, :force => true do |t| @@ -243,6 +245,7 @@ ActiveRecord::Schema.define(:version => 20100823190348) do t.integer "size" t.integer "width" t.integer "height" + t.boolean "thumbnails_processed", :default => false end create_table "inputs", :force => true do |t| diff --git a/public/images/icons-app/image-loading-big.png b/public/images/icons-app/image-loading-big.png new file mode 100644 index 0000000..b5c8fbe Binary files /dev/null and b/public/images/icons-app/image-loading-big.png differ diff --git a/public/images/icons-app/image-loading-display.png b/public/images/icons-app/image-loading-display.png new file mode 100644 index 0000000..4c7573f Binary files /dev/null and b/public/images/icons-app/image-loading-display.png differ diff --git a/public/images/icons-app/image-loading-icon.png b/public/images/icons-app/image-loading-icon.png new file mode 100644 index 0000000..08319c3 Binary files /dev/null and b/public/images/icons-app/image-loading-icon.png differ diff --git a/public/images/icons-app/image-loading-minor.png b/public/images/icons-app/image-loading-minor.png new file mode 100644 index 0000000..1c9a14d Binary files /dev/null and b/public/images/icons-app/image-loading-minor.png differ diff --git a/public/images/icons-app/image-loading-portrait.png b/public/images/icons-app/image-loading-portrait.png new file mode 100644 index 0000000..479ce45 Binary files /dev/null and b/public/images/icons-app/image-loading-portrait.png differ diff --git a/public/images/icons-app/image-loading-slideshow.png b/public/images/icons-app/image-loading-slideshow.png new file mode 100644 index 0000000..80f63f1 Binary files /dev/null and b/public/images/icons-app/image-loading-slideshow.png differ diff --git a/public/images/icons-app/image-loading-thumb.png b/public/images/icons-app/image-loading-thumb.png new file mode 100644 index 0000000..0642df1 Binary files /dev/null and b/public/images/icons-app/image-loading-thumb.png differ diff --git a/test/functional/cms_controller_test.rb b/test/functional/cms_controller_test.rb index 81aef79..c7ab56d 100644 --- a/test/functional/cms_controller_test.rb +++ b/test/functional/cms_controller_test.rb @@ -923,8 +923,10 @@ class CmsControllerTest < Test::Unit::TestCase :parent_id => f.id, :article => {:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')} - assert File.exists?(assigns(:article).icon_name) - assigns(:article).destroy + process_delayed_job_queue + file = profile.articles.find_by_name('rails.png') + assert File.exists?(file.icon_name) + file.destroy end should 'create icon upload file' do @@ -932,8 +934,10 @@ class CmsControllerTest < Test::Unit::TestCase :type => UploadedFile.name, :article => {:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')} - assert File.exists?(assigns(:article).icon_name) - assigns(:article).destroy + process_delayed_job_queue + file = profile.articles.find_by_name('rails.png') + assert File.exists?(file.icon_name) + file.destroy end should 'record when coming from public view on upload files' do @@ -1052,10 +1056,20 @@ class CmsControllerTest < Test::Unit::TestCase should 'display list of images' do file = UploadedFile.create!(:profile => profile, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')) + process_delayed_job_queue + get :media_listing, :profile => profile.identifier + + assert_tag :tag => 'div', :attributes => { :id => 'media-listing-images' }, :descendant => { :tag => 'img', :attributes => {:src => /rails.png/}} + end + + should 'display loading image if not processed yet' do + file = UploadedFile.create!(:profile => profile, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')) get :media_listing, :profile => profile.identifier - assert_tag :tag => 'div', :attributes => { :id => 'media-listing-images' }, :descendant => { :tag => 'img', :attributes => {:src => /#{file.name}/}} + + assert_tag :tag => 'div', :attributes => { :id => 'media-listing-images' }, :descendant => { :tag => 'img', :attributes => {:src => /image-loading-thumb.png/}} end + should 'display list of documents' do file = UploadedFile.create!(:profile => profile, :uploaded_data => fixture_file_upload('/files/test.txt', 'text/plain')) get :media_listing, :profile => profile.identifier @@ -1265,4 +1279,19 @@ class CmsControllerTest < Test::Unit::TestCase assert_template 'edit' end + should 'create thumbnails for images with delayed_job' do + post :upload_files, :profile => profile.identifier, :uploaded_files => [fixture_file_upload('/files/rails.png', 'image/png'), fixture_file_upload('/files/test.txt', 'text/plain')] + file_1 = profile.articles.find_by_path('rails.png') + file_2 = profile.articles.find_by_path('test.txt') + + process_delayed_job_queue + + UploadedFile.attachment_options[:thumbnails].each do |suffix, size| + assert File.exists?(UploadedFile.find(file_1.id).public_filename(suffix)) + assert !File.exists?(UploadedFile.find(file_2.id).public_filename(suffix)) + end + file_1.destroy + file_2.destroy + end + end diff --git a/test/functional/content_viewer_controller_test.rb b/test/functional/content_viewer_controller_test.rb index c25f774..803cb40 100644 --- a/test/functional/content_viewer_controller_test.rb +++ b/test/functional/content_viewer_controller_test.rb @@ -791,6 +791,52 @@ class ContentViewerControllerTest < Test::Unit::TestCase assert_equal 2, assigns(:images).size end + should 'display default image in the slideshow if thumbnails were not processed' do + @controller.stubs(:per_page).returns(1) + folder = Folder.create!(:name => 'gallery', :profile => profile, :view_as => 'image_gallery') + + image1 = UploadedFile.create!(:profile => profile, :parent => folder, :uploaded_data => fixture_file_upload('/files/other-pic.jpg', 'image/jpg')) + + get :view_page, :profile => profile.identifier, :page => folder.explode_path, :slideshow => true + + assert_tag :tag => 'img', :attributes => {:src => /\/images\/icons-app\/image-loading-display.png/} + end + + should 'display thumbnail image in the slideshow if thumbnails were processed' do + @controller.stubs(:per_page).returns(1) + folder = Folder.create!(:name => 'gallery', :profile => profile, :view_as => 'image_gallery') + + image1 = UploadedFile.create!(:profile => profile, :parent => folder, :uploaded_data => fixture_file_upload('/files/other-pic.jpg', 'image/jpg')) + + process_delayed_job_queue + get :view_page, :profile => profile.identifier, :page => folder.explode_path, :slideshow => true + + assert_tag :tag => 'img', :attributes => {:src => /other-pic_display.jpg/} + end + + should 'display default image in gallery if thumbnails were not processed' do + @controller.stubs(:per_page).returns(1) + folder = Folder.create!(:name => 'gallery', :profile => profile, :view_as => 'image_gallery') + + image1 = UploadedFile.create!(:profile => profile, :parent => folder, :uploaded_data => fixture_file_upload('/files/other-pic.jpg', 'image/jpg')) + + get :view_page, :profile => profile.identifier, :page => folder.explode_path + + assert_tag :tag => 'img', :attributes => {:src => /\/images\/icons-app\/image-loading-thumb.png/} + end + + should 'display thumbnail image in gallery if thumbnails were processed' do + @controller.stubs(:per_page).returns(1) + folder = Folder.create!(:name => 'gallery', :profile => profile, :view_as => 'image_gallery') + + image1 = UploadedFile.create!(:profile => profile, :parent => folder, :uploaded_data => fixture_file_upload('/files/other-pic.jpg', 'image/jpg')) + + process_delayed_job_queue + get :view_page, :profile => profile.identifier, :page => folder.explode_path + + assert_tag :tag => 'img', :attributes => {:src => /other-pic_thumb.jpg/} + end + should 'display source from article' do profile.articles << TextileArticle.new(:name => "Article one", :profile => profile, :source => 'http://www.original-source.invalid') get :view_page, :profile => profile.identifier, :page => ['article-one'] diff --git a/test/functional/search_controller_test.rb b/test/functional/search_controller_test.rb index 85fc2af..1f40280 100644 --- a/test/functional/search_controller_test.rb +++ b/test/functional/search_controller_test.rb @@ -562,6 +562,8 @@ class SearchControllerTest < Test::Unit::TestCase cat = Category.create!(:name => 'category2', :environment => Environment.default, :parent => parent, :image_builder => {:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')} ) + + process_delayed_job_queue get :category_index, :category_path => [ 'category1', 'category2' ], :query => 'teste' assert_tag :tag => 'img', :attributes => { :src => /rails_thumb\.png/ } end diff --git a/test/unit/image_test.rb b/test/unit/image_test.rb index 9f1838d..b29a651 100644 --- a/test/unit/image_test.rb +++ b/test/unit/image_test.rb @@ -3,6 +3,11 @@ require File.dirname(__FILE__) + '/../test_helper' class ImageTest < Test::Unit::TestCase fixtures :images + def setup + @profile = create_user('testinguser').person + end + attr_reader :profile + should 'have thumbnails options' do [:big, :thumb, :portrait, :minor, :icon].each do |option| assert Image.attachment_options[:thumbnails].include?(option), "should have #{option}" @@ -16,4 +21,67 @@ class ImageTest < Test::Unit::TestCase assert_match /#{Image.max_size.to_humanreadable}/, image.errors[:size] end + should 'create thumbnails after processing jobs' do + file = Image.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :owner => profile) + + process_delayed_job_queue + Image.attachment_options[:thumbnails].each do |suffix, size| + assert File.exists?(Image.find(file.id).public_filename(suffix)) + end + file.destroy + end + + should 'set thumbnails_processed to true after creating thumbnails' do + file = Image.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :owner => profile) + + process_delayed_job_queue + + assert Image.find(file.id).thumbnails_processed + file.destroy + end + + should 'have thumbnails_processed attribute' do + assert Image.new.respond_to?(:thumbnails_processed) + end + + should 'return false by default in thumbnails_processed' do + assert !Image.new.thumbnails_processed + end + + should 'set thumbnails_processed to true' do + file = Image.new + file.thumbnails_processed = true + + assert file.thumbnails_processed + end + + should 'have a default image if thumbnails were not processed' do + file = Image.new + assert_equal '/images/icons-app/image-loading-thumb.png', file.public_filename(:thumb) + end + + should 'return image thumbnail if thumbnails were processed' do + file = Image.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :owner => profile) + process_delayed_job_queue + + assert_match(/rails_thumb.png/, Image.find(file.id).public_filename(:thumb)) + + file.destroy + end + + should 'store width and height after processing' do + file = Image.create!(:owner => profile, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')) + file.create_thumbnails + + file = Image.find(file.id) + assert_equal [50, 64], [file.width, file.height] + end + + should 'have a loading image to each size of thumbnails' do + Image.attachment_options[:thumbnails].each do |suffix, size| + image = RAILS_ROOT + '/public/images/icons-app/image-loading-%s.png' % suffix + assert File.exists?(image) + end + end + end diff --git a/test/unit/products_block_test.rb b/test/unit/products_block_test.rb index 83e8d4d..f02b0c8 100644 --- a/test/unit/products_block_test.rb +++ b/test/unit/products_block_test.rb @@ -136,4 +136,27 @@ class ProductsBlockTest < ActiveSupport::TestCase assert_tag_in_string footer, :tag => 'a', :attributes => { :href => /\/catalog\/testenterprise$/ }, :content => 'View all products' end + + should 'display the default minor image if thumbnails were not processed' do + enterprise = Enterprise.create!(:name => 'testenterprise', :identifier => 'testenterprise') + enterprise.products.create!(:name => 'product', :product_category => @product_category, :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')}) + + block.expects(:products).returns(enterprise.products) + + content = block.content + + assert_tag_in_string content, :tag => 'a', :attributes => { :style => /image-loading-minor.png/ } + end + + should 'display the thumbnail image if thumbnails were processed' do + enterprise = Enterprise.create!(:name => 'testenterprise', :identifier => 'testenterprise') + enterprise.products.create!(:name => 'product', :product_category => @product_category, :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')}) + + process_delayed_job_queue + block.expects(:products).returns(enterprise.products.reload) + + content = block.content + assert_tag_in_string content, :tag => 'a', :attributes => { :style => /rails_minor.png/ } + end + end diff --git a/test/unit/slideshow_block_test.rb b/test/unit/slideshow_block_test.rb index 82c29c1..57d6c72 100644 --- a/test/unit/slideshow_block_test.rb +++ b/test/unit/slideshow_block_test.rb @@ -82,6 +82,14 @@ class SlideshowBlockTest < ActiveSupport::TestCase assert_equal '/bli/slideshow.png', SlideshowBlock.new(:image_size => 'slideshow').public_filename_for(image) end + should 'display the default slideshow image if thumbnails were not processed' do + image = mock + image.expects(:public_filename).with('slideshow').returns('/images/icons-app/image-loading-slideshow.png') + File.expects(:exists?).with("#{Rails.root}/public/images/icons-app/image-loading-slideshow.png").returns(true) + + assert_equal '/images/icons-app/image-loading-slideshow.png', SlideshowBlock.new(:image_size => 'slideshow').public_filename_for(image) + end + should 'fallback to existing size in case the requested size does not exist' do block = SlideshowBlock.new(:image_size => 'slideshow') diff --git a/test/unit/uploaded_file_test.rb b/test/unit/uploaded_file_test.rb index 230abe2..c7c9c3d 100644 --- a/test/unit/uploaded_file_test.rb +++ b/test/unit/uploaded_file_test.rb @@ -90,7 +90,8 @@ class UploadedFileTest < Test::Unit::TestCase f = fast_create(Folder, :name => 'test_folder', :profile_id => p.id) file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :parent_id => f.id, :profile => p) - assert File.exists?(file.public_filename(:icon)) + process_delayed_job_queue + assert File.exists?(UploadedFile.find(file.id).public_filename(:icon)) file.destroy end @@ -98,7 +99,8 @@ class UploadedFileTest < Test::Unit::TestCase p = create_user('test_user').person file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :profile => p) - assert File.exists?(file.public_filename(:icon)) + process_delayed_job_queue + assert File.exists?(UploadedFile.find(file.id).public_filename(:icon)) file.destroy end @@ -156,4 +158,74 @@ class UploadedFileTest < Test::Unit::TestCase assert_nil upload.errors[:title] end + should 'create thumbnails after processing jobs' do + file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :profile => profile) + + process_delayed_job_queue + + UploadedFile.attachment_options[:thumbnails].each do |suffix, size| + assert File.exists?(UploadedFile.find(file.id).public_filename(suffix)) + end + file.destroy + end + + should 'set thumbnails_processed to true after creating thumbnails' do + file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :profile => profile) + + process_delayed_job_queue + + assert UploadedFile.find(file.id).thumbnails_processed + file.destroy + end + + should 'have thumbnails_processed attribute' do + assert UploadedFile.new.respond_to?(:thumbnails_processed) + end + + should 'return false by default in thumbnails_processed' do + assert !UploadedFile.new.thumbnails_processed + end + + should 'set thumbnails_processed to true' do + file = UploadedFile.new + file.thumbnails_processed = true + + assert file.thumbnails_processed + end + + should 'have a default image if thumbnails were not processed' do + file = UploadedFile.new + assert_equal '/images/icons-app/image-loading-thumb.png', file.public_filename + end + + should 'return image thumbnail if thumbnails were processed' do + file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :profile => profile) + process_delayed_job_queue + + assert_match(/rails_thumb.png/, UploadedFile.find(file.id).public_filename(:thumb)) + + file.destroy + end + + should 'return the default thumbnail image as icon for images ' do + f = UploadedFile.new + f.expects(:image?).returns(true) + f.expects(:public_filename).with(:icon).returns('/path/to/file.xyz') + assert_equal '/path/to/file.xyz', f.icon_name + end + + should 'store width and height after processing' do + file = UploadedFile.create!(:profile => @profile, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')) + file.create_thumbnails + + file = UploadedFile.find(file.id) + assert_equal [50, 64], [file.width, file.height] + end + + should 'have a loading image to each size of thumbnails' do + UploadedFile.attachment_options[:thumbnails].each do |suffix, size| + image = RAILS_ROOT + '/public/images/icons-app/image-loading-%s.png' % suffix + assert File.exists?(image) + end + end end -- libgit2 0.21.2