File upload with carrierwave, amazon S3 upload and env variables with figaro

jamesperet 10 years ago
parent
commit
3ac0d0c12d

+ 3 - 1
.gitignore

@@ -55,4 +55,6 @@ Icon
55 55
 .AppleDesktop
56 56
 Network Trash Folder
57 57
 Temporary Items
58
-.apdisk
58
+.apdisk
59
+# Ignore application configuration
60
+/config/application.yml

+ 4 - 0
Gemfile

@@ -41,6 +41,10 @@ gem 'devise'
41 41
 gem "letter_opener", :group => :development
42 42
 gem 'redcarpet'
43 43
 gem 'summernote-rails'
44
+gem 'figaro'
45
+gem "mini_magick"
46
+gem 'fog'
47
+gem "carrierwave"
44 48
 
45 49
 # Use ActiveModel has_secure_password
46 50
 # gem 'bcrypt', '~> 3.1.7'

+ 47 - 0
Gemfile.lock

@@ -31,6 +31,11 @@ GEM
31 31
     bootstrap-timepicker-rails (0.1.3)
32 32
       railties (>= 3.0)
33 33
     builder (3.1.4)
34
+    carrierwave (0.10.0)
35
+      activemodel (>= 3.2.0)
36
+      activesupport (>= 3.2.0)
37
+      json (>= 1.7)
38
+      mime-types (>= 1.16)
34 39
     coffee-rails (4.0.1)
35 40
       coffee-script (>= 2.2.0)
36 41
       railties (>= 4.0.0, < 5.0)
@@ -46,15 +51,44 @@ GEM
46 51
       thread_safe (~> 0.1)
47 52
       warden (~> 1.2.3)
48 53
     erubis (2.7.0)
54
+    excon (0.39.5)
49 55
     execjs (2.2.1)
56
+    figaro (1.0.0)
57
+      thor (~> 0.14)
50 58
     flatstrap-rails (0.3.0.2)
51 59
       twitter-bootstrap-rails (~> 2.2.6)
60
+    fog (1.23.0)
61
+      fog-brightbox
62
+      fog-core (~> 1.23)
63
+      fog-json
64
+      fog-softlayer
65
+      ipaddress (~> 0.5)
66
+      nokogiri (~> 1.5, >= 1.5.11)
67
+    fog-brightbox (0.5.1)
68
+      fog-core (~> 1.22)
69
+      fog-json
70
+      inflecto
71
+    fog-core (1.24.0)
72
+      builder
73
+      excon (~> 0.38)
74
+      formatador (~> 0.2)
75
+      mime-types
76
+      net-scp (~> 1.1)
77
+      net-ssh (>= 2.1.3)
78
+    fog-json (1.0.0)
79
+      multi_json (~> 1.0)
80
+    fog-softlayer (0.3.17)
81
+      fog-core
82
+      fog-json
52 83
     font-awesome-rails (4.2.0.0)
53 84
       railties (>= 3.2, < 5.0)
85
+    formatador (0.2.5)
54 86
     friendly_id (5.0.4)
55 87
       activerecord (>= 4.0.0)
56 88
     hike (1.2.3)
57 89
     i18n (0.6.11)
90
+    inflecto (0.0.2)
91
+    ipaddress (0.8.0)
58 92
     jbuilder (1.5.3)
59 93
       activesupport (>= 3.0.0)
60 94
       multi_json (>= 1.2.0)
@@ -76,8 +110,16 @@ GEM
76 110
       mime-types (~> 1.16)
77 111
       treetop (~> 1.4.8)
78 112
     mime-types (1.25.1)
113
+    mini_magick (3.8.1)
114
+      subexec (~> 0.2.1)
115
+    mini_portile (0.6.0)
79 116
     minitest (4.7.5)
80 117
     multi_json (1.10.1)
118
+    net-scp (1.2.1)
119
+      net-ssh (>= 2.6.5)
120
+    net-ssh (2.9.1)
121
+    nokogiri (1.6.3.1)
122
+      mini_portile (= 0.6.0)
81 123
     orm_adapter (0.5.0)
82 124
     pg (0.17.1)
83 125
     polyglot (0.3.5)
@@ -117,6 +159,7 @@ GEM
117 159
       actionpack (>= 3.0)
118 160
       activesupport (>= 3.0)
119 161
       sprockets (~> 2.8)
162
+    subexec (0.2.3)
120 163
     summernote-rails (0.4.0)
121 164
       railties (>= 3.1)
122 165
     therubyracer (0.12.1)
@@ -147,15 +190,19 @@ PLATFORMS
147 190
 
148 191
 DEPENDENCIES
149 192
   bootstrap-timepicker-rails
193
+  carrierwave
150 194
   coffee-rails (~> 4.0.0)
151 195
   devise
196
+  figaro
152 197
   flatstrap-rails
198
+  fog
153 199
   font-awesome-rails
154 200
   friendly_id (~> 5.0.0)
155 201
   jbuilder (~> 1.2)
156 202
   jquery-rails
157 203
   less-rails
158 204
   letter_opener
205
+  mini_magick
159 206
   pg
160 207
   rails (= 4.0.4)
161 208
   redcarpet

+ 1 - 1
app/controllers/application_controller.rb

@@ -6,7 +6,7 @@ class ApplicationController < ActionController::Base
6 6
   before_filter :configure_permitted_parameters, if: :devise_controller?
7 7
   
8 8
   def configure_permitted_parameters
9
-    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:first_name, :last_name, :email, :current_password) }
9
+    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:first_name, :last_name, :email, :current_password, :avatar) }
10 10
     devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name, :last_name, :email, :password) }
11 11
   end
12 12
   

+ 2 - 0
app/models/user.rb

@@ -8,6 +8,8 @@ class User < ActiveRecord::Base
8 8
   validates :password, length: {minimum: 5, maximum: 120}, on: :update, allow_blank: true
9 9
   
10 10
   has_many :posts
11
+  
12
+  mount_uploader :avatar, AvatarUploader
11 13
            
12 14
   def full_name
13 15
     name = self.first_name.to_s + ' ' + self.last_name.to_s

+ 71 - 0
app/uploaders/avatar_uploader.rb

@@ -0,0 +1,71 @@
1
+# encoding: utf-8
2
+
3
+class AvatarUploader < CarrierWave::Uploader::Base
4
+
5
+  # Include RMagick or MiniMagick support:
6
+  # include CarrierWave::RMagick
7
+  include CarrierWave::MiniMagick
8
+  include CarrierWave::MimeTypes
9
+
10
+  # Choose what kind of storage to use for this uploader:
11
+  
12
+  if Rails.env.test? or Rails.env.cucumber?
13
+      storage :file
14
+  end
15
+
16
+  if Rails.env.development?
17
+      storage :file
18
+  end
19
+
20
+  if Rails.env.production?
21
+      # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility if using fog: 
22
+      include Sprockets::Helpers::RailsHelper  
23
+      include Sprockets::Helpers::IsolatedHelper 
24
+      storage :fog
25
+  end
26
+  
27
+  process :set_content_type
28
+
29
+  # Override the directory where uploaded files will be stored.
30
+  # This is a sensible default for uploaders that are meant to be mounted:
31
+  def store_dir
32
+    "uploads/#{mounted_as}/#{model.id}"
33
+  end
34
+  
35
+  def cache_dir
36
+    " ./tmp/uploads/#{mounted_as}/#{model.id}"
37
+  end
38
+
39
+  # Provide a default URL as a default if there hasn't been a file uploaded:
40
+  # def default_url
41
+  #   # For Rails 3.1+ asset pipeline compatibility:
42
+  #   # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
43
+  #
44
+  #   "/images/fallback/" + [version_name, "default.png"].compact.join('_')
45
+  # end
46
+
47
+  # Process files as they are uploaded:
48
+  # process :scale => [200, 300]
49
+  #
50
+  # def scale(width, height)
51
+  #   # do something
52
+  # end
53
+
54
+  # Create different versions of your uploaded files:
55
+  version :thumb do
56
+     process :resize_to_fit => [50, 50]
57
+  end
58
+
59
+  # Add a white list of extensions which are allowed to be uploaded.
60
+  # For images you might use something like this:
61
+  # def extension_white_list
62
+  #   %w(jpg jpeg gif png)
63
+  # end
64
+
65
+  # Override the filename of the uploaded files:
66
+  # Avoid using model.id or version_name here, see uploader/store.rb for details.
67
+  # def filename
68
+  #   "something.jpg" if original_filename
69
+  # end
70
+
71
+end

+ 2 - 0
app/views/admin_panel/users.html.erb

@@ -7,6 +7,7 @@
7 7
 		<table class="table table-bordered">
8 8
 		  <thead>
9 9
 		    <tr>
10
+			 <th>Image</th>
10 11
 		      <th>Name</th>
11 12
 		      <th>Email</th>
12 13
 		    </tr>
@@ -15,6 +16,7 @@
15 16
 		  <tbody>
16 17
 		    <% @users.each do |user| %>
17 18
 		      <tr>
19
+			   <th><%= image_tag user.avatar.thumb.to_s if user.avatar != nil %></th>
18 20
 		        <td><%= user.full_name %></td>
19 21
 		        <td><%= user.email %></td>
20 22
 		      </tr>

+ 1 - 0
app/views/devise/registrations/edit.html.erb

@@ -7,6 +7,7 @@
7 7
     <%= f.input :first_name, required: true, autofocus: true %>
8 8
     <%= f.input :last_name, required: true %>
9 9
     <%= f.input :email, required: true %>
10
+    <%= f.file_field :avatar %>
10 11
 
11 12
     <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
12 13
       <p>Currently waiting confirmation for: <%= resource.unconfirmed_email %></p>

+ 9 - 0
config/initializers/carrierwave.rb

@@ -0,0 +1,9 @@
1
+CarrierWave.configure do |config|  
2
+  config.fog_credentials = {
3
+    provider:"AWS",    
4
+           aws_access_key_id: ENV["AWS_ACCESS_KEY_ID"],    
5
+           aws_secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"],
6
+           region: 'sa-east-1'
7
+      }  
8
+  config.fog_directory = ENV["AWS_S3_BUCKET"]
9
+end

+ 5 - 0
db/migrate/20140922054459_add_avatar_to_users.rb

@@ -0,0 +1,5 @@
1
+class AddAvatarToUsers < ActiveRecord::Migration
2
+  def change
3
+    add_column :users, :avatar, :string
4
+  end
5
+end

+ 2 - 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: 20140918040715) do
14
+ActiveRecord::Schema.define(version: 20140922054459) do
15 15
 
16 16
   # These are extensions that must be enabled in order to support this database
17 17
   enable_extension "plpgsql"
@@ -59,6 +59,7 @@ ActiveRecord::Schema.define(version: 20140918040715) do
59 59
     t.datetime "created_at"
60 60
     t.datetime "updated_at"
61 61
     t.boolean  "admin"
62
+    t.string   "avatar"
62 63
   end
63 64
 
64 65
   add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree

BIN
public/uploads/avatar/1/avatar_jamesperet.jpg


BIN
public/uploads/avatar/1/thumb_avatar_jamesperet.jpg