bcms_s3 0.1.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 ADDED
@@ -0,0 +1,16 @@
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::Module.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::Module.heroku_caching in config/initializers/browsercms.rb to true.
12
+
13
+ ## Important things to note
14
+ 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.
15
+ 2. Changing from local storage to S3 storage will require you to re-upload all your files (or copy the tree to s3)
16
+ 3. This module requires the RightAWS gem from RightScale (sudo gem install right_aws)
@@ -0,0 +1,7 @@
1
+ module Cms::Routes
2
+ def routes_for_bcms_s3
3
+ namespace(:cms) do |cms|
4
+ #cms.content_blocks :s3s
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,102 @@
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 :options
8
+ end
9
+ module ContentController
10
+ def self.included(controller_class)
11
+ controller_class.alias_method_chain :render_page_with_caching, :s3
12
+ controller_class.alias_method_chain :try_to_stream_file, :s3
13
+ end
14
+ def render_page_with_caching_with_s3
15
+ render_page
16
+ response.headers['Cache-Control'] = 'public, max-age=300' if Cms::S3.heroku_caching
17
+ end
18
+ def try_to_stream_file_with_s3
19
+ split = @paths.last.to_s.split('.')
20
+ ext = split.size > 1 ? split.last.to_s.downcase : nil
21
+
22
+ #Only try to stream cache file if it has an extension
23
+ unless ext.blank?
24
+
25
+ #Check access to file
26
+ @attachment = ::Attachment.find_live_by_file_path(@path)
27
+ if @attachment
28
+ raise Cms::Errors::AccessDenied unless current_user.able_to_view?(@attachment)
29
+
30
+ if Cms::S3.enabled
31
+ #get the file off S3
32
+ redirect_to("http://#{Cms::S3.options[:bucket]}.s3.amazonaws.com/#{@attachment.file_location}")
33
+ else
34
+ #Construct a path to where this file would be if it were cached
35
+ @file = @attachment.full_file_location
36
+
37
+ #Stream the file if it exists
38
+ if @path != "/" && File.exists?(@file)
39
+ send_file(@file,
40
+ :filename => @attachment.file_name,
41
+ :type => @attachment.file_type,
42
+ :disposition => "inline"
43
+ )
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ module Attachment
51
+ def self.included(model_class)
52
+ model_class.alias_method_chain :set_file_location, :s3
53
+ model_class.alias_method_chain :write_temp_file_to_storage_location, :s3
54
+ end
55
+ def set_file_location_with_s3
56
+ unless temp_file.blank?
57
+ prefix = temp_file.original_filename.sub(/\..+$/,'')
58
+ if temp_file.original_filename =~ /.+(\..+)$/
59
+ suffix = $1
60
+ else
61
+ suffix = ""
62
+ end
63
+ new_filename = "#{prefix}-#{ActiveSupport::SecureRandom.hex(4)}#{suffix}"
64
+ self.file_location = "#{Time.now.strftime("%Y/%m/%d")}/#{new_filename}"
65
+ end
66
+ end
67
+ def write_temp_file_to_storage_location_with_s3
68
+ unless temp_file.blank?
69
+ FileUtils.mkdir_p File.dirname(full_file_location) if !Cms::S3.enabled
70
+ if temp_file.local_path
71
+
72
+ if Cms::S3.enabled
73
+ s3 = RightAws::S3.new(Cms::S3.options[:access_key_id], Cms::S3.options[:secret_access_key] )
74
+ bucket = s3.bucket(Cms::S3.options[:bucket], true, 'public-read')
75
+ key = RightAws::S3::Key.create(bucket, file_location)
76
+ key.put(temp_file.read,'public-read', {"Content-Type" => file_type})
77
+ else
78
+ FileUtils.copy temp_file.local_path, full_file_location
79
+ end
80
+ else
81
+ open(full_file_location, 'w') {|f| f << temp_file.read }
82
+ end
83
+
84
+ if Cms.attachment_file_permission && !Cms::S3.enabled
85
+ FileUtils.chmod Cms.attachment_file_permission, full_file_location
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
92
+
93
+ Cms::ContentController.send(:include, Cms::S3::ContentController)
94
+ Attachment.send(:include, Cms::S3::Attachment)
95
+ # ensure S3 storage disabled by default
96
+ Cms::S3.enabled = false if Cms::S3.enabled.nil?
97
+ # ensure heroku caching disabled by default
98
+ Cms::S3.heroku_caching = false if Cms::S3.heroku_caching.nil?
99
+ if File.exists?("#{RAILS_ROOT}/config/s3.yml")
100
+ Cms::S3.options = YAML.load_file("#{RAILS_ROOT}/config/s3.yml")
101
+ Cms::S3.options.symbolize_keys!
102
+ end
data/lib/bcms_s3.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'bcms_s3/routes'
2
+ require 'bcms_s3/s3_module'
@@ -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,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bcms_s3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Anthony Underwood
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-25 00:00:00 +00:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: email2ants@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.markdown
24
+ files:
25
+ - lib/bcms_s3.rb
26
+ - lib/bcms_s3/routes.rb
27
+ - lib/bcms_s3/s3_module.rb
28
+ - templates/blank.rb
29
+ - README.markdown
30
+ has_rdoc: true
31
+ homepage: http://github.com/aunderwo/bcms_s3
32
+ licenses: []
33
+
34
+ post_install_message:
35
+ rdoc_options:
36
+ - --charset=UTF-8
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ requirements: []
52
+
53
+ rubyforge_project:
54
+ rubygems_version: 1.3.5
55
+ signing_key:
56
+ specification_version: 3
57
+ 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
58
+ test_files: []
59
+