Class: CamaleonCmsUploader
- Inherits:
-
Object
- Object
- CamaleonCmsUploader
- Defined in:
- app/uploaders/camaleon_cms_uploader.rb
Direct Known Subclasses
Constant Summary collapse
- PRIVATE_DIRECTORY =
'private'.freeze
Instance Attribute Summary collapse
-
#thumb ⇒ Object
Returns the value of attribute thumb.
Class Method Summary collapse
- .delete_block(&block) ⇒ Object
-
.get_file_format(path) ⇒ Object
return the file format (String) of path (depends of file extension).
-
.get_file_format_extensions(format) ⇒ Object
return the files extensión for each format support for multiples formats, sample: image,audio.
-
.validate_file_format(key, valid_formats = '*') ⇒ Object
verify permitted formats (return boolean true | false) true: if format is accepted false: if format is not accepted sample: validate_file_format(‘/var/www/myfile.xls’, ‘image,audio,docx,xls’) => return true if the file extension is in formats.
Instance Method Summary collapse
- #after_initialize ⇒ Object
-
#cache_item(file_parsed, _objects_db = nil, _custom_cache_key = nil) ⇒ Object
save file_parsed as a cache into DB file_parsed: (HASH) File parsed object objects_db: HASH Object where to add the current object (optional).
-
#clear_cache ⇒ Object
clean cached of files structure saved into DB.
- #disable_private_mode! ⇒ Object
- #enable_private_mode! ⇒ Object
- #file_exists?(file_name) ⇒ Boolean
-
#get_file(key, use_cache = true) ⇒ Object
check if file with :key exist and return parsed_file, else return nil.
-
#get_media_collection ⇒ Object
return the media collection for current situation.
-
#initialize(args = {}, instance = nil) ⇒ CamaleonCmsUploader
constructor
root_folder= ‘/var/www/my_public_foler/’, current_site= CamaSite.first.decorate, thumb = 100, h: 75, aws_settings: access_key, secret_key, bucket.
-
#objects(prefix = '/', _sort = 'created_at') ⇒ Object
load media files from a specific folder path.
-
#reload ⇒ Object
reload cache files structure.
-
#search(search_text) ⇒ Object
search for folders or files that includes search_text in their names.
-
#search_new_key(key) ⇒ Object
verify if this file name already exist if the file is already exist, return a new name for this file sample: search_new_key(“my_file/file.txt”).
- #valid_folder_path?(path) ⇒ Boolean
-
#version_path(image_path, version_name = nil) ⇒ Object
convert current string path into file version_path, sample: version_path(‘/media/1/screen.png’) into /media/1/thumb/screen-png.png (thumbs) Sample: version_path(‘/media/1/screen.png’, ‘200x200’) ==> /media/1/thumb/screen-png_200x200.png (image versions).
Constructor Details
#initialize(args = {}, instance = nil) ⇒ CamaleonCmsUploader
root_folder= ‘/var/www/my_public_foler/’, current_site= CamaSite.first.decorate, thumb = 100, h: 75, aws_settings: access_key, secret_key, bucket
8 9 10 11 12 13 14 15 16 |
# File 'app/uploaders/camaleon_cms_uploader.rb', line 8 def initialize(args = {}, instance = nil) @current_site = args[:current_site] t_w, t_h = @current_site.get_option('filesystem_thumb_size', '100x100').split('x') @thumb = args[:thumb] || { w: t_w, h: t_h } @aws_settings = args[:aws_settings] || {} @args = args @instance = instance after_initialize end |
Instance Attribute Details
#thumb ⇒ Object
Returns the value of attribute thumb.
4 5 6 |
# File 'app/uploaders/camaleon_cms_uploader.rb', line 4 def thumb @thumb end |
Class Method Details
.delete_block(&block) ⇒ Object
167 168 169 170 |
# File 'app/uploaders/camaleon_cms_uploader.rb', line 167 def self.delete_block(&block) @delete_block = block if block @delete_block end |
.get_file_format(path) ⇒ Object
return the file format (String) of path (depends of file extension)
82 83 84 85 86 87 88 89 90 91 |
# File 'app/uploaders/camaleon_cms_uploader.rb', line 82 def self.get_file_format(path) ext = File.extname(path).sub('.', '').downcase format = 'unknown' format = 'image' if get_file_format_extensions('image').split(',').include?(ext) format = 'video' if get_file_format_extensions('video').split(',').include?(ext) format = 'audio' if get_file_format_extensions('audio').split(',').include?(ext) format = 'document' if get_file_format_extensions('document').split(',').include?(ext) format = 'compress' if get_file_format_extensions('compress').split(',').include?(ext) format end |
.get_file_format_extensions(format) ⇒ Object
return the files extensión for each format support for multiples formats, sample: image,audio
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'app/uploaders/camaleon_cms_uploader.rb', line 95 def self.get_file_format_extensions(format) res = [] format.downcase.gsub(' ', '').split(',').each do |f| res << case f when 'image', 'images' 'jpg,jpeg,png,gif,bmp,ico,svg' when 'video', 'videos' 'flv,webm,wmv,avi,swf,mp4,mov,mpg' when 'audio' 'mp3,ogg' when 'document', 'documents' 'pdf,xls,xlsx,doc,docx,ppt,pptx,html,txt,xml,json' when 'compress' 'zip,7z,rar,tar,bz2,gz,rar2' else '' end end res.join(',') end |
.validate_file_format(key, valid_formats = '*') ⇒ Object
verify permitted formats (return boolean true | false) true: if format is accepted false: if format is not accepted sample: validate_file_format(‘/var/www/myfile.xls’, ‘image,audio,docx,xls’) => return true if the file extension is in formats
120 121 122 123 124 125 126 |
# File 'app/uploaders/camaleon_cms_uploader.rb', line 120 def self.validate_file_format(key, valid_formats = '*') return true if valid_formats == '*' || !valid_formats.present? valid_formats = valid_formats.gsub(' ', '').downcase.split(',') + get_file_format_extensions(valid_formats).split(',') valid_formats.include?(File.extname(key).sub('.', '').split('?').first.try(:downcase)) end |
Instance Method Details
#after_initialize ⇒ Object
18 |
# File 'app/uploaders/camaleon_cms_uploader.rb', line 18 def after_initialize; end |
#cache_item(file_parsed, _objects_db = nil, _custom_cache_key = nil) ⇒ Object
save file_parsed as a cache into DB file_parsed: (HASH) File parsed object objects_db: HASH Object where to add the current object (optional)
58 59 60 61 62 63 64 |
# File 'app/uploaders/camaleon_cms_uploader.rb', line 58 def cache_item(file_parsed, _objects_db = nil, _custom_cache_key = nil) unless get_media_collection.where(name: file_parsed['name'], folder_path: file_parsed['folder_path']).any? a = get_media_collection.new(file_parsed.except('key')) a.save! end file_parsed end |
#clear_cache ⇒ Object
clean cached of files structure saved into DB
41 42 43 |
# File 'app/uploaders/camaleon_cms_uploader.rb', line 41 def clear_cache get_media_collection.destroy_all end |
#disable_private_mode! ⇒ Object
159 160 161 |
# File 'app/uploaders/camaleon_cms_uploader.rb', line 159 def disable_private_mode! @args[:private] = false end |
#enable_private_mode! ⇒ Object
153 154 155 156 157 |
# File 'app/uploaders/camaleon_cms_uploader.rb', line 153 def enable_private_mode! @args[:private] = true setup_private_folder end |
#file_exists?(file_name) ⇒ Boolean
163 164 165 |
# File 'app/uploaders/camaleon_cms_uploader.rb', line 163 def file_exists?(file_name) File.exist?(file_name) end |
#get_file(key, use_cache = true) ⇒ Object
check if file with :key exist and return parsed_file, else return nil
149 150 151 |
# File 'app/uploaders/camaleon_cms_uploader.rb', line 149 def get_file(key, use_cache = true) # deprecated end |
#get_media_collection ⇒ Object
return the media collection for current situation
67 68 69 |
# File 'app/uploaders/camaleon_cms_uploader.rb', line 67 def get_media_collection is_private_uploader? ? @current_site.public_media : @current_site.private_media end |
#objects(prefix = '/', _sort = 'created_at') ⇒ Object
load media files from a specific folder path
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'app/uploaders/camaleon_cms_uploader.rb', line 21 def objects(prefix = '/', _sort = 'created_at') prefix = prefix.cama_fix_media_key browser_files unless get_media_collection.any? res = if ['/', ''].include?(prefix) get_media_collection.where(folder_path: '/') else get_media_collection.find_by_key(prefix).take.try(:items) end # Private hook to recover custom files to include in current list where data can be modified to add custom{files, folders} # Note: this hooks doesn't have access to public vars like params. requests, ... if @instance args = { data: res, prefix: prefix } @instance.hooks_run('uploader_list_objects', args) res = args[:data] end res end |
#reload ⇒ Object
reload cache files structure
51 52 53 |
# File 'app/uploaders/camaleon_cms_uploader.rb', line 51 def reload browser_files end |
#search(search_text) ⇒ Object
search for folders or files that includes search_text in their names
46 47 48 |
# File 'app/uploaders/camaleon_cms_uploader.rb', line 46 def search(search_text) get_media_collection.search(search_text) end |
#search_new_key(key) ⇒ Object
verify if this file name already exist if the file is already exist, return a new name for this file sample: search_new_key(“my_file/file.txt”)
137 138 139 140 141 142 143 144 145 146 |
# File 'app/uploaders/camaleon_cms_uploader.rb', line 137 def search_new_key(key) _key = key if get_media_collection.find_by_key(key).any? (1..999).each do |i| _key = key.cama_add_postfix_file_name("_#{i}") break unless get_media_collection.find_by_key(_key).any? end end _key end |
#valid_folder_path?(path) ⇒ Boolean
128 129 130 131 132 |
# File 'app/uploaders/camaleon_cms_uploader.rb', line 128 def valid_folder_path?(path) return false if path.include?('..') || path.include?('://') true end |
#version_path(image_path, version_name = nil) ⇒ Object
convert current string path into file version_path, sample: version_path(‘/media/1/screen.png’) into /media/1/thumb/screen-png.png (thumbs) Sample: version_path(‘/media/1/screen.png’, ‘200x200’) ==> /media/1/thumb/screen-png_200x200.png (image versions)
74 75 76 77 78 79 |
# File 'app/uploaders/camaleon_cms_uploader.rb', line 74 def version_path(image_path, version_name = nil) res = File.join(File.dirname(image_path), 'thumb', "#{File.basename(image_path).parameterize}#{File.extname(image_path)}") res = res.cama_add_postfix_file_name("_#{version_name}") if version_name.present? res end |