From 6e0082da9e46a35643b87369951bcb22175a8f5e Mon Sep 17 00:00:00 2001 From: Guillermo Date: Sun, 26 Sep 2010 11:03:47 -0500 Subject: [PATCH 1/4] Files are uploaded to S3 if the ENV variables are set (Heroku-friendly) --- Gemfile | 1 + Gemfile.lock | 7 +++++++ config/initializers/carrierwave.rb | 10 +++++++++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index cf863eb43..ece3df81c 100644 --- a/Gemfile +++ b/Gemfile @@ -36,6 +36,7 @@ gem 'magent', :git => 'http://github.com/dcu/magent.git' #File uploading gem 'carrierwave', :git => 'git://github.com/rsofaer/carrierwave.git' , :branch => 'master' #Untested mongomapper branch gem 'mini_magick' +gem 'aws' group :test, :development do gem 'factory_girl_rails' diff --git a/Gemfile.lock b/Gemfile.lock index ac66e59c1..c9f3289e1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -99,6 +99,10 @@ GEM arel (1.0.1) activesupport (~> 3.0.0) autotest (4.3.2) + aws (2.3.21) + http_connection + uuidtools + xml-simple bcrypt-ruby (2.1.2) bson (1.0.7) bson_ext (1.0.7) @@ -126,6 +130,7 @@ GEM haml (3.0.18) hashie (0.4.0) highline (1.6.1) + http_connection (1.3.1) i18n (0.4.1) json (1.4.6) lsof (0.3.0) @@ -218,6 +223,7 @@ GEM rack (>= 1.0) rack-test (>= 0.5.3) will_paginate (3.0.pre2) + xml-simple (1.0.12) PLATFORMS ruby @@ -225,6 +231,7 @@ PLATFORMS DEPENDENCIES addressable autotest + aws bson (= 1.0.7) bson_ext (= 1.0.7) bundler (= 1.0.0) diff --git a/config/initializers/carrierwave.rb b/config/initializers/carrierwave.rb index b28f1be66..ed1d07f91 100644 --- a/config/initializers/carrierwave.rb +++ b/config/initializers/carrierwave.rb @@ -3,5 +3,13 @@ # the COPYRIGHT file. CarrierWave.configure do |config| - config.storage = :file + if ENV['S3_KEY'] && ENV['S3_SECRET'] && ENV['S3_BUCKET'] + config.storage = :s3 + config.s3_access_key_id = ENV['S3_KEY'] + config.s3_secret_access_key = ENV['S3_SECRET'] + config.s3_bucket = ENV['S3_BUCKET'] + config.cache_dir = "#{Rails.root}/tmp/uploads" + else + config.storage = :file + end end From 8a92f1594812530e6438165fc378fdc525ff2241 Mon Sep 17 00:00:00 2001 From: Guillermo Date: Sun, 26 Sep 2010 11:04:19 -0500 Subject: [PATCH 2/4] storage isn't required here since is set in initializer --- app/uploaders/image_uploader.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/uploaders/image_uploader.rb b/app/uploaders/image_uploader.rb index 58399c90d..247f5f997 100644 --- a/app/uploaders/image_uploader.rb +++ b/app/uploaders/image_uploader.rb @@ -5,8 +5,6 @@ class ImageUploader < CarrierWave::Uploader::Base include CarrierWave::MiniMagick - storage :file - def store_dir "uploads/images" end From 86cbce288ba3d6ffa33c9b62a4cfa0ccdee25f69 Mon Sep 17 00:00:00 2001 From: Guillermo Date: Sun, 26 Sep 2010 11:05:17 -0500 Subject: [PATCH 3/4] Isn't necessary prepare URL if file is saved in S3 --- app/controllers/users_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 260b5b7fa..9fb2c2989 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -30,7 +30,7 @@ class UsersController < ApplicationController params[:profile].delete(:image_url) else url = APP_CONFIG[:pod_url].chop if APP_CONFIG[:pod_url][-1,1] == '/' - params[:profile][:image_url] = url + params[:profile][:image_url] + params[:profile][:image_url] = url + params[:profile][:image_url] if params[:profile][:image_url].at(0) == '/' end end From c835814ee199eadb19dae3fa53c1f58b98785d1d Mon Sep 17 00:00:00 2001 From: Guillermo Date: Mon, 27 Sep 2010 19:20:42 -0500 Subject: [PATCH 4/4] Merging prep_image_url with master --- app/controllers/users_controller.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 9fb2c2989..b5f352651 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -29,8 +29,11 @@ class UsersController < ApplicationController if params[:profile][:image_url].empty? params[:profile].delete(:image_url) else - url = APP_CONFIG[:pod_url].chop if APP_CONFIG[:pod_url][-1,1] == '/' - params[:profile][:image_url] = url + params[:profile][:image_url] if params[:profile][:image_url].at(0) == '/' + if /^http:\/\// =~ params[:profile][:image_url] + params[:profile][:image_url] = params[:profile][:image_url] + else + params[:profile][:image_url] = url + params[:profile][:image_url] + end end end