paperclip-aws-core 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fd32454d50abf1f22884abcd2920a87f664fc670
4
+ data.tar.gz: 5b9b2fe30fe9a9a01f3966fd2bd218d6f23318ce
5
+ SHA512:
6
+ metadata.gz: 6a93e1134a264d54cf359ddff043b6d4c808676aa846b70b4bab40a57c2fec281c53e11b71b47eb60f2195a7ff7c4181b6bdee19fac8e7abbfa5edb44a05326b
7
+ data.tar.gz: 5cdd9aa851649df3b0c509368a03351a1c6f2e22e1ecc1caabac57bf0a7588fc65ed76e160cb2a95e8c4a83a5f0e156dbbae02ea2e3889b6ce66fc5933197ffd
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in paperclip-aws-core.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Bill Centinaro
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Paperclip::Aws::Core
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'paperclip-aws-core'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install paperclip-aws-core
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/<my-github-username>/paperclip-aws-core/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,7 @@
1
+ module Paperclip
2
+ module Aws
3
+ module Core
4
+ VERSION = "0.0.1"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,218 @@
1
+ require "paperclip/aws/core/version"
2
+
3
+ module Paperclip
4
+ module Storage
5
+ module AwsCore
6
+ def self.extended base
7
+ begin
8
+ require 'aws-core-sdk'
9
+ rescue LoadError => e
10
+ e.message << " (You may need to install the aws-core-sdk gem)"
11
+ raise e
12
+ end unless defined?(Aws)
13
+
14
+ # TODO: Covert the Log Formating
15
+ # # Overriding log formatter to make sure it return a UTF-8 string
16
+ # if defined?(AWS::Core::LogFormatter)
17
+ # AWS::Core::LogFormatter.class_eval do
18
+ # def summarize_hash(hash)
19
+ # hash.map { |key, value| ":#{key}=>#{summarize_value(value)}".force_encoding('UTF-8') }.sort.join(',')
20
+ # end
21
+ # end
22
+ # elsif defined?(AWS::Core::ClientLogging)
23
+ # AWS::Core::ClientLogging.class_eval do
24
+ # def sanitize_hash(hash)
25
+ # hash.map { |key, value| "#{sanitize_value(key)}=>#{sanitize_value(value)}".force_encoding('UTF-8') }.sort.join(',')
26
+ # end
27
+ # end
28
+ # end
29
+
30
+ base.instance_eval do
31
+ @s3_options = @options[:s3_options] || {}
32
+
33
+ @s3_protocol = @options[:s3_protocol] ||
34
+ Proc.new do |style, attachment|
35
+ permission = (@s3_permissions[style.to_s.to_sym] || @s3_permissions[:default])
36
+ permission = permission.call(attachment, style) if permission.respond_to?(:call)
37
+ (permission == :public_read) ? 'http' : 'https'
38
+ end
39
+
40
+ unless @options[:url].to_s.match(/\A:s3.*url\Z/) || @options[:url] == ":asset_host"
41
+ @options[:path] = @options[:path].gsub(/:url/, @options[:url]).gsub(/\A:rails_root\/public\/system/, '')
42
+ @options[:url] = ":s3_path_url"
43
+ end
44
+ @options[:url] = @options[:url].inspect if @options[:url].is_a?(Symbol)
45
+
46
+ @http_proxy = @options[:http_proxy] || nil
47
+ end
48
+
49
+ Paperclip.interpolates(:s3_alias_url) do |attachment, style|
50
+ "#{attachment.s3_protocol(style, true)}//#{attachment.s3_host_alias}/#{attachment.path(style).gsub(%r{\A/}, "")}"
51
+ end unless Paperclip::Interpolations.respond_to? :s3_alias_url
52
+ Paperclip.interpolates(:s3_path_url) do |attachment, style|
53
+ "#{attachment.s3_protocol(style, true)}//#{attachment.s3_host_alias}/#{attachment.path(style).gsub(%r{\A/}, "")}"
54
+ end unless Paperclip::Interpolations.respond_to? :s3_path_url
55
+ Paperclip.interpolates(:s3_domain_url) do |attachment, style|
56
+ "#{attachment.s3_protocol(style, true)}//#{attachment.s3_host_alias}/#{attachment.path(style).gsub(%r{\A/}, "")}"
57
+ end unless Paperclip::Interpolations.respond_to? :s3_domain_url
58
+ Paperclip.interpolates(:asset_host) do |attachment, style|
59
+ "#{attachment.path(style).gsub(%r{\A/}, "")}"
60
+ end unless Paperclip::Interpolations.respond_to? :asset_host
61
+ end
62
+
63
+ def expiring_url(time = 3600, style_name = default_style)
64
+ if path(style_name)
65
+ base_options = { :expires => time, :secure => use_secure_protocol?(style_name) }
66
+ s3_object(style_name).url_for(:read, base_options.merge(s3_url_options)).to_s
67
+ else
68
+ url(style_name)
69
+ end
70
+ end
71
+
72
+ def s3_credentials
73
+ # @s3_credentials ||= parse_credentials(@options[:s3_credentials])
74
+ end
75
+
76
+ def s3_host_name
77
+ host_name = @options[:s3_host_name] || "s3.amazonaws.com"
78
+ # host_name = host_name.call(self) if host_name.is_a?(Proc)
79
+ #
80
+ # host_name || s3_credentials[:s3_host_name] || "s3.amazonaws.com"
81
+ end
82
+
83
+ def s3_host_alias
84
+ @s3_host_alias = @options[:host_alias] || "#{s3_host_name}/#{bucket_name}"
85
+ @s3_host_alias
86
+ end
87
+
88
+ def s3_url_options
89
+ s3_url_options = @options[:s3_url_options] || {}
90
+ s3_url_options = s3_url_options.call(instance) if s3_url_options.respond_to?(:call)
91
+ s3_url_options
92
+ end
93
+
94
+ def bucket_name
95
+ @bucket = @options[:bucket] || s3_credentials[:bucket]
96
+ @bucket = @bucket.call(self) if @bucket.respond_to?(:call)
97
+ @bucket or raise ArgumentError, "missing required :bucket option"
98
+ end
99
+
100
+ def s3_interface
101
+ @s3_interface ||= begin
102
+ config = { } # :s3_endpoint => s3_host_name }
103
+
104
+ obtain_s3_instance_for(config.merge(@s3_options))
105
+ end
106
+ end
107
+
108
+ def obtain_s3_instance_for(options)
109
+ instances = (Thread.current[:paperclip_s3_instances] ||= {})
110
+ instances[options] ||= ::Aws::S3.new()
111
+ end
112
+
113
+
114
+ def exists?(style = default_style)
115
+ if original_filename
116
+ s3_interface.get_object(bucket: bucket_name, key: style)
117
+ true
118
+ else
119
+ false
120
+ end
121
+ rescue Aws::S3::Errors::NoSuchKey => e
122
+ false
123
+ end
124
+
125
+
126
+ def s3_protocol(style = default_style, with_colon = false)
127
+ protocol = @s3_protocol
128
+ protocol = protocol.call(style, self) if protocol.respond_to?(:call)
129
+
130
+ if with_colon && !protocol.empty?
131
+ "#{protocol}:"
132
+ else
133
+ protocol.to_s
134
+ end
135
+ end
136
+
137
+ def flush_writes #:nodoc:
138
+ @queued_for_write.each do |style, file|
139
+ begin
140
+ log("saving #{path(style)}")
141
+ acl = @s3_permissions[style] || @s3_permissions[:default]
142
+ acl = acl.call(self, style) if acl.respond_to?(:call)
143
+
144
+
145
+ write_options = {
146
+ bucket: bucket_name,
147
+ key: path(style),
148
+ acl: 'public-read',
149
+ body: File.read(file.path),
150
+ content_type: file.content_type
151
+ }
152
+
153
+
154
+ if @s3_server_side_encryption
155
+ write_options[:server_side_encryption] = @s3_server_side_encryption
156
+ end
157
+
158
+ style_specific_options = styles[style]
159
+
160
+ if style_specific_options
161
+ merge_s3_headers( style_specific_options[:s3_headers], @s3_headers, @s3_metadata) if style_specific_options[:s3_headers]
162
+ @s3_metadata.merge!(style_specific_options[:s3_metadata]) if style_specific_options[:s3_metadata]
163
+ end
164
+
165
+ write_options[:metadata] = @s3_metadata unless @s3_metadata.empty?
166
+ write_options.merge!(@s3_headers)
167
+
168
+ # TODO: Change out for API Call
169
+
170
+ s3_interface.put_object(write_options)
171
+ # s3_object(style).write(file, write_options)
172
+ rescue ::Aws::S3::Errors::NoSuchBucket => e
173
+ # create_bucket
174
+ # retry
175
+ ensure
176
+ # file.rewind
177
+ end
178
+ end
179
+
180
+ after_flush_writes # allows attachment to clean up temp files
181
+
182
+ @queued_for_write = {}
183
+ end
184
+
185
+ def flush_deletes #:nodoc:
186
+ @queued_for_delete.each do |path|
187
+ begin
188
+ log("deleting #{path}")
189
+ s3_interface.delete_object(bucket: bucket_name, key: path.sub(%r{\A/},''))
190
+ rescue Aws::Errors::Base => e
191
+ # Ignore this.
192
+ end
193
+ end
194
+ @queued_for_delete = []
195
+ end
196
+
197
+
198
+ def copy_to_local_file(style, local_dest_path)
199
+ # TODO: This whole function
200
+
201
+ # log("copying #{path(style)} to local file #{local_dest_path}")
202
+ # local_file = ::File.open(local_dest_path, 'wb')
203
+ # file = s3_object(style)
204
+ # local_file.write(file.read)
205
+ # local_file.close
206
+ # rescue AWS::Errors::Base => e
207
+ # warn("#{e} - cannot copy #{path(style)} to local file #{local_dest_path}")
208
+ # false
209
+ end
210
+
211
+ private
212
+ def use_secure_protocol?(style_name)
213
+ s3_protocol(style_name) == "https"
214
+ end
215
+
216
+ end
217
+ end
218
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'paperclip/aws/core/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "paperclip-aws-core"
8
+ spec.version = Paperclip::Aws::Core::VERSION
9
+ spec.authors = ["Bill Centinaro"]
10
+ spec.email = ["billc@seatengine.com"]
11
+ spec.summary = %q{A simple gem to add support for the AWS Core Gem for paperclip}
12
+ spec.description = %q{}
13
+ spec.homepage = "https://github.com/SeatEngine/paperclip-aws-core"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "aws-sdk-core"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.5"
24
+ spec.add_development_dependency "rake"
25
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paperclip-aws-core
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Bill Centinaro
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk-core
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: ''
56
+ email:
57
+ - billc@seatengine.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/paperclip/aws/core.rb
68
+ - lib/paperclip/aws/core/version.rb
69
+ - paperclip-aws-core.gemspec
70
+ homepage: https://github.com/SeatEngine/paperclip-aws-core
71
+ licenses:
72
+ - MIT
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.2.0
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: A simple gem to add support for the AWS Core Gem for paperclip
94
+ test_files: []