mezza-bcms_s3 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.markdown +64 -0
- data/lib/bcms_s3.rb +2 -0
- data/lib/bcms_s3/routes.rb +7 -0
- data/lib/bcms_s3/s3_module.rb +138 -0
- data/templates/blank.rb +31 -0
- metadata +66 -0
data/README.markdown
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# A [BrowserCMS](http://www.browsercms.org) module to allow storage of images and files on the Amazon S3 storage facility
|
2
|
+
## Using S3 for file storage
|
3
|
+
To enable S3 file storage set Cms::S3.enabled in config/initializers/browsercms.rb (create this if it does not exist) to true. Ensure that you as provide a s3.yml file that contains your credentials and bucket.
|
4
|
+
This should be in the following format
|
5
|
+
|
6
|
+
access_key_id: your AWS access key
|
7
|
+
secret_access_key: your AWS secret access key
|
8
|
+
bucket: your unique bucket name
|
9
|
+
|
10
|
+
## Using this module with [Heroku](http://heroku.com)
|
11
|
+
If using this module in conjunction with deployment on heroku you should probably turning heroku caching on by setting Cms::S3.heroku_caching in config/initializers/browsercms.rb to true.
|
12
|
+
|
13
|
+
In order to avoid putting your secret AWS key in the s3.yml file, you can take advantage of [heroku's config vars](http://docs.heroku.com/config-vars). Use ERB to read the values from the environment. This way you can safely commit your s3.yml file to the repository without revealing your amazon credentials.
|
14
|
+
|
15
|
+
access_key_id: <%= ENV['s3_access_key_id'] %>
|
16
|
+
secret_access_key: <%= ENV['s3_secret_access_key'] %>
|
17
|
+
bucket: <%= ENV['s3_bucket'] %>
|
18
|
+
|
19
|
+
For developing on your local machine, export the s3 variables to your environment.
|
20
|
+
|
21
|
+
export s3_access_key_id='your AWS access key'
|
22
|
+
export s3_secret_access_key='your AWS secret access key'
|
23
|
+
export s3_bucket='your unique bucket name'
|
24
|
+
|
25
|
+
Set the config vars on heroku to get it working there as well.
|
26
|
+
|
27
|
+
heroku config:add s3_access_key_id='your AWS access key'
|
28
|
+
heroku config:add s3_secret_access_key='your AWS secret access key'
|
29
|
+
heroku config:add s3_bucket='your unique bucket name'
|
30
|
+
|
31
|
+
## www prefix for non cms urls
|
32
|
+
If your non cms domain is www.myapp.com rather than app.com this can be enabled by setting Cms::S3.www_domain_prefix in config/initializers/browsercms.rb to true.
|
33
|
+
|
34
|
+
## using cnames to your S3 bucket
|
35
|
+
If you've set up CNAMES in your DNS to point to your bucket, then you can enable the use of that instead of the FQDN ending in amazonaws.com by setting Cms::S3.options[:s3_cname] in your s3.yml file.
|
36
|
+
|
37
|
+
## Important things to note
|
38
|
+
1. The s3.yml should be excluded from public repositories (e.g github) since it contains your secret AWS key which should **never** be revealed to the public.
|
39
|
+
**Please note**. This no longer applies since the access keys and buckets are now specified in environmental variables and therefore the s3.yml file now contains just references to these environmental variables.
|
40
|
+
2. Changing from local storage to S3 storage will require you to re-upload all your files (or copy the tree to s3)
|
41
|
+
3. This module requires the RightAWS gem from RightScale (sudo gem install right_aws)
|
42
|
+
|
43
|
+
##### Based on original work on S3 storage for BrowserCMS by [Neil Middleton](http://github.com/neilmiddleton/)
|
44
|
+
|
45
|
+
## Important note for use with Rails 3 and BCMS 3.1.x-axes
|
46
|
+
|
47
|
+
Due to the use of Engines, I can't work out how to extend the Cms::AttachmentsController and Models from within the module, so you will need to add the block below in the initializer for browsercms.rb
|
48
|
+
|
49
|
+
Cms::AttachmentsController.send(:include, Cms::S3::AttachmentsController)
|
50
|
+
Cms::ContentController.send(:include, Cms::S3::ContentController)
|
51
|
+
Attachment.send(:include, Cms::S3::Attachment)
|
52
|
+
Cms::ApplicationController.send(:include, Cms::S3::ApplicationController)
|
53
|
+
# ensure S3 storage disabled by default
|
54
|
+
Cms::S3.enabled = false if Cms::S3.enabled.nil?
|
55
|
+
# ensure heroku caching disabled by default
|
56
|
+
Cms::S3.heroku_caching = false if Cms::S3.heroku_caching.nil?
|
57
|
+
# function to set domain prefix without url to 'www' is disabled by default
|
58
|
+
Cms::S3.www_domain_prefix = false if Cms::S3.www_domain_prefix.nil?
|
59
|
+
# load s3 options if s3.yml exists
|
60
|
+
if File.exists?("#{RAILS_ROOT}/config/s3.yml")
|
61
|
+
yaml_string = IO.read("#{RAILS_ROOT}/config/s3.yml")
|
62
|
+
Cms::S3.options = YAML::load(ERB.new(yaml_string).result)
|
63
|
+
Cms::S3.options.symbolize_keys!
|
64
|
+
end
|
data/lib/bcms_s3.rb
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
require 'right_aws'
|
2
|
+
module Cms
|
3
|
+
module S3
|
4
|
+
class << self
|
5
|
+
attr_accessor :enabled
|
6
|
+
attr_accessor :heroku_caching
|
7
|
+
attr_accessor :www_domain_prefix
|
8
|
+
attr_accessor :options
|
9
|
+
end
|
10
|
+
module AttachmentsController
|
11
|
+
def self.included(controller_class)
|
12
|
+
controller_class.alias_method_chain :show, :s3
|
13
|
+
end
|
14
|
+
|
15
|
+
def show_with_s3
|
16
|
+
@attachment = ::Attachment.find(params[:id])
|
17
|
+
@attachment = @attachment.as_of_version(params[:version]) if params[:version]
|
18
|
+
if Cms::S3.enabled
|
19
|
+
#get the file off S3
|
20
|
+
if Cms::S3.options[:s3_cname]
|
21
|
+
redirect_to("http://#{Cms::S3.options[:s3_cname]}/#{@attachment.file_location}")
|
22
|
+
else
|
23
|
+
redirect_to("http://#{Cms::S3.options[:bucket]}.s3.amazonaws.com/#{@attachment.file_location}")
|
24
|
+
end
|
25
|
+
else
|
26
|
+
#Construct a path to where this file would be if it were cached
|
27
|
+
@file = @attachment.full_file_location
|
28
|
+
|
29
|
+
#Stream the file if it exists
|
30
|
+
if @path != "/" && File.exists?(@file)
|
31
|
+
send_file(@file,
|
32
|
+
:filename => @attachment.file_name,
|
33
|
+
:type => @attachment.file_type,
|
34
|
+
:disposition => "inline"
|
35
|
+
)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
module ContentController
|
41
|
+
def self.included(controller_class)
|
42
|
+
controller_class.alias_method_chain :render_page_with_caching, :s3
|
43
|
+
controller_class.alias_method_chain :try_to_stream_file, :s3
|
44
|
+
end
|
45
|
+
def render_page_with_caching_with_s3
|
46
|
+
render_page
|
47
|
+
response.headers['Cache-Control'] = 'public, max-age=300' if Cms::S3.heroku_caching
|
48
|
+
end
|
49
|
+
def try_to_stream_file_with_s3
|
50
|
+
split = @paths.last.to_s.split('.')
|
51
|
+
ext = split.size > 1 ? split.last.to_s.downcase : nil
|
52
|
+
|
53
|
+
#Only try to stream cache file if it has an extension
|
54
|
+
unless ext.blank?
|
55
|
+
|
56
|
+
#Check access to file
|
57
|
+
@attachment = ::Attachment.find_live_by_file_path(@path)
|
58
|
+
if @attachment
|
59
|
+
raise Cms::Errors::AccessDenied unless current_user.able_to_view?(@attachment)
|
60
|
+
|
61
|
+
if Cms::S3.enabled
|
62
|
+
#get the file off S3
|
63
|
+
if Cms::S3.options[:s3_cname]
|
64
|
+
redirect_to("http://#{Cms::S3.options[:s3_cname]}/#{@attachment.file_location}")
|
65
|
+
else
|
66
|
+
redirect_to("http://#{Cms::S3.options[:bucket]}.s3.amazonaws.com/#{@attachment.file_location}")
|
67
|
+
end
|
68
|
+
else
|
69
|
+
#Construct a path to where this file would be if it were cached
|
70
|
+
@file = @attachment.full_file_location
|
71
|
+
|
72
|
+
#Stream the file if it exists
|
73
|
+
if @path != "/" && File.exists?(@file)
|
74
|
+
send_file(@file,
|
75
|
+
:filename => @attachment.file_name,
|
76
|
+
:type => @attachment.file_type,
|
77
|
+
:disposition => "inline"
|
78
|
+
)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
module Attachment
|
86
|
+
def self.included(model_class)
|
87
|
+
model_class.alias_method_chain :set_file_location, :s3
|
88
|
+
model_class.alias_method_chain :write_temp_file_to_storage_location, :s3
|
89
|
+
end
|
90
|
+
def set_file_location_with_s3
|
91
|
+
unless temp_file.blank?
|
92
|
+
prefix = temp_file.original_filename.sub(/\..+$/,'')
|
93
|
+
if temp_file.original_filename =~ /.+(\..+)$/
|
94
|
+
suffix = $1
|
95
|
+
else
|
96
|
+
suffix = ""
|
97
|
+
end
|
98
|
+
new_filename = "#{prefix}-#{ActiveSupport::SecureRandom.hex(4)}#{suffix}"
|
99
|
+
self.file_location = "#{Time.now.strftime("%Y/%m/%d")}/#{new_filename}"
|
100
|
+
end
|
101
|
+
end
|
102
|
+
def write_temp_file_to_storage_location_with_s3
|
103
|
+
unless temp_file.blank?
|
104
|
+
FileUtils.mkdir_p File.dirname(full_file_location) if !Cms::S3.enabled
|
105
|
+
if temp_file.local_path
|
106
|
+
|
107
|
+
if Cms::S3.enabled
|
108
|
+
s3 = RightAws::S3.new(Cms::S3.options[:access_key_id], Cms::S3.options[:secret_access_key] )
|
109
|
+
bucket = s3.bucket(Cms::S3.options[:bucket], true, 'public-read')
|
110
|
+
key = RightAws::S3::Key.create(bucket, file_location)
|
111
|
+
key.put(temp_file.read,'public-read', {"Content-Type" => file_type})
|
112
|
+
else
|
113
|
+
FileUtils.copy temp_file.local_path, full_file_location
|
114
|
+
end
|
115
|
+
else
|
116
|
+
open(full_file_location, 'w') {|f| f << temp_file.read }
|
117
|
+
end
|
118
|
+
|
119
|
+
if Cms.attachment_file_permission && !Cms::S3.enabled
|
120
|
+
FileUtils.chmod Cms.attachment_file_permission, full_file_location
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
module ApplicationController
|
126
|
+
def self.included(controller_class)
|
127
|
+
controller_class.alias_method_chain :url_without_cms_domain_prefix, :www
|
128
|
+
end
|
129
|
+
def url_without_cms_domain_prefix_with_www
|
130
|
+
if Cms::S3.www_domain_prefix
|
131
|
+
request.url.sub(/#{cms_domain_prefix}\./,'www.')
|
132
|
+
else
|
133
|
+
request.url.sub(/#{cms_domain_prefix}\./,'')
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
data/templates/blank.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# Remove the file on both *unix and Windows
|
2
|
+
if Gem.win_platform?
|
3
|
+
run "del public\\index.html"
|
4
|
+
else
|
5
|
+
run "rm public/index.html"
|
6
|
+
end
|
7
|
+
|
8
|
+
gem "browsercms"
|
9
|
+
if Gem.win_platform?
|
10
|
+
puts " rake db:create"
|
11
|
+
`rake.cmd db:create`
|
12
|
+
else
|
13
|
+
rake "db:create"
|
14
|
+
end
|
15
|
+
route "map.routes_for_browser_cms"
|
16
|
+
generate :browser_cms
|
17
|
+
environment 'SITE_DOMAIN="localhost:3000"', :env => "development"
|
18
|
+
environment 'SITE_DOMAIN="localhost:3000"', :env => "test"
|
19
|
+
environment 'SITE_DOMAIN="localhost:3000"', :env => "production"
|
20
|
+
environment 'config.action_view.cache_template_loading = false', :env => "production"
|
21
|
+
environment 'config.action_controller.page_cache_directory = RAILS_ROOT + "/public/cache/"', :env => "production"
|
22
|
+
initializer 'browsercms.rb', <<-CODE
|
23
|
+
Cms.attachment_file_permission = 0640
|
24
|
+
Cms::S3.enabled = false
|
25
|
+
Cms::S3.heroku_caching = false
|
26
|
+
CODE
|
27
|
+
if Gem.win_platform?
|
28
|
+
puts " rake db:migrate"
|
29
|
+
`rake.cmd db:migrate`
|
30
|
+
else
|
31
|
+
rak
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mezza-bcms_s3
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 3
|
8
|
+
- 0
|
9
|
+
version: 0.3.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Anthony Underwood
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-08-25 00:00:00 +01:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description:
|
22
|
+
email: email2ants@gmail.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- README.markdown
|
29
|
+
files:
|
30
|
+
- lib/bcms_s3.rb
|
31
|
+
- lib/bcms_s3/routes.rb
|
32
|
+
- lib/bcms_s3/s3_module.rb
|
33
|
+
- templates/blank.rb
|
34
|
+
- README.markdown
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: http://github.com/aunderwo/bcms_s3
|
37
|
+
licenses: []
|
38
|
+
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options:
|
41
|
+
- --charset=UTF-8
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
segments:
|
49
|
+
- 0
|
50
|
+
version: "0"
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
version: "0"
|
58
|
+
requirements: []
|
59
|
+
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 1.3.6
|
62
|
+
signing_key:
|
63
|
+
specification_version: 3
|
64
|
+
summary: This is a browsercms (browsercms.org) module to allow the facility to have attachments stored on Amazon S3. Also there is the option to change caching to suit heroku and/or use 'www' as the prefix for the non-cms site. Based on original work by Neil Middleton
|
65
|
+
test_files: []
|
66
|
+
|