Skip to content

Commit

Permalink
upload img with carrierwave #41
Browse files Browse the repository at this point in the history
  • Loading branch information
happypeter committed Dec 30, 2012
1 parent 3090d49 commit 5387079
Show file tree
Hide file tree
Showing 16 changed files with 209 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public/assets/
unicorn/
# Ignore the default SQLite database.
db/*.sqlite3
config/settings.yml

# Ignore all logfiles and tempfiles.
log/*.log
Expand Down
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ gem 'mysql2'
gem "cancan"
gem 'unicorn'
gem 'redcarpet'
gem 'carrierwave', "~> 0.6.2"
gem 'carrierwave-upyun', '~> 0.1.6'
gem 'rmagick'
gem 'mime-types'
gem 'settingslogic'


# Gems used only for assets and not required
Expand Down
15 changes: 15 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ GEM
net-sftp (>= 2.0.0)
net-ssh (>= 2.0.14)
net-ssh-gateway (>= 1.1.0)
carrierwave (0.6.2)
activemodel (>= 3.2.0)
activesupport (>= 3.2.0)
carrierwave-upyun (0.1.6)
carrierwave (>= 0.5.7)
rest-client (>= 1.6.7)
coffee-rails (3.2.2)
coffee-script (>= 2.2.0)
railties (~> 3.2.0)
Expand Down Expand Up @@ -104,11 +110,15 @@ GEM
rdoc (3.12)
json (~> 1.4)
redcarpet (2.1.1)
rest-client (1.6.7)
mime-types (>= 1.16)
rmagick (2.13.1)
sass (3.2.1)
sass-rails (3.2.5)
railties (~> 3.2.0)
sass (>= 3.1.10)
tilt (~> 1.3)
settingslogic (2.0.8)
sprockets (2.1.3)
hike (~> 1.2)
rack (~> 1.0)
Expand Down Expand Up @@ -137,12 +147,17 @@ DEPENDENCIES
bootstrap-sass
cancan
capistrano
carrierwave (~> 0.6.2)
carrierwave-upyun (~> 0.1.6)
coffee-rails (~> 3.2.1)
jquery-rails
mime-types
mysql2
rails (= 3.2.8)
redcarpet
rmagick
sass-rails (~> 3.2.3)
settingslogic
therubyracer
uglifier (>= 1.0.3)
unicorn
21 changes: 21 additions & 0 deletions app/controllers/upyun_images_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class UpyunImagesController < ApplicationController
def new
@upyun_image = UpyunImage.new
end

def show
@img = UpyunImage.find(params[:id])
end

def create
file = params[:upyun_image][:asset]
image = UpyunImage.new(params[:upyun_image])
image.filename = file.original_filename
image.user = current_user
if image.save
redirect_to upyun_image_path(image)
else
redirect_to new_upyun_image_path
end
end
end
5 changes: 5 additions & 0 deletions app/models/settings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# encoding: utf-8
class Settings < Settingslogic
source "#{Rails.root}/config/settings.yml"
namespace Rails.env
end
17 changes: 17 additions & 0 deletions app/models/upyun_image.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class UpyunImage < ActiveRecord::Base
belongs_to :user
attr_accessible :asset

mount_uploader :asset, UpyunImageUploader

validates :asset, :content_type, :size, :filename, :presence => true
validates_integrity_of :asset

before_validation :set_metadata

private
def set_metadata
self.content_type = asset.file.content_type
self.size = asset.file.size
end
end
19 changes: 19 additions & 0 deletions app/uploaders/uploader_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'digest'
require 'carrierwave/processing/mime_types'

module UploaderHelper
def store_dir
"uploads/"
end

def cache_dir
"/tmp"
end

def filename
if original_filename.present?
hashed_name = Digest::MD5.hexdigest(File.dirname(current_path))[5..15]
"#{hashed_name}.#{file.extension}"
end
end
end
63 changes: 63 additions & 0 deletions app/uploaders/upyun_image_uploader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# encoding: utf-8

class UpyunImageUploader < CarrierWave::Uploader::Base
include UploaderHelper

# Include RMagick or MiniMagick support:
include CarrierWave::RMagick
# include CarrierWave::MiniMagick

# Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
# include Sprockets::Helpers::RailsHelper
# include Sprockets::Helpers::IsolatedHelper

# Choose what kind of storage to use for this uploader:
# storage :file
# storage :fog
storage :upyun

self.upyun_username = Settings.upyun.operator_name
self.upyun_password = Settings.upyun.operator_password
self.upyun_bucket = Settings.upyun.bucket
self.upyun_bucket_domain = Settings.upyun.bucket_domain

process :resize_to_limit => [570, 0]
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
#def store_dir
#"public/uploads/"
#end

# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url
# # For Rails 3.1+ asset pipeline compatibility:
# # asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
#
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end

# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end

# Create different versions of your uploaded files:
# version :thumb do
# process :scale => [50, 50]
# end

# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
# def extension_white_list
# %w(jpg jpeg gif png)
# end

# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end

end
1 change: 1 addition & 0 deletions app/views/posts/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
</div>
</div>
</div>

10 changes: 10 additions & 0 deletions app/views/upyun_images/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div class="container">
<%= form_for @upyun_image, :html => {:multipart => true} do |f| %>
<p>
<%= f.file_field :asset %>
<br>
<br>
<%= f.submit "upload" %>
</p>
<% end %>
</div>
3 changes: 3 additions & 0 deletions app/views/upyun_images/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="container">
<%= image_tag @img.asset_url.to_s %>
</div>
1 change: 1 addition & 0 deletions config/initializers/carrierwave.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CarrierWave::SanitizedFile.sanitize_regexp = /[^[:word:]\.\-\+]/
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
get "/write_blog" => "posts#new"
resources :posts
resources :courses
resources :upyun_images
resources :videos

resources :users
Expand Down
23 changes: 23 additions & 0 deletions config/settings.yml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
default: &default
canonical_host: "www.example.com"
system_email: "no-reply@example.com"
memcached:
servers: ["127.0.0.1:11211"]
namespace: "example"
upyun:
switch: 'off' # to support file upload, change this to 'on'
operator_name: ''
operator_password: ''
bucket: ''
bucket_domain: ''

production:
<<: *default

development:
<<: *default

test:
<<: *default
default_password: 'project_rabel'

13 changes: 13 additions & 0 deletions db/migrate/20121229090529_create_upyun_images.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreateUpyunImages < ActiveRecord::Migration
def change
create_table :upyun_images do |t|
t.integer :user_id
t.string :asset
t.integer :size
t.string :content_type
t.string :filename

t.timestamps
end
end
end
12 changes: 11 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20121228095218) do
ActiveRecord::Schema.define(:version => 20121229090529) do

create_table "comments", :force => true do |t|
t.string "content"
Expand Down Expand Up @@ -45,6 +45,16 @@
t.datetime "updated_at", :null => false
end

create_table "upyun_images", :force => true do |t|
t.integer "user_id"
t.string "asset"
t.integer "size"
t.string "content_type"
t.string "filename"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

create_table "users", :force => true do |t|
t.string "email"
t.string "password_digest"
Expand Down

0 comments on commit 5387079

Please sign in to comment.