model_attachment 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/model_attachment.rb +15 -9
- data/lib/model_attachment/amazon.rb +2 -2
- data/test/model_attachment_test.rb +8 -8
- metadata +3 -3
data/lib/model_attachment.rb
CHANGED
@@ -14,7 +14,7 @@ require 'model_attachment/amazon'
|
|
14
14
|
|
15
15
|
# The base module that gets included in ActiveRecord::Base.
|
16
16
|
module ModelAttachment
|
17
|
-
VERSION = "0.0.
|
17
|
+
VERSION = "0.0.8"
|
18
18
|
|
19
19
|
class << self
|
20
20
|
|
@@ -132,8 +132,9 @@ module ModelAttachment
|
|
132
132
|
server_name += ":" + port.to_s if port
|
133
133
|
type_string = "?type=#{type}" if type
|
134
134
|
|
135
|
-
|
136
|
-
|
135
|
+
# if we aren't using aws set @bucket to nil
|
136
|
+
if !self.class.attachment_options[:aws]
|
137
|
+
@bucket = nil
|
137
138
|
end
|
138
139
|
|
139
140
|
# if files are public, serve public url
|
@@ -141,12 +142,12 @@ module ModelAttachment
|
|
141
142
|
url_path = path.gsub(/^\/public(.*)/, '\1')
|
142
143
|
type_string = "_#{type}" if type
|
143
144
|
url = "#{proto}://#{server_name}#{url_path}#{basename}#{type_string}#{extension}"
|
144
|
-
elsif
|
145
|
-
# if bucket is set, then use aws url
|
146
|
-
aws_url(type)
|
147
|
-
else
|
145
|
+
elsif bucket.nil?
|
148
146
|
# otherwise use private url with deliver
|
149
147
|
url = "#{proto}://#{server_name}#{url_path}#{id}#{type_string}"
|
148
|
+
else
|
149
|
+
# if bucket is set, then use aws url
|
150
|
+
url = aws_url(type)
|
150
151
|
end
|
151
152
|
|
152
153
|
log("Providing URL: #{url}")
|
@@ -162,7 +163,7 @@ module ModelAttachment
|
|
162
163
|
if (self.class.attachment_options[:path])
|
163
164
|
return interpolate(self.class.attachment_options[:path])
|
164
165
|
else
|
165
|
-
return "/public/#{self.class.to_s.downcase.pluralize}/" + sprintf("%04d", id) + "/"
|
166
|
+
return "/public/system/#{self.class.to_s.downcase.pluralize}/" + sprintf("%04d", id) + "/"
|
166
167
|
end
|
167
168
|
end
|
168
169
|
|
@@ -197,10 +198,15 @@ module ModelAttachment
|
|
197
198
|
content_type =~ /^image\//
|
198
199
|
end
|
199
200
|
|
201
|
+
def valid_methods
|
202
|
+
# reject any methods that will cause issues
|
203
|
+
self.class.instance_methods.sort.reverse.reject {|m| m =~ /\W+/ }
|
204
|
+
end
|
205
|
+
|
200
206
|
# create the path based on the template
|
201
207
|
def interpolate(path, *args)
|
202
208
|
#methods = ["domain", "folder", "document", "version", "user", "account"]
|
203
|
-
|
209
|
+
valid_methods.inject( path.dup ) do |result, tag|
|
204
210
|
#$stderr.puts("Result: #{result} Tag: #{tag}")
|
205
211
|
result.gsub(/:#{tag}/) do |match|
|
206
212
|
send( tag, *args )
|
@@ -26,12 +26,12 @@ module ModelAttachment
|
|
26
26
|
|
27
27
|
# connect to aws, uses access_key_id and secret_access_key in config/amazon.yml
|
28
28
|
def aws_connect
|
29
|
+
log("aws_connect")
|
29
30
|
return if AWS::S3::Base.connected?
|
30
31
|
|
31
32
|
begin
|
32
33
|
config = YAML.load_file(self.class.attachment_options[:aws])
|
33
34
|
if config
|
34
|
-
log("Connect to Amazon")
|
35
35
|
AWS::S3::Base.establish_connection!(
|
36
36
|
:access_key_id => config['access_key_id'],
|
37
37
|
:secret_access_key => config['secret_access_key'],
|
@@ -48,9 +48,9 @@ module ModelAttachment
|
|
48
48
|
|
49
49
|
# moves file to amazon along with modified images, removes local images once object existence is confirmed
|
50
50
|
def move_to_amazon
|
51
|
+
log("Move #{aws_key} to Amazon.")
|
51
52
|
aws_connect
|
52
53
|
|
53
|
-
log("Move #{aws_key} to Amazon.")
|
54
54
|
begin
|
55
55
|
AWS::S3::S3Object.store(aws_key, open(full_filename), default_bucket, :content_type => content_type)
|
56
56
|
|
@@ -68,18 +68,18 @@ class DocumentDefault < Document
|
|
68
68
|
end
|
69
69
|
|
70
70
|
class DocumentNoResize < Document
|
71
|
-
has_attachment :path => "/:domain/:folder/:document/:version/"
|
71
|
+
has_attachment :path => "/system/:domain/:folder/:document/:version/"
|
72
72
|
end
|
73
73
|
|
74
74
|
class DocumentWithResize < Document
|
75
|
-
has_attachment :path => "/:domain/:folder/:document/:version/",
|
75
|
+
has_attachment :path => "/system/:domain/:folder/:document/:version/",
|
76
76
|
:types => {
|
77
77
|
:small => { :command => '/opt/local/bin/convert -geometry 100x100' }
|
78
78
|
}
|
79
79
|
end
|
80
80
|
|
81
81
|
class DocumentWithAWS < Document
|
82
|
-
has_attachment :path => "/:domain/:folder/:document/:version/",
|
82
|
+
has_attachment :path => "/system/:domain/:folder/:document/:version/",
|
83
83
|
:aws => File.join(File.dirname(__FILE__), "amazon.yml"),
|
84
84
|
:types => {
|
85
85
|
:small => { :command => '/opt/local/bin/convert -geometry 100x100' }
|
@@ -122,10 +122,10 @@ class ModelAttachmentTest < Test::Unit::TestCase
|
|
122
122
|
assert_equal "test1.jpg", document.file_name
|
123
123
|
assert_equal "image/jpeg", document.content_type
|
124
124
|
|
125
|
-
assert File.exists?(RAILS_ROOT + "/system/0001/test1.jpg")
|
125
|
+
assert File.exists?(RAILS_ROOT + "/public/system/documentdefaults/0001/test1.jpg")
|
126
126
|
|
127
127
|
document.destroy
|
128
|
-
assert !File.exists?(RAILS_ROOT + "/system/0001/test1.jpg")
|
128
|
+
assert !File.exists?(RAILS_ROOT + "/public/system/documentdefaults/0001/test1.jpg")
|
129
129
|
end
|
130
130
|
|
131
131
|
def test_save_with_no_resize
|
@@ -161,7 +161,7 @@ class ModelAttachmentTest < Test::Unit::TestCase
|
|
161
161
|
assert !File.exists?(RAILS_ROOT + "/system/bbs/1/1/0/test2.jpg")
|
162
162
|
assert !File.exists?(RAILS_ROOT + "/system/bbs/1/1/0/test2_small.jpg")
|
163
163
|
end
|
164
|
-
|
164
|
+
|
165
165
|
def test_save_with_aws
|
166
166
|
FileUtils.cp(RAILS_ROOT + "/assets/test.jpg", RAILS_ROOT + "/assets/test3.jpg")
|
167
167
|
file = File.open(RAILS_ROOT + "/assets/test3.jpg")
|
@@ -170,17 +170,17 @@ class ModelAttachmentTest < Test::Unit::TestCase
|
|
170
170
|
document.save
|
171
171
|
assert File.exist?(RAILS_ROOT + "/system/bbs/1/1/0/test3.jpg")
|
172
172
|
|
173
|
-
assert_equal "http://localhost:3000/
|
173
|
+
assert_equal "http://localhost:3000/documentwithaws/deliver/1", document.url(:port => "3000")
|
174
174
|
assert_equal "test3.jpg", document.file_name
|
175
175
|
assert_equal String, document.file_name.class
|
176
176
|
assert_equal "image/jpeg", document.content_type
|
177
177
|
|
178
178
|
document.move_to_amazon
|
179
|
+
assert_equal 'globalfolders', document.bucket
|
179
180
|
|
180
181
|
assert_match /https:\/\/s3.amazonaws.com\/globalfolders\/system\/bbs\/1\/1\/0\/test3\.jpg/, document.url
|
181
182
|
assert_match /https:\/\/s3.amazonaws.com\/globalfolders\/system\/bbs\/1\/1\/0\/test3_small\.jpg/, document.url(:type => "small")
|
182
183
|
|
183
|
-
assert_equal 'globalfolders', document.bucket
|
184
184
|
assert !File.exists?(RAILS_ROOT + "/system/bbs/1/1/0/test3.jpg")
|
185
185
|
assert !File.exists?(RAILS_ROOT + "/system/bbs/1/1/0/test3_small.jpg")
|
186
186
|
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 8
|
9
|
+
version: 0.0.8
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Steve Walker
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-05-
|
17
|
+
date: 2010-05-05 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|