Permalink
Browse files

Converted file uploads to use carrierwave

  • Loading branch information...
1 parent 1218ff3 commit 332774697fcfa7fc19925099f8f15567048e14d5 @jkorz jkorz committed Feb 17, 2013
View
@@ -11,7 +11,11 @@ public/attachments
public/assets
config/initializers/secret_token.rb
config/initializers/setup_mail.rb
+config/initializers/carrierwave.rb
lib/development_mail_interceptor.rb
config/initializers/mail_settings.rb
config/initializers/omniauth.rb
+.idea
+Vagrantfile
+.vagrant
bin/
View
@@ -17,6 +17,8 @@ gem 'pg'
gem 'acts-as-taggable-on'
gem 'jquery-rails'
gem 'faker', :require => false
+gem 'carrierwave'
+gem 'fog', "~> 1.3.1"
################
# Mail #
View
@@ -45,6 +45,9 @@ GEM
rack-test (>= 0.5.4)
selenium-webdriver (~> 2.0)
xpath (~> 0.1.4)
+ carrierwave (0.8.0)
+ activemodel (>= 3.2.0)
+ activesupport (>= 3.2.0)
childprocess (0.3.8)
ffi (~> 1.0, >= 1.0.11)
chunky_png (1.2.7)
@@ -77,6 +80,7 @@ GEM
erubis (2.7.0)
exception_notification (3.0.1)
actionmailer (>= 3.0.4)
+ excon (0.13.4)
execjs (1.4.0)
multi_json (~> 1.0)
factory_girl (4.1.0)
@@ -89,6 +93,17 @@ GEM
faraday (0.8.5)
multipart-post (~> 1.1)
ffi (1.4.0)
+ fog (1.3.1)
+ builder
+ excon (~> 0.13.0)
+ formatador (~> 0.2.0)
+ mime-types
+ multi_json (~> 1.0)
+ net-scp (~> 1.0.4)
+ net-ssh (>= 2.1.3)
+ nokogiri (~> 1.5.0)
+ ruby-hmac
+ formatador (0.2.4)
fssm (0.2.10)
haml (4.0.0)
tilt
@@ -121,8 +136,8 @@ GEM
minitest (2.8.1)
multi_json (1.6.1)
multipart-post (1.1.5)
- net-scp (1.1.0)
- net-ssh (>= 2.6.5)
+ net-scp (1.0.4)
+ net-ssh (>= 1.99.1)
net-sftp (2.1.1)
net-ssh (>= 2.6.5)
net-ssh (2.6.5)
@@ -175,6 +190,7 @@ GEM
json (~> 1.4)
redcarpet (2.1.0)
ref (1.0.2)
+ ruby-hmac (0.4.0)
ruby-progressbar (0.0.10)
rubyzip (0.9.9)
sass (3.2.5)
@@ -218,6 +234,7 @@ DEPENDENCIES
acts-as-taggable-on
capistrano
capybara (~> 1.1.1)
+ carrierwave
cocoon
coffee-rails (~> 3.2.0)
colorific (~> 1.0.0)
@@ -228,6 +245,7 @@ DEPENDENCIES
exception_notification
factory_girl_rails (~> 4.1.0)
faker
+ fog (~> 1.3.1)
haml
jquery-rails
json (~> 1.7.7)
View
@@ -1,39 +1,11 @@
class Attachment < ActiveRecord::Base
- belongs_to :puzzle
-
- before_save :save_file
- before_destroy :delete_file
-
- validates_presence_of :file_name
- validates_uniqueness_of :file_name, :scope => :puzzle_id
-
- def file=(tempfile)
- self.file_name = File.basename(tempfile.original_filename)
- @tempfile = tempfile
- end
-
- def file_path
- File.join(Rails.root, 'public', 'attachments', puzzle.id.to_s, file_name)
- end
+ mount_uploader :file, FileUploader
- def directory
- File.join(Rails.root, 'public', 'attachments', puzzle.id.to_s)
- end
-
- def public_path
- "/puzzles/#{puzzle.slug}/attachments/#{file_name}"
- end
-
- private
+ belongs_to :puzzle
+ before_destroy :remove_file!
- def save_file
- if puzzle && @tempfile
- FileUtils.mkdir_p(directory)
- File.open(file_path, "wb") { |f| f.write(@tempfile.read) }
- end
+ def file_name
+ file.file.filename.split('?')[0]
end
- def delete_file
- FileUtils.rm(file_path) if File.exists?(file_path)
- end
end
@@ -0,0 +1,53 @@
+# encoding: utf-8
+
+class FileUploader < CarrierWave::Uploader::Base
+
+ # Include RMagick or MiniMagick support:
+ # include CarrierWave::RMagick
+ # include CarrierWave::MiniMagick
+
+ # Choose what kind of storage to use for this uploader:
+
+ if Rails.env.test? or Rails.env.cucumber?
+ storage :file
+ else
+ storage :fog
+ end
+
+
+ # 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
+ "uploads/#{model.id}/"
+ end
+
+ # Provide a default URL as a default if there hasn't been a file uploaded:
+ # def default_url
+ # "/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
@@ -16,5 +16,5 @@
%h2 Files
#files
= file_link "#{@puzzle.slug}.md", puzzle_path(@puzzle, :format => "md")
- - @puzzle.attachments.map do |file|
- = file_link file.file_name, file.public_path
+ - @puzzle.attachments.map do |attachment|
+ = file_link attachment.file_name, attachment.file_url
@@ -0,0 +1,13 @@
+CarrierWave.configure do |config|
+ config.fog_credentials = {
+ :provider => 'AWS', # required
+ :aws_access_key_id => 'xxx', # required
+ :aws_secret_access_key => 'yyy', # required
+ :region => 'eu-west-1', # optional, defaults to 'us-east-1'
+ :host => 's3.example.com', # optional, defaults to nil
+ :endpoint => 'https://s3.example.com:8080' # optional, defaults to nil
+ }
+ config.fog_directory = 'name_of_directory' # required
+ config.fog_public = false # optional, defaults to true
+ config.fog_attributes = {'Cache-Control'=>'max-age=315576000'} # optional, defaults to {}
+end
@@ -0,0 +1,9 @@
+class AttachmentRenameFileNameToFile < ActiveRecord::Migration
+ def up
+ rename_column :attachments, :file_name, :file
+ end
+
+ def down
+ rename_column :attachments, :file, :file_name
+ end
+end
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120222154408) do
+ActiveRecord::Schema.define(:version => 20130216195839) do
create_table "announcements", :force => true do |t|
t.text "body"
@@ -23,7 +23,7 @@
create_table "attachments", :force => true do |t|
t.integer "puzzle_id"
- t.string "file_name"
+ t.string "file"
t.string "description"
t.datetime "created_at"
t.datetime "updated_at"
View
@@ -26,7 +26,7 @@ def create_submission(puzzle, user, correct)
end
def cleanup_attachment(attachment)
- FileUtils.rm_rf(attachment.directory)
+ FileUtils.rm_rf(attachment.file.path)
end
end
@@ -25,7 +25,7 @@ class AttachmentTest < ActiveSupport::TestCase
attachment = puzzle.attachments.create(:file => @uploaded_file)
- assert File.exists?(attachment.file_path),
+ assert File.exists?(attachment.file.path),
"File was not written to public directory"
cleanup_attachment(attachment)
@@ -36,12 +36,14 @@ class AttachmentTest < ActiveSupport::TestCase
attachment = puzzle.attachments.create(:file => @uploaded_file)
- assert File.exists?(attachment.file_path),
+ path = attachment.file.path
+
+ assert File.exists?(path),
"File was not written to public directory"
attachment.destroy
- refute File.exists?(attachment.file_path),
+ refute File.exists?(path),
"File was not removed from the public directory"
end
@@ -50,7 +52,9 @@ class AttachmentTest < ActiveSupport::TestCase
attachment = puzzle.attachments.create(:file => @uploaded_file)
- assert File.exists?(attachment.file_path),
+ path = attachment.file.path
+
+ assert File.exists?(path),
"File was not written to public directory"
cleanup_attachment(attachment)
@@ -59,7 +63,7 @@ class AttachmentTest < ActiveSupport::TestCase
assert attachment.destroyed?, "Attachment was not destroyed"
- refute File.exists?(attachment.file_path),
+ refute File.exists?(path),
"File was not removed from the public directory"
end
end

0 comments on commit 3327746

Please sign in to comment.