miloops-attachment_fu 3.2.6 → 3.2.7
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.
@@ -9,15 +9,15 @@ module Technoweenie # :nodoc:
|
|
9
9
|
#
|
10
10
|
# == Requirements
|
11
11
|
#
|
12
|
-
# Requires the {Cloud Files Gem}[http://www.mosso.com/cloudfiles.jsp] by Rackspace
|
12
|
+
# Requires the {Cloud Files Gem}[http://www.mosso.com/cloudfiles.jsp] by Rackspace
|
13
13
|
#
|
14
14
|
# == Configuration
|
15
15
|
#
|
16
|
-
# Configuration is done via <tt>
|
16
|
+
# Configuration is done via <tt>Rails.root/config/rackspace_cloudfiles.yml</tt> and is loaded according to the <tt>Rails.env</tt>.
|
17
17
|
# The minimum connection options that you must specify are a container name, your Mosso login name and your Mosso API key.
|
18
|
-
# You can sign up for Cloud Files and get access keys by visiting https://www.mosso.com/buy.htm
|
18
|
+
# You can sign up for Cloud Files and get access keys by visiting https://www.mosso.com/buy.htm
|
19
19
|
#
|
20
|
-
# Example configuration (
|
20
|
+
# Example configuration (Rails.root/config/rackspace_cloudfiles.yml)
|
21
21
|
#
|
22
22
|
# development:
|
23
23
|
# container_name: appname_development
|
@@ -36,7 +36,7 @@ module Technoweenie # :nodoc:
|
|
36
36
|
#
|
37
37
|
# You can change the location of the config path by passing a full path to the :cloudfiles_config_path option.
|
38
38
|
#
|
39
|
-
# has_attachment :storage => :cloud_files, :cloudfiles_config_path => (
|
39
|
+
# has_attachment :storage => :cloud_files, :cloudfiles_config_path => (Rails.root + '/config/mosso.yml')
|
40
40
|
#
|
41
41
|
# === Required configuration parameters
|
42
42
|
#
|
@@ -116,8 +116,8 @@ module Technoweenie # :nodoc:
|
|
116
116
|
end
|
117
117
|
|
118
118
|
begin
|
119
|
-
@@cloudfiles_config_path = base.attachment_options[:cloudfiles_config_path] || (
|
120
|
-
@@cloudfiles_config = @@cloudfiles_config = YAML.load(ERB.new(File.read(@@cloudfiles_config_path)).result)[
|
119
|
+
@@cloudfiles_config_path = base.attachment_options[:cloudfiles_config_path] || (Rails.root + '/config/rackspace_cloudfiles.yml')
|
120
|
+
@@cloudfiles_config = @@cloudfiles_config = YAML.load(ERB.new(File.read(@@cloudfiles_config_path)).result)[Rails.env].symbolize_keys
|
121
121
|
rescue
|
122
122
|
#raise ConfigFileNotFoundError.new('File %s not found' % @@cloudfiles_config_path)
|
123
123
|
end
|
@@ -125,7 +125,7 @@ module Technoweenie # :nodoc:
|
|
125
125
|
@@container_name = @@cloudfiles_config[:container_name]
|
126
126
|
@@cf = CloudFiles::Connection.new(@@cloudfiles_config[:username], @@cloudfiles_config[:api_key])
|
127
127
|
@@container = @@cf.container(@@container_name)
|
128
|
-
|
128
|
+
|
129
129
|
base.before_update :rename_file
|
130
130
|
end
|
131
131
|
|
@@ -9,12 +9,12 @@ module Technoweenie # :nodoc:
|
|
9
9
|
def self.included(base) #:nodoc:
|
10
10
|
base.before_update :rename_file
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
# Gets the full path to the filename in this format:
|
14
14
|
#
|
15
15
|
# # This assumes a model name like MyModel
|
16
|
-
# # public/#{table_name} is the default filesystem path
|
17
|
-
#
|
16
|
+
# # public/#{table_name} is the default filesystem path
|
17
|
+
# Rails.root/public/my_models/5/blah.jpg
|
18
18
|
#
|
19
19
|
# Overwrite this method in your model to customize the filename.
|
20
20
|
# The optional thumbnail argument will output the thumbnail's filename.
|
@@ -22,17 +22,17 @@ module Technoweenie # :nodoc:
|
|
22
22
|
file_system_path = (thumbnail ? thumbnail_class : self).attachment_options[:path_prefix].to_s
|
23
23
|
Rails.root.join( file_system_path, *partitioned_path(thumbnail_name_for(thumbnail))).to_s
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
# Used as the base path that #public_filename strips off full_filename to create the public path
|
27
27
|
def base_path
|
28
28
|
@base_path ||= Rails.root.join('public').to_s
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
# The attachment ID used in the full path of a file
|
32
32
|
def attachment_path_id
|
33
33
|
((respond_to?(:parent_id) && parent_id) || id) || 0
|
34
34
|
end
|
35
|
-
|
35
|
+
|
36
36
|
# Partitions the given path into an array of path components.
|
37
37
|
#
|
38
38
|
# For example, given an <tt>*args</tt> of ["foo", "bar"], it will return
|
@@ -42,10 +42,10 @@ module Technoweenie # :nodoc:
|
|
42
42
|
# hashing the string value of the id with SHA-512, and splitting the result
|
43
43
|
# into 4 components. If the id a 128-bit UUID (as set by :uuid_primary_key => true)
|
44
44
|
# then it will be split into 2 components.
|
45
|
-
#
|
45
|
+
#
|
46
46
|
# To turn this off entirely, set :partition => false.
|
47
47
|
def partitioned_path(*args)
|
48
|
-
if respond_to?(:attachment_options) && attachment_options[:partition] == false
|
48
|
+
if respond_to?(:attachment_options) && attachment_options[:partition] == false
|
49
49
|
args
|
50
50
|
elsif attachment_options[:uuid_primary_key]
|
51
51
|
# Primary key is a 128-bit UUID in hex format. Split it into 2 components.
|
@@ -65,13 +65,13 @@ module Technoweenie # :nodoc:
|
|
65
65
|
end
|
66
66
|
end
|
67
67
|
end
|
68
|
-
|
68
|
+
|
69
69
|
# Gets the public path to the file
|
70
70
|
# The optional thumbnail argument will output the thumbnail's filename.
|
71
71
|
def public_filename(thumbnail = nil)
|
72
72
|
full_filename(thumbnail).gsub %r(^#{Regexp.escape(base_path)}), ''
|
73
73
|
end
|
74
|
-
|
74
|
+
|
75
75
|
def filename=(value)
|
76
76
|
@old_filename = full_filename unless filename.nil? || @old_filename
|
77
77
|
write_attribute :filename, sanitize_filename(value)
|
@@ -104,7 +104,7 @@ module Technoweenie # :nodoc:
|
|
104
104
|
@old_filename = nil
|
105
105
|
true
|
106
106
|
end
|
107
|
-
|
107
|
+
|
108
108
|
# Saves the file to the file system
|
109
109
|
def save_to_storage
|
110
110
|
if save_attachment?
|
@@ -116,7 +116,7 @@ module Technoweenie # :nodoc:
|
|
116
116
|
@old_filename = nil
|
117
117
|
true
|
118
118
|
end
|
119
|
-
|
119
|
+
|
120
120
|
def current_data
|
121
121
|
File.file?(full_filename) ? File.read(full_filename) : nil
|
122
122
|
end
|
@@ -12,7 +12,7 @@ module Technoweenie # :nodoc:
|
|
12
12
|
#
|
13
13
|
# == Configuration
|
14
14
|
#
|
15
|
-
# Configuration is done via <tt>
|
15
|
+
# Configuration is done via <tt>Rails.root/config/amazon_s3.yml</tt> and is loaded according to the <tt>RAILS_ENV</tt>.
|
16
16
|
# The minimum connection options that you must specify are a bucket name, your access key id and your secret access key.
|
17
17
|
# If you don't already have your access keys, all you need to sign up for the S3 service is an account at Amazon.
|
18
18
|
# You can sign up for S3 and get access keys by visiting http://aws.amazon.com/s3.
|
@@ -20,7 +20,7 @@ module Technoweenie # :nodoc:
|
|
20
20
|
# If you wish to use Amazon CloudFront to serve the files, you can also specify a distibution domain for the bucket.
|
21
21
|
# To read more about CloudFront, visit http://aws.amazon.com/cloudfront
|
22
22
|
#
|
23
|
-
# Example configuration (
|
23
|
+
# Example configuration (Rails.root/config/amazon_s3.yml)
|
24
24
|
#
|
25
25
|
# development:
|
26
26
|
# bucket_name: appname_development
|
@@ -42,7 +42,7 @@ module Technoweenie # :nodoc:
|
|
42
42
|
#
|
43
43
|
# You can change the location of the config path by passing a full path to the :s3_config_path option.
|
44
44
|
#
|
45
|
-
# has_attachment :storage => :s3, :s3_config_path => (
|
45
|
+
# has_attachment :storage => :s3, :s3_config_path => (Rails.root + '/config/s3.yml')
|
46
46
|
#
|
47
47
|
# === Required configuration parameters
|
48
48
|
#
|
@@ -158,9 +158,9 @@ module Technoweenie # :nodoc:
|
|
158
158
|
#
|
159
159
|
# Niether <tt>base_path</tt> or <tt>full_filename</tt> include the bucket name as part of the path.
|
160
160
|
# You can retrieve the bucket name using the <tt>bucket_name</tt> method.
|
161
|
-
#
|
161
|
+
#
|
162
162
|
# === Accessing CloudFront URLs
|
163
|
-
#
|
163
|
+
#
|
164
164
|
# You can get an object's CloudFront URL using the cloudfront_url accessor. Using the example from above:
|
165
165
|
# @postcard.cloudfront_url # => http://XXXX.cloudfront.net/photos/1/mexico.jpg
|
166
166
|
#
|
@@ -183,7 +183,7 @@ module Technoweenie # :nodoc:
|
|
183
183
|
end
|
184
184
|
|
185
185
|
begin
|
186
|
-
@@s3_config_path = base.attachment_options[:s3_config_path] || (
|
186
|
+
@@s3_config_path = base.attachment_options[:s3_config_path] || (Rails.root + '/config/amazon_s3.yml')
|
187
187
|
@@s3_config = @@s3_config = YAML.load(ERB.new(File.read(@@s3_config_path)).result)[RAILS_ENV].symbolize_keys
|
188
188
|
#rescue
|
189
189
|
# raise ConfigFileNotFoundError.new('File %s not found' % @@s3_config_path)
|
@@ -216,7 +216,7 @@ module Technoweenie # :nodoc:
|
|
216
216
|
def self.port_string
|
217
217
|
@port_string ||= (s3_config[:port].nil? || s3_config[:port] == (s3_config[:use_ssl] ? 443 : 80)) ? '' : ":#{s3_config[:port]}"
|
218
218
|
end
|
219
|
-
|
219
|
+
|
220
220
|
def self.distribution_domain
|
221
221
|
@distribution_domain = s3_config[:distribution_domain]
|
222
222
|
end
|
@@ -233,7 +233,7 @@ module Technoweenie # :nodoc:
|
|
233
233
|
def s3_port_string
|
234
234
|
Technoweenie::AttachmentFu::Backends::S3Backend.port_string
|
235
235
|
end
|
236
|
-
|
236
|
+
|
237
237
|
def cloudfront_distribution_domain
|
238
238
|
Technoweenie::AttachmentFu::Backends::S3Backend.distribution_domain
|
239
239
|
end
|
@@ -269,26 +269,26 @@ module Technoweenie # :nodoc:
|
|
269
269
|
#
|
270
270
|
# The resulting url is in the form: <tt>http(s)://:server/:bucket_name/:table_name/:id/:file</tt> where
|
271
271
|
# the <tt>:server</tt> variable defaults to <tt>AWS::S3 URL::DEFAULT_HOST</tt> (s3.amazonaws.com) and can be
|
272
|
-
# set using the configuration parameters in <tt>
|
272
|
+
# set using the configuration parameters in <tt>Rails.root/config/amazon_s3.yml</tt>.
|
273
273
|
#
|
274
274
|
# The optional thumbnail argument will output the thumbnail's filename (if any).
|
275
275
|
def s3_url(thumbnail = nil)
|
276
276
|
File.join(s3_protocol + s3_hostname + s3_port_string, bucket_name, full_filename(thumbnail))
|
277
277
|
end
|
278
|
-
|
278
|
+
|
279
279
|
# All public objects are accessible via a GET request to CloudFront. You can generate a
|
280
280
|
# url for an object using the cloudfront_url method.
|
281
281
|
#
|
282
282
|
# @photo.cloudfront_url
|
283
283
|
#
|
284
284
|
# The resulting url is in the form: <tt>http://:distribution_domain/:table_name/:id/:file</tt> using
|
285
|
-
# the <tt>:distribution_domain</tt> variable set in the configuration parameters in <tt>
|
285
|
+
# the <tt>:distribution_domain</tt> variable set in the configuration parameters in <tt>Rails.root/config/amazon_s3.yml</tt>.
|
286
286
|
#
|
287
287
|
# The optional thumbnail argument will output the thumbnail's filename (if any).
|
288
288
|
def cloudfront_url(thumbnail = nil)
|
289
289
|
"http://" + cloudfront_distribution_domain + "/" + full_filename(thumbnail)
|
290
290
|
end
|
291
|
-
|
291
|
+
|
292
292
|
def public_filename(*args)
|
293
293
|
if attachment_options[:cloudfront]
|
294
294
|
cloudfront_url(args)
|
@@ -347,7 +347,7 @@ module Technoweenie # :nodoc:
|
|
347
347
|
def s3_port_string
|
348
348
|
Technoweenie::AttachmentFu::Backends::S3Backend.port_string
|
349
349
|
end
|
350
|
-
|
350
|
+
|
351
351
|
def cloudfront_distribution_domain
|
352
352
|
Technoweenie::AttachmentFu::Backends::S3Backend.distribution_domain
|
353
353
|
end
|
data/test/test_helper.rb
CHANGED
@@ -2,10 +2,10 @@
|
|
2
2
|
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
3
3
|
|
4
4
|
ENV['RAILS_ENV'] = 'test'
|
5
|
-
ENV['
|
5
|
+
ENV['Rails.root'] ||= File.dirname(__FILE__) + '/../../../..'
|
6
6
|
|
7
7
|
require 'test/unit'
|
8
|
-
require File.expand_path(File.join(ENV['
|
8
|
+
require File.expand_path(File.join(ENV['Rails.root'], 'config/environment.rb'))
|
9
9
|
|
10
10
|
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
|
11
11
|
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
|
@@ -13,7 +13,7 @@ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
|
|
13
13
|
db_adapter = ENV['DB']
|
14
14
|
|
15
15
|
# no db passed, try one of these fine config-free DBs before bombing.
|
16
|
-
db_adapter ||=
|
16
|
+
db_adapter ||=
|
17
17
|
begin
|
18
18
|
require 'rubygems'
|
19
19
|
require 'sqlite'
|
@@ -52,7 +52,7 @@ class Test::Unit::TestCase #:nodoc:
|
|
52
52
|
DbFile.transaction { [Attachment, FileAttachment, OrphanAttachment, MinimalAttachment, DbFile].each { |klass| klass.delete_all } }
|
53
53
|
attachment_model self.class.attachment_model
|
54
54
|
end
|
55
|
-
|
55
|
+
|
56
56
|
def teardown
|
57
57
|
FileUtils.rm_rf File.join(File.dirname(__FILE__), 'files')
|
58
58
|
# Files generated by random_tempfile_filename
|
@@ -63,7 +63,7 @@ class Test::Unit::TestCase #:nodoc:
|
|
63
63
|
#self.use_instantiated_fixtures = false
|
64
64
|
|
65
65
|
def self.attachment_model(klass = nil)
|
66
|
-
@attachment_model = klass if klass
|
66
|
+
@attachment_model = klass if klass
|
67
67
|
@attachment_model
|
68
68
|
end
|
69
69
|
|
@@ -97,7 +97,7 @@ class Test::Unit::TestCase #:nodoc:
|
|
97
97
|
return att
|
98
98
|
end
|
99
99
|
end
|
100
|
-
|
100
|
+
|
101
101
|
def use_temp_file(fixture_filename)
|
102
102
|
temp_path = File.join('/tmp', File.basename(fixture_filename))
|
103
103
|
temp_dir = File.join(FIXTURE_PATH, 'tmp')
|
@@ -121,10 +121,10 @@ class Test::Unit::TestCase #:nodoc:
|
|
121
121
|
end
|
122
122
|
end
|
123
123
|
|
124
|
-
def assert_valid(record)
|
124
|
+
def assert_valid(record)
|
125
125
|
assert record.valid?, record.errors.full_messages.join("\n")
|
126
|
-
end
|
127
|
-
|
126
|
+
end
|
127
|
+
|
128
128
|
def assert_file_jpeg_quality(model, thumbnail, expected)
|
129
129
|
filename = if model.respond_to?(:full_filename)
|
130
130
|
model.full_filename(thumbnail)
|
@@ -145,11 +145,11 @@ class Test::Unit::TestCase #:nodoc:
|
|
145
145
|
STDERR.puts "ImageMagick's identify not found / not in PATH: can't quickly check produced image quality."
|
146
146
|
end
|
147
147
|
end
|
148
|
-
|
148
|
+
|
149
149
|
def assert_not_created
|
150
150
|
assert_created(0) { yield }
|
151
151
|
end
|
152
|
-
|
152
|
+
|
153
153
|
def should_reject_by_size_with(klass)
|
154
154
|
attachment_model klass
|
155
155
|
assert_not_created do
|
@@ -159,22 +159,22 @@ class Test::Unit::TestCase #:nodoc:
|
|
159
159
|
assert_nil attachment.db_file if attachment.respond_to?(:db_file)
|
160
160
|
end
|
161
161
|
end
|
162
|
-
|
162
|
+
|
163
163
|
def assert_difference(object, method = nil, difference = 1)
|
164
164
|
initial_value = object.send(method)
|
165
165
|
yield
|
166
166
|
assert_equal initial_value + difference, object.send(method)
|
167
167
|
end
|
168
|
-
|
168
|
+
|
169
169
|
def assert_no_difference(object, method, &block)
|
170
170
|
assert_difference object, method, 0, &block
|
171
171
|
end
|
172
|
-
|
172
|
+
|
173
173
|
def attachment_model(klass = nil)
|
174
|
-
@attachment_model = klass if klass
|
174
|
+
@attachment_model = klass if klass
|
175
175
|
@attachment_model
|
176
176
|
end
|
177
177
|
end
|
178
178
|
|
179
179
|
require File.join(File.dirname(__FILE__), 'fixtures/attachment')
|
180
|
-
require File.join(File.dirname(__FILE__), 'base_attachment_tests')
|
180
|
+
require File.join(File.dirname(__FILE__), 'base_attachment_tests')
|