Class: CamaleonCmsAwsUploader

Inherits:
CamaleonCmsUploader show all
Defined in:
app/uploaders/camaleon_cms_aws_uploader.rb

Instance Attribute Summary

Attributes inherited from CamaleonCmsUploader

#thumb

Instance Method Summary collapse

Methods inherited from CamaleonCmsUploader

#after_initialize, #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


43
44
45
46
47
48
49
50
# File 'app/uploaders/camaleon_cms_aws_uploader.rb', line 43

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



53
54
55
56
57
58
# File 'app/uploaders/camaleon_cms_aws_uploader.rb', line 53

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

#browser_filesObject

recover all files from AWS and parse it to save into DB as cache



3
4
5
6
7
8
9
10
11
# File 'app/uploaders/camaleon_cms_aws_uploader.rb', line 3

def browser_files
  objects = {}
  objects['/'] = {files: {}, folders: {}}
  bucket.objects.each do |file|
    cache_item(file_parse(file), objects)
  end
  @current_site.set_meta(cache_key, objects)
  objects
end

#bucketObject

initialize a bucket with AWS configurations return: (AWS Bucket object)



74
75
76
77
78
79
80
# File 'app/uploaders/camaleon_cms_aws_uploader.rb', line 74

def bucket
  @bucket ||= lambda{
    config = Aws.config.update({ region: @current_site.get_option("filesystem_region", 'us-west-2'), credentials: Aws::Credentials.new(@current_site.get_option("filesystem_s3_access_key"), @current_site.get_option("filesystem_s3_secret_key")) })
    s3 = Aws::S3::Resource.new
    bucket = s3.bucket(@current_site.get_option("filesystem_s3_bucket_name"))
  }.call
end

#delete_file(key) ⇒ Object

delete a file in AWS with :key



67
68
69
70
# File 'app/uploaders/camaleon_cms_aws_uploader.rb', line 67

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



61
62
63
64
# File 'app/uploaders/camaleon_cms_aws_uploader.rb', line 61

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



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/uploaders/camaleon_cms_aws_uploader.rb', line 14

def file_parse(s3_file)
  key = s3_file.is_a?(String) ? s3_file : s3_file.key
  key = "/#{key}" unless key.starts_with?('/')
  is_dir = File.extname(key) == ''
  res = {
      "name" => File.basename(key),
      "key" => key,
      "url" => is_dir ? '' : (@current_site.get_option("filesystem_s3_cloudfront").present? ? File.join(@current_site.get_option("filesystem_s3_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