Created
October 14, 2009 14:05
-
-
Save neilmiddleton/210095 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 63254641c717dbdafdeb92fa0a3b70034d237a91 Mon Sep 17 00:00:00 2001 | |
From: Neil Middleton <[email protected]> | |
Date: Thu, 15 Oct 2009 14:32:33 +0100 | |
Subject: [PATCH 2/2] Changes to enable bcms deploment on heroku | |
--- | |
.gems | 1 + | |
config/environment.rb | 1 + | |
config/initializers/browsercms.rb | 7 ++++ | |
config/s3.yml | 9 +++++ | |
.../app/controllers/cms/content_controller.rb | 29 ++++++++++------- | |
.../gems/browsercms-3.0.2/app/models/attachment.rb | 34 ++++++++++++++++++-- | |
.../lib/cms/behaviors/attaching.rb | 2 +- | |
.../lib/cms/behaviors/versioning.rb | 2 +- | |
vendor/gems/browsercms-3.0.2/lib/cms/init.rb | 4 ++ | |
9 files changed, 72 insertions(+), 17 deletions(-) | |
create mode 100644 .gems | |
create mode 100644 config/s3.yml | |
diff --git a/.gems b/.gems | |
new file mode 100644 | |
index 0000000..05d9ca9 | |
--- /dev/null | |
+++ b/.gems | |
@@ -0,0 +1 @@ | |
+right_aws | |
\ No newline at end of file | |
diff --git a/config/environment.rb b/config/environment.rb | |
index 71955d0..66d2b45 100644 | |
--- a/config/environment.rb | |
+++ b/config/environment.rb | |
@@ -7,6 +7,7 @@ RAILS_GEM_VERSION = '2.3.4' unless defined? RAILS_GEM_VERSION | |
require File.join(File.dirname(__FILE__), 'boot') | |
Rails::Initializer.run do |config| | |
+ config.gem "right_aws" | |
config.gem 'browsercms' | |
# Settings in config/environments/* take precedence over those specified here. | |
diff --git a/config/initializers/browsercms.rb b/config/initializers/browsercms.rb | |
index 8d0dfad..1d17d3b 100644 | |
--- a/config/initializers/browsercms.rb | |
+++ b/config/initializers/browsercms.rb | |
@@ -1 +1,8 @@ | |
Cms.attachment_file_permission = 0640 | |
+ | |
+Cms.file_storage_on_s3 = true | |
+ | |
+Cms.s3_options = { | |
+ :s3_bucket => "YOUR_BUCKET_NAME", | |
+ :s3_credentials => "#{RAILS_ROOT}/config/s3.yml" | |
+} | |
\ No newline at end of file | |
diff --git a/config/s3.yml b/config/s3.yml | |
new file mode 100644 | |
index 0000000..528afdb | |
--- /dev/null | |
+++ b/config/s3.yml | |
@@ -0,0 +1,9 @@ | |
+development: | |
+ access_key_id: YOUR_ACCESS_KEY | |
+ secret_access_key: YOUR_SECRET_ACCESS_KEY | |
+test: | |
+ access_key_id: YOUR_ACCESS_KEY | |
+ secret_access_key: YOUR_SECRET_ACCESS_KEY | |
+production: | |
+ access_key_id: YOUR_ACCESS_KEY | |
+ secret_access_key: YOUR_SECRET_ACCESS_KEY | |
\ No newline at end of file | |
diff --git a/vendor/gems/browsercms-3.0.2/app/controllers/cms/content_controller.rb b/vendor/gems/browsercms-3.0.2/app/controllers/cms/content_controller.rb | |
index 7e441ec..ccb6757 100644 | |
--- a/vendor/gems/browsercms-3.0.2/app/controllers/cms/content_controller.rb | |
+++ b/vendor/gems/browsercms-3.0.2/app/controllers/cms/content_controller.rb | |
@@ -63,7 +63,8 @@ class Cms::ContentController < Cms::ApplicationController | |
def render_page_with_caching | |
render_page | |
- cache_page if perform_caching | |
+ response.headers['Cache-Control'] = 'public, max-age=300' if perform_caching | |
+ #cache_page if perform_caching | |
end | |
# This is the method all error handlers delegate to | |
@@ -156,17 +157,21 @@ class Cms::ContentController < Cms::ApplicationController | |
@attachment = Attachment.find_live_by_file_path(@path) | |
if @attachment | |
raise Cms::Errors::AccessDenied unless current_user.able_to_view?(@attachment) | |
- | |
- #Construct a path to where this file would be if it were cached | |
- @file = @attachment.full_file_location | |
- | |
- #Stream the file if it exists | |
- if @path != "/" && File.exists?(@file) | |
- send_file(@file, | |
- :filename => @attachment.file_name, | |
- :type => @attachment.file_type, | |
- :disposition => "inline" | |
- ) | |
+ if Cms.file_storage_on_s3 | |
+ #get the file off S3 | |
+ redirect_to("http://#{Cms.s3_options[:s3_bucket]}.s3.amazonaws.com/#{@attachment.file_location}") | |
+ else | |
+ #Construct a path to where this file would be if it were cached | |
+ @file = @attachment.full_file_location | |
+ | |
+ #Stream the file if it exists | |
+ if @path != "/" && File.exists?(@file) | |
+ send_file(@file, | |
+ :filename => @attachment.file_name, | |
+ :type => @attachment.file_type, | |
+ :disposition => "inline" | |
+ ) | |
+ end | |
end | |
end | |
end | |
diff --git a/vendor/gems/browsercms-3.0.2/app/models/attachment.rb b/vendor/gems/browsercms-3.0.2/app/models/attachment.rb | |
index 066c7ab..c56f4d8 100644 | |
--- a/vendor/gems/browsercms-3.0.2/app/models/attachment.rb | |
+++ b/vendor/gems/browsercms-3.0.2/app/models/attachment.rb | |
@@ -114,19 +114,47 @@ class Attachment < ActiveRecord::Base | |
def write_temp_file_to_storage_location | |
unless temp_file.blank? | |
- FileUtils.mkdir_p File.dirname(full_file_location) | |
+ FileUtils.mkdir_p File.dirname(full_file_location) if !Cms.file_storage_on_s3 | |
if temp_file.local_path | |
- File.copy temp_file.local_path, full_file_location | |
+ if Cms.file_storage_on_s3 | |
+ credentials = parse_credentials(Cms.s3_options[:s3_credentials]) | |
+ debugger | |
+ s3 = RightAws::S3.new(credentials[:access_key_id], credentials[:secret_access_key] ) | |
+ bucket = s3.bucket(Cms.s3_options[:s3_bucket], true, 'public-read') | |
+ key = RightAws::S3::Key.create(bucket, file_location) | |
+ key.put(temp_file.read,'public-read') | |
+ else | |
+ File.copy temp_file.local_path, full_file_location | |
+ end | |
else | |
open(full_file_location, 'w') {|f| f << temp_file.read } | |
end | |
- if Cms.attachment_file_permission | |
+ if Cms.attachment_file_permission && !Cms.file_storage_on_s3 | |
FileUtils.chmod Cms.attachment_file_permission, full_file_location | |
end | |
end | |
end | |
+ def parse_credentials creds | |
+ creds = find_credentials(creds).stringify_keys | |
+ (creds[RAILS_ENV] || creds).symbolize_keys | |
+ end | |
+ | |
+ def find_credentials creds | |
+ case creds | |
+ when File | |
+ YAML.load_file(creds.path) | |
+ when String | |
+ YAML.load_file(creds) | |
+ when Hash | |
+ creds | |
+ else | |
+ raise ArgumentError, "Credentials are not a path, file, or hash." | |
+ end | |
+ end | |
+ private :find_credentials | |
+ | |
def clear_ivars | |
@temp_file = nil | |
@section = nil | |
diff --git a/vendor/gems/browsercms-3.0.2/lib/cms/behaviors/attaching.rb b/vendor/gems/browsercms-3.0.2/lib/cms/behaviors/attaching.rb | |
index e7a840f..5995ec0 100644 | |
--- a/vendor/gems/browsercms-3.0.2/lib/cms/behaviors/attaching.rb | |
+++ b/vendor/gems/browsercms-3.0.2/lib/cms/behaviors/attaching.rb | |
@@ -16,7 +16,7 @@ module Cms | |
before_validation :process_attachment | |
before_save :update_attachment_if_changed | |
after_save :clear_attachment_ivars | |
- belongs_to :attachment, :dependent => :destroy | |
+ belongs_to :attachment, :dependent => :destroy , :class_name => '::Attachment' | |
validates_each :attachment_file do |record, attr, value| | |
if record.attachment && !record.attachment.valid? | |
diff --git a/vendor/gems/browsercms-3.0.2/lib/cms/behaviors/versioning.rb b/vendor/gems/browsercms-3.0.2/lib/cms/behaviors/versioning.rb | |
index 9853807..7714662 100644 | |
--- a/vendor/gems/browsercms-3.0.2/lib/cms/behaviors/versioning.rb | |
+++ b/vendor/gems/browsercms-3.0.2/lib/cms/behaviors/versioning.rb | |
@@ -52,7 +52,7 @@ module Cms | |
end | |
def version_class_name | |
- "#{name}::Version" | |
+ "::#{name}::Version" | |
end | |
def version_foreign_key | |
diff --git a/vendor/gems/browsercms-3.0.2/lib/cms/init.rb b/vendor/gems/browsercms-3.0.2/lib/cms/init.rb | |
index 092d614..30d024a 100644 | |
--- a/vendor/gems/browsercms-3.0.2/lib/cms/init.rb | |
+++ b/vendor/gems/browsercms-3.0.2/lib/cms/init.rb | |
@@ -7,6 +7,8 @@ module Cms | |
::SPEC = eval(File.read(__root__ + '/browsercms.gemspec')) | |
attr_accessor :attachment_file_permission | |
+ attr_accessor :file_storage_on_s3 | |
+ attr_accessor :s3_options | |
def version | |
@version = SPEC.version.version | |
@@ -31,6 +33,8 @@ module Cms | |
# so they could be blown away on a server restart or something | |
# so this just makes sure they get written out | |
DynamicView.write_all_to_disk! if DynamicView.table_exists? | |
+ | |
+ Cms.file_storage_on_s3 if Cms.file_storage_on_s3.nil? | |
end | |
# This is used by CMS modules to register with the CMS generator | |
-- | |
1.6.2.2 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment