mm-attach-it 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +1 -1
- data/README.rdoc +1 -1
- data/lib/attach_it/attachment_options.rb +1 -1
- data/lib/attach_it/storage/s3.rb +111 -0
- data/lib/attach_it/version.rb +1 -1
- metadata +5 -4
data/README
CHANGED
data/README.rdoc
CHANGED
@@ -105,7 +105,7 @@ class AttachmentOptions
|
|
105
105
|
|
106
106
|
private
|
107
107
|
def interpolate(source = nil, style_name = nil)
|
108
|
-
result = source.gsub(/\:rails_root/, Rails.root)
|
108
|
+
result = source.gsub(/\:rails_root/, Rails.root.to_s)
|
109
109
|
result.gsub!(/\:environment/, Rails.env)
|
110
110
|
result.gsub!(/\:filename/, @filename.nil? ? '' : @filename)
|
111
111
|
result.gsub!(/\:extension/, @extension.nil? ? '' : @extension)
|
@@ -0,0 +1,111 @@
|
|
1
|
+
require 'aws/s3'
|
2
|
+
require 'tempfile'
|
3
|
+
require 'net/http'
|
4
|
+
|
5
|
+
class S3 < Storage
|
6
|
+
|
7
|
+
def initialize(credentials_file = nil, environment = nil)
|
8
|
+
begin
|
9
|
+
credentials = YAML::load_file(credentials_file)
|
10
|
+
@access_key_id = credentials[environment]['access_key_id']
|
11
|
+
@secret_access_key = credentials[environment]['secret_access_key']
|
12
|
+
@bucket = credentials[environment]['bucket']
|
13
|
+
@permission = credentials[environment]['permission'].nil? ? :public_read : credentials[environment]['permission'].to_sym
|
14
|
+
check_credencials
|
15
|
+
rescue Exception => exception
|
16
|
+
return exception.to_s
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def flush_write(attach_options = nil)
|
21
|
+
attach_options.styles.each do |style_name, style_value|
|
22
|
+
begin
|
23
|
+
temp_file = create_temp_file
|
24
|
+
transform(style_value, attach_options.assigned_file.path).write(temp_file.path)
|
25
|
+
AWS::S3::S3Object.store(attach_options.short_url(style_name), File.open(temp_file.path, 'rb'), @bucket, :access => @permission)
|
26
|
+
#temp_file.unlink
|
27
|
+
rescue Exception => exception
|
28
|
+
attach_options.add_error(exception.to_s)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
unless attach_options.styles.has_key?(:original)
|
33
|
+
begin
|
34
|
+
AWS::S3::S3Object.store(attach_options.short_url, File.open(attach_options.assigned_file.path, 'rb'), @bucket, :access => @permission)
|
35
|
+
rescue Exception => exception
|
36
|
+
attach_options.add_error(exception.to_s)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def flush_delete(queued_for_delete = nil)
|
42
|
+
queued_for_delete.each do |file|
|
43
|
+
begin
|
44
|
+
AWS::S3::S3Object.delete(reajust_url(file), @bucket)
|
45
|
+
rescue AWS::S3::NoSuchBucket => exception
|
46
|
+
nil
|
47
|
+
rescue Exception => exception
|
48
|
+
nil
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def url(attach_options = nil, url = '', style = nil)
|
54
|
+
file = reajust_url(url)
|
55
|
+
AWS::S3::S3Object.url_for(attach_options.interpolate(url, style), @bucket)
|
56
|
+
end
|
57
|
+
|
58
|
+
def base64(attach_options = nil, style = nil)
|
59
|
+
url_s3 = URI.parse(attach_options.url(style))
|
60
|
+
|
61
|
+
Net::HTTP.start(url_s3.host, url_s3.port) do |http|
|
62
|
+
resp = http.get(url_s3.path)
|
63
|
+
resp.body
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def set_queued_for_delete(attach_options = nil, styles = {})
|
68
|
+
[styles.keys, :original].flatten.map do |style_name|
|
69
|
+
attach_options.url(style_name)
|
70
|
+
end.compact
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
def reajust_url(url = '')
|
75
|
+
url.sub(/^(\/)/, '')
|
76
|
+
end
|
77
|
+
|
78
|
+
def create_temp_file
|
79
|
+
temp_file = Tempfile.open('mm-attach-it-')
|
80
|
+
temp_file.close
|
81
|
+
return temp_file
|
82
|
+
end
|
83
|
+
|
84
|
+
def check_credencials
|
85
|
+
begin
|
86
|
+
AWS::S3::Base.establish_connection!(
|
87
|
+
:access_key_id => @access_key_id,
|
88
|
+
:secret_access_key => @secret_access_key
|
89
|
+
)
|
90
|
+
rescue Exception => exception
|
91
|
+
return exception.to_s
|
92
|
+
end
|
93
|
+
|
94
|
+
begin
|
95
|
+
AWS::S3::Bucket.find(@bucket)
|
96
|
+
rescue AWS::S3::InvalidAccessKeyId => exception
|
97
|
+
return exception.to_s
|
98
|
+
rescue AWS::S3::SignatureDoesNotMatch
|
99
|
+
return exception.to_s
|
100
|
+
rescue AWS::S3::AccessDenied => exception
|
101
|
+
return exception.to_s
|
102
|
+
rescue AWS::S3::NoSuchBucket => exception
|
103
|
+
AWS::S3::Bucket.create(@bucket)
|
104
|
+
rescue Exception => exception
|
105
|
+
return exception.to_s
|
106
|
+
end
|
107
|
+
|
108
|
+
return nil
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
data/lib/attach_it/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mm-attach-it
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 1
|
10
|
+
version: 0.2.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Adilson Chacon
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-09-14 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: wand
|
@@ -123,6 +123,7 @@ files:
|
|
123
123
|
- lib/attach_it/version.rb
|
124
124
|
- lib/attach_it/storage/gridfs.rb
|
125
125
|
- lib/attach_it/storage/filesystem.rb
|
126
|
+
- lib/attach_it/storage/s3.rb
|
126
127
|
- lib/attach_it/storage/storage.rb
|
127
128
|
- lib/attach_it/attachment_options.rb
|
128
129
|
- lib/attach_it/attach_it.rb
|