Class: CamaleonCmsUploader

Inherits:
Object
  • Object
show all
Defined in:
app/uploaders/camaleon_cms_uploader.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ CamaleonCmsUploader

root_folder, current_site, service = ‘Local’, thumb



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

def initialize(args = {})
  @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}
  @args = args
  after_initialize
end

Instance Attribute Details

#thumbObject

Returns the value of attribute thumb.



2
3
4
# File 'app/uploaders/camaleon_cms_uploader.rb', line 2

def thumb
  @thumb
end

Class Method Details

.get_file_format(path) ⇒ Object

return the file format (String) of path (depends of file extension)



80
81
82
83
84
85
86
87
88
89
# File 'app/uploaders/camaleon_cms_uploader.rb', line 80

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



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/uploaders/camaleon_cms_uploader.rb', line 93

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"
              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



118
119
120
121
122
# File 'app/uploaders/camaleon_cms_uploader.rb', line 118

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(".", "").downcase)
end

Instance Method Details

#after_initializeObject



12
13
14
# File 'app/uploaders/camaleon_cms_uploader.rb', line 12

def after_initialize

end

#cache_item(file_parsed, _objects_db = 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)



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/uploaders/camaleon_cms_uploader.rb', line 51

def cache_item(file_parsed, _objects_db = nil)
  objects_db = _objects_db || @current_site.get_meta(cache_key, {}) || {}
  prefix = File.dirname(file_parsed['key'])

  s = prefix.split('/').clean_empty
  return file_parsed if s.last == 'thumb'
  s.each_with_index{|_s, i| k = "/#{File.join(s.slice(0, i), _s)}"; cache_item(file_parse(k), objects_db) unless objects_db[k].present? } unless ['/', '', '.'].include?(prefix)

  objects_db[prefix] = {files: {}, folders: {}} if objects_db[prefix].nil?
  if file_parsed['format'] == 'folder'
    objects_db[prefix][:folders][file_parsed['name']] = file_parsed
  else
    objects_db[prefix][:files][file_parsed['name']] = file_parsed
  end
  @current_site.set_meta(cache_key, objects_db) if _objects_db.nil?
  file_parsed
end

#clear_cacheObject

clean cached of files structure saved into DB



26
27
28
# File 'app/uploaders/camaleon_cms_uploader.rb', line 26

def clear_cache
  @current_site.set_meta(cache_key, nil)
end

#get_file(key, use_cache = true) ⇒ Object

check if file with :key exist and return parsed_file, else return nil



140
141
142
143
144
145
146
147
# File 'app/uploaders/camaleon_cms_uploader.rb', line 140

def get_file(key, use_cache = true)
  if use_cache
    db = (@current_site.get_meta(cache_key) || {})[File.dirname(key)] || {}
  else
    db = objects(File.dirname(key)) unless use_cache
  end
  (db[:files][File.basename(key)] || db[:folders][File.basename(key)]) rescue nil
end

#objects(prefix = '/') ⇒ Object

return all files structure, within folder prefix return json like: {‘file_name’: {‘name’=> ‘a.jpg’, key: ‘/test/a.jpg’, url: ”, url: ”, size: ”, format: ”, thumb: ‘thumb_url’, type: ”, created_at: ”, dimension: ‘120x120’}, folders: name’ => {name: ‘folder name’, key: ‘/folder name’, …}}



19
20
21
22
23
# File 'app/uploaders/camaleon_cms_uploader.rb', line 19

def objects(prefix = '/')
  prefix = "/#{prefix}" unless prefix.starts_with?('/')
  db = @current_site.get_meta(cache_key, nil) || browser_files
  db[prefix.gsub('//', '/')] || {files: {}, folders: {}}
end

#reloadObject

reload cache files structure



44
45
46
# File 'app/uploaders/camaleon_cms_uploader.rb', line 44

def reload
  browser_files
end

#search(search_text) ⇒ Object

search for folders or files that includes search_text in their names



31
32
33
34
35
36
37
38
39
40
41
# File 'app/uploaders/camaleon_cms_uploader.rb', line 31

def search(search_text)
  res = {files: {}, folders: {}}
  (@current_site.get_meta(cache_key, nil) || browser_files).each do |folder_key, object|
    res[:folders][folder_key] = get_file(folder_key) if !['', '/'].include?(folder_key) && folder_key.split('/').last.include?(search_text)
    object[:files].each do |file_key, obj|
      res[:files][file_key] = obj if file_key.include?(search_text)
    end
    res
  end
  res
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”)



128
129
130
131
132
133
134
135
136
137
# File 'app/uploaders/camaleon_cms_uploader.rb', line 128

def search_new_key(key)
  _key = key
  if get_file(key).present?
    (1..999).each do |i|
      _key = key.cama_add_postfix_file_name("_#{i}")
      break unless get_file(_key).present?
    end
  end
  _key
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)



73
74
75
76
77
# File 'app/uploaders/camaleon_cms_uploader.rb', line 73

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