class PopulateMe::S3Attachment
Public Class Methods
ensure_bucket()
click to toggle source
# 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()
click to toggle source
# File lib/populate_me/s3_attachment.rb, line 101 def middleware nil end
Public Instance Methods
deletable?(variation_name=:original)
click to toggle source
# File lib/populate_me/s3_attachment.rb, line 85 def deletable? variation_name=:original !WebUtils.blank? self.field_filename(variation_name) # Fine since deleting a non-existent file does not raise an error in S3 end
local_url_prefix()
click to toggle source
# 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()
click to toggle source
Attachee_prefix is moved on field_value for S3 as well as url_prefix
# File lib/populate_me/s3_attachment.rb, line 26 def location_root File.join( settings.root, settings.url_prefix ) end
next_available_filename(filename)
click to toggle source
# 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
perform_create(hash)
click to toggle source
# 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
perform_delete(variation_name=:original)
click to toggle source
# 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)
click to toggle source
Attachee_prefix is moved on field_value for S3 as well as url_prefix
# 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