Class: CamaleonCmsAwsUploader
- Inherits:
-
CamaleonCmsUploader
- Object
- CamaleonCmsUploader
- CamaleonCmsAwsUploader
- Defined in:
- app/uploaders/camaleon_cms_aws_uploader.rb
Instance Attribute Summary
Attributes inherited from CamaleonCmsUploader
Instance Method Summary collapse
-
#add_file(uploaded_io_or_file_path, key, args = {}) ⇒ Object
add a file object or file path into AWS server :key => (String) key of the file ot save in AWS :args => (HASH) false, is_thumb: false, where: - same_name: false => avoid to overwrite an existent file with same key and search for an available key - is_thumb: true => if this file is a thumbnail of an uploaded file.
-
#add_folder(key) ⇒ Object
add new folder to AWS with :key.
- #after_initialize ⇒ Object
-
#browser_files ⇒ Object
recover all files from AWS and parse it to save into DB as cache.
-
#bucket ⇒ Object
initialize a bucket with AWS configurations return: (AWS Bucket object).
-
#delete_file(key) ⇒ Object
delete a file in AWS with :key.
-
#delete_folder(key) ⇒ Object
delete a folder in AWS with :key.
-
#file_parse(s3_file) ⇒ Object
parse an AWS file into custom file_object.
Methods inherited from CamaleonCmsUploader
#cache_item, #clear_cache, #get_file, get_file_format, get_file_format_extensions, #initialize, #objects, #reload, #search, #search_new_key, validate_file_format, #version_path
Constructor Details
This class inherits a constructor from CamaleonCmsUploader
Instance Method Details
#add_file(uploaded_io_or_file_path, key, args = {}) ⇒ Object
add a file object or file path into AWS server :key => (String) key of the file ot save in AWS :args => (HASH) false, is_thumb: false, where:
- same_name: false => avoid to overwrite an existent file with same key and search for an available key
- is_thumb: true => if this file is a thumbnail of an uploaded file
52 53 54 55 56 57 58 59 |
# File 'app/uploaders/camaleon_cms_aws_uploader.rb', line 52 def add_file(uploaded_io_or_file_path, key, args = {}) args, res = {same_name: false, is_thumb: false}.merge(args), nil key = search_new_key(key) unless args[:same_name] s3_file = bucket.object(key.split('/').clean_empty.join('/')) s3_file.upload_file(uploaded_io_or_file_path.is_a?(String) ? uploaded_io_or_file_path : uploaded_io_or_file_path.path, acl: 'public-read') res = cache_item(file_parse(s3_file)) unless args[:is_thumb] res end |
#add_folder(key) ⇒ Object
add new folder to AWS with :key
62 63 64 65 66 67 |
# File 'app/uploaders/camaleon_cms_aws_uploader.rb', line 62 def add_folder(key) s3_file = bucket.object(key.split('/').clean_empty.join('/') << '/') s3_file.put(body: nil) cache_item(file_parse(s3_file)) s3_file end |
#after_initialize ⇒ Object
3 4 5 6 7 8 9 |
# File 'app/uploaders/camaleon_cms_aws_uploader.rb', line 3 def after_initialize @cloudfront = @aws_settings[:cloud_front] || @current_site.get_option("filesystem_s3_cloudfront") @aws_region = @aws_settings[:region] || @current_site.get_option("filesystem_region", 'us-west-2') @aws_akey = @aws_settings[:access_key] || @current_site.get_option("filesystem_s3_access_key") @aws_asecret = @aws_settings[:secret_key] || @current_site.get_option("filesystem_s3_secret_key") @aws_bucket = @aws_settings[:bucket] || @current_site.get_option("filesystem_s3_bucket_name") end |
#browser_files ⇒ Object
recover all files from AWS and parse it to save into DB as cache
12 13 14 15 16 17 18 19 20 |
# File 'app/uploaders/camaleon_cms_aws_uploader.rb', line 12 def browser_files objects = {} objects['/'] = {files: {}, folders: {}} bucket.objects.each do |file| cache_item(file_parse(file), objects) end @current_site.(cache_key, objects) objects end |
#bucket ⇒ Object
initialize a bucket with AWS configurations return: (AWS Bucket object)
83 84 85 86 87 88 89 |
# File 'app/uploaders/camaleon_cms_aws_uploader.rb', line 83 def bucket @bucket ||= lambda{ config = Aws.config.update({ region: @aws_region, credentials: Aws::Credentials.new(@aws_akey, @aws_asecret) }) s3 = Aws::S3::Resource.new bucket = s3.bucket(@aws_bucket) }.call end |
#delete_file(key) ⇒ Object
delete a file in AWS with :key
76 77 78 79 |
# File 'app/uploaders/camaleon_cms_aws_uploader.rb', line 76 def delete_file(key) bucket.object(key.split('/').clean_empty.join('/')).delete rescue '' reload end |
#delete_folder(key) ⇒ Object
delete a folder in AWS with :key
70 71 72 73 |
# File 'app/uploaders/camaleon_cms_aws_uploader.rb', line 70 def delete_folder(key) bucket.objects(prefix: key.split('/').clean_empty.join('/') << '/').delete reload end |
#file_parse(s3_file) ⇒ Object
parse an AWS file into custom file_object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'app/uploaders/camaleon_cms_aws_uploader.rb', line 23 def file_parse(s3_file) key = s3_file.is_a?(String) ? s3_file : s3_file.key key = "/#{key}" unless key.starts_with?('/') is_dir = s3_file.is_a?(String) || File.extname(key) == '' res = { "name" => File.basename(key), "key" => key, "url" => is_dir ? '' : (@cloudfront.present? ? File.join(@cloudfront, key) : s3_file.public_url), "is_folder" => is_dir, "size" => is_dir ? 0 : s3_file.size.round(2), "format" => is_dir ? 'folder' : self.class.get_file_format(key), "deleteUrl" => '', "thumb" => '', 'type' => is_dir ? '' : (s3_file.content_type rescue (MIME::Types.type_for(key).first.content_type rescue "")), 'created_at' => is_dir ? '' : s3_file.last_modified, 'dimension' => '' }.with_indifferent_access res["thumb"] = version_path(res['url']) if res['format'] == 'image' && File.extname(res['name']).downcase != '.gif' if res['format'] == 'image' # TODO: Recover image dimension (suggestion: save dimesion as metadata) end res end |