Upload Image bg processing with carrierwave_backgrounder and testing

jamesperet 9 years ago
parent
commit
82f1b72319

+ 1 - 0
Gemfile

@@ -52,6 +52,7 @@ gem 'figaro'
52 52
 gem "mini_magick"
53 53
 gem 'fog'
54 54
 gem "carrierwave"
55
+gem 'carrierwave_backgrounder'
55 56
 gem 'i18n'
56 57
 gem 'rails_12factor', group: :production
57 58
 gem 'gibbon'

+ 3 - 0
Gemfile.lock

@@ -44,6 +44,8 @@ GEM
44 44
       activesupport (>= 3.2.0)
45 45
       json (>= 1.7)
46 46
       mime-types (>= 1.16)
47
+    carrierwave_backgrounder (0.4.1)
48
+      carrierwave (~> 0.5)
47 49
     coffee-rails (4.0.1)
48 50
       coffee-script (>= 2.2.0)
49 51
       railties (>= 4.0.0, < 5.0)
@@ -289,6 +291,7 @@ DEPENDENCIES
289 291
   bootstrap-timepicker-rails
290 292
   bootstrap_form
291 293
   carrierwave
294
+  carrierwave_backgrounder
292 295
   coffee-rails (~> 4.0.0)
293 296
   cucumber-rails
294 297
   database_cleaner

+ 1 - 2
app/controllers/uploads_controller.rb

@@ -25,7 +25,6 @@ class UploadsController < ApplicationController
25 25
   # POST /uploads.json
26 26
   def create
27 27
     @upload = Upload.new(upload_params)
28
-
29 28
     respond_to do |format|
30 29
       if @upload.save
31 30
         format.html { redirect_to @upload, notice: 'Upload was successfully created.' }
@@ -78,6 +77,6 @@ class UploadsController < ApplicationController
78 77
 
79 78
     # Never trust parameters from the scary internet, only allow the white list through.
80 79
     def upload_params
81
-      params.require(:upload).permit(:title, :file, :description)
80
+      params.require(:upload).permit(:title, :file, :description, :file_tmp, :file_processing)
82 81
     end
83 82
 end

+ 16 - 9
app/models/upload.rb

@@ -1,17 +1,24 @@
1 1
 class Upload < ActiveRecord::Base
2 2
   
3 3
     mount_uploader :file, FileUploader
4
+    process_in_background :file
5
+    store_in_background :file
6
+    
4 7
   
5 8
     def get_extension
6
-      case self.file.file.content_type
7
-      when 'image/jpeg'
8
-        return 'jpg'
9
-      when 'image/png'
10
-        return 'png'
11
-      when 'image/gif'
12
-        return 'gif'
13
-      when 'application/pdf'
14
-        return 'pdf'
9
+      if(self.file_processing == false)
10
+        case self.file.file.content_type
11
+        when 'image/jpeg'
12
+          return 'jpg'
13
+        when 'image/png'
14
+          return 'png'
15
+        when 'image/gif'
16
+          return 'gif'
17
+        when 'application/pdf'
18
+          return 'pdf'
19
+        else
20
+          return 'none'
21
+        end
15 22
       else
16 23
         return 'none'
17 24
       end

+ 8 - 2
app/uploaders/file_uploader.rb

@@ -6,7 +6,9 @@ class FileUploader < CarrierWave::Uploader::Base
6 6
   # include CarrierWave::RMagick
7 7
   include CarrierWave::MiniMagick
8 8
   include CarrierWave::MimeTypes
9
-
9
+  
10
+  include ::CarrierWave::Backgrounder::Delay
11
+  
10 12
   # Choose what kind of storage to use for this uploader:
11 13
   if Rails.env.test? or Rails.env.cucumber?
12 14
       storage :file
@@ -29,8 +31,12 @@ class FileUploader < CarrierWave::Uploader::Base
29 31
     "uploads/#{mounted_as}/#{model.id}"
30 32
   end
31 33
   
34
+  def root
35
+    "#{Rails.root}/public"
36
+  end
37
+  
32 38
   def cache_dir
33
-    " ./tmp/uploads/#{mounted_as}/#{model.id}"
39
+    " ./tmp/uploads/#{mounted_as}/"
34 40
   end
35 41
 
36 42
   # Provide a default URL as a default if there hasn't been a file uploaded:

+ 9 - 0
config/initializers/carrierwave_backgrounder.rb

@@ -0,0 +1,9 @@
1
+CarrierWave::Backgrounder.configure do |c|
2
+  # c.backend :delayed_job, queue: :carrierwave
3
+  c.backend :resque, queue: :carrierwave
4
+  # c.backend :sidekiq, queue: :carrierwave
5
+  # c.backend :girl_friday, queue: :carrierwave
6
+  # c.backend :sucker_punch, queue: :carrierwave
7
+  # c.backend :qu, queue: :carrierwave
8
+  # c.backend :qc
9
+end

+ 6 - 0
db/migrate/20150109224815_add_tmp_to_files.rb

@@ -0,0 +1,6 @@
1
+class AddTmpToFiles < ActiveRecord::Migration
2
+  def change
3
+    add_column :uploads, :file_tmp, :string
4
+    add_column :uploads, :file_processing, :boolean, null: false, default: false
5
+  end
6
+end

+ 3 - 1
db/schema.rb

@@ -11,7 +11,7 @@
11 11
 #
12 12
 # It's strongly recommended that you check this file into your version control system.
13 13
 
14
-ActiveRecord::Schema.define(version: 20150105022653) do
14
+ActiveRecord::Schema.define(version: 20150109224815) do
15 15
 
16 16
   # These are extensions that must be enabled in order to support this database
17 17
   enable_extension "plpgsql"
@@ -85,6 +85,8 @@ ActiveRecord::Schema.define(version: 20150105022653) do
85 85
     t.text     "description"
86 86
     t.datetime "created_at"
87 87
     t.datetime "updated_at"
88
+    t.string   "file_tmp"
89
+    t.boolean  "file_processing", default: false, null: false
88 90
   end
89 91
 
90 92
   create_table "users", force: true do |t|

+ 1 - 1
spec/factories.rb

@@ -23,7 +23,7 @@ FactoryGirl.define do
23 23
     f.content "foobar"  
24 24
     f.published true
25 25
     f.description "foobar is cool" 
26
-  end 
26
+  end
27 27
   
28 28
   factory :info do
29 29
     website_name 'Rails Website Template'