Class: PopulateMe::S3Attachment
Instance Attribute Summary
Attributes inherited from Attachment
#document, #field
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Attachment
#attachee_prefix, #create, #create_variations, #delete, #delete_all, #ensure_local_path, #field_filename, #field_options, #field_value, inherited, #initialize, #location, #location_for_filename, middleware_options, set, #settings, #variations
Class Method Details
.ensure_bucket ⇒ Object
97
98
99
|
# File 'lib/populate_me/s3_attachment.rb', line 97
def ensure_bucket
raise MissingBucketError, "Attachment class #{self.name} does not have an S3 bucket." if settings.bucket.nil?
end
|
.middleware ⇒ Object
101
102
103
|
# File 'lib/populate_me/s3_attachment.rb', line 101
def middleware
nil
end
|
Instance Method Details
#deletable?(variation_name = :original) ⇒ Boolean
85
86
87
88
|
# File 'lib/populate_me/s3_attachment.rb', line 85
def deletable? variation_name=:original
!WebUtils.blank? self.field_filename(variation_name)
end
|
#local_url_prefix ⇒ Object
33
34
35
36
37
38
39
|
# File 'lib/populate_me/s3_attachment.rb', line 33
def local_url_prefix
(
self.field_options[:url_prefix] ||
self.document.class.settings.s3_url_prefix ||
settings.url_prefix
).gsub(/^\/|\/$/,'')
end
|
#location_root ⇒ Object
Attachee_prefix is moved on field_value for S3 as well as url_prefix
26
27
28
29
30
31
|
# File 'lib/populate_me/s3_attachment.rb', line 26
def location_root
File.join(
settings.root,
settings.url_prefix
)
end
|
#next_available_filename(filename) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/populate_me/s3_attachment.rb', line 41
def next_available_filename filename
ext = File.extname(filename)
base = File.join(
local_url_prefix,
attachee_prefix,
File.basename(filename,ext)
).gsub(/^\//,'')
i = 0
loop do
suffix = i==0 ? '' : "-#{i}"
potential_filename = [base,suffix,ext].join
if settings.bucket.object(potential_filename).exists?
i += 1
else
filename = potential_filename
break
end
end
filename
end
|
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/populate_me/s3_attachment.rb', line 62
def perform_create hash
if hash[:variation].nil?
fn = next_available_filename(hash[:filename])
file = hash[:tempfile]
type = hash[:type]
else
fn = WebUtils.filename_variation hash[:future_field_value], hash[:variation].name, hash[:variation].ext
file = File.open(hash[:variation_path])
type = Rack::Mime.mime_type ".#{hash[:variation].ext}"
end
settings.bucket.put_object({
acl: self.field_options[:acl] || 'public-read',
key: fn,
content_type: type,
body: file,
metadata: {
parent_collection: (self.document.class.respond_to?(:collection) ? self.document.class.collection.name : self.attachee_prefix),
}
})
file.close unless hash[:variation].nil?
fn
end
|
90
91
92
93
|
# File 'lib/populate_me/s3_attachment.rb', line 90
def perform_delete variation_name=:original
s3file = settings.bucket.object(self.field_filename(variation_name))
s3file.delete unless s3file.nil?
end
|
#url(variation_name = :original) ⇒ Object
Attachee_prefix is moved on field_value for S3 as well as url_prefix
20
21
22
23
|
# File 'lib/populate_me/s3_attachment.rb', line 20
def url variation_name=:original
return nil if WebUtils.blank?(self.field_filename(variation_name))
"#{settings.bucket.url}/#{self.field_filename(variation_name)}"
end
|