Class: CamaleonCmsLocalUploader
Constant Summary
collapse
- PRIVATE_DIRECTORY =
'private'
Instance Attribute Summary
#thumb
Class Method Summary
collapse
Instance Method Summary
collapse
#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
Class Method Details
.private_file_path(key, current_site) ⇒ Object
return the full file path for private file with key sample: ‘my_file.pdf’ ==> /var/www/my_app/private/my_file.pdf
20
21
22
|
# File 'app/uploaders/camaleon_cms_local_uploader.rb', line 20
def self.private_file_path(key, current_site)
Rails.root.join(self::PRIVATE_DIRECTORY, current_site.id.to_s, key.gsub(/(\/){2,}/, "/")).to_s
end
|
Instance Method Details
#add_file(uploaded_io_or_file_path, key, args = {}) ⇒ Object
63
64
65
66
67
68
69
70
71
|
# File 'app/uploaders/camaleon_cms_local_uploader.rb', line 63
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]
add_folder(File.dirname(key)) if File.dirname(key).present?
upload_io = uploaded_io_or_file_path.is_a?(String) ? File.open(uploaded_io_or_file_path) : uploaded_io_or_file_path
File.open(File.join(@root_folder, key), 'wb'){|file| file.write(upload_io.read) }
res = cache_item(file_parse(key)) unless args[:is_thumb]
res
end
|
#add_folder(key) ⇒ Object
73
74
75
76
77
78
79
80
81
82
|
# File 'app/uploaders/camaleon_cms_local_uploader.rb', line 73
def add_folder(key)
d, is_new_folder = File.join(@root_folder, key).to_s, false
unless Dir.exist?(d)
FileUtils.mkdir_p(d)
is_new_folder = true if File.basename(d) != 'thumb'
end
f = file_parse(key)
cache_item(f) if is_new_folder
f
end
|
#after_initialize ⇒ Object
29
30
31
32
33
34
35
36
|
# File 'app/uploaders/camaleon_cms_local_uploader.rb', line 29
def after_initialize
if is_private_uploader?
@root_folder = Rails.root.join(self.class::PRIVATE_DIRECTORY, @current_site.id.to_s).to_s
else
@root_folder = @args[:root_folder] || @current_site.upload_directory
end
FileUtils.mkdir_p(@root_folder) unless Dir.exist?(@root_folder)
end
|
#browser_files(prefix = '/', objects = {}) ⇒ Object
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
# File 'app/uploaders/camaleon_cms_local_uploader.rb', line 3
def browser_files(prefix = '/', objects = {})
objects[prefix] = {files: {}, folders: {}}
path = File.join(@root_folder, prefix)
Dir.entries(path).each do |f_name|
next if f_name == '..' || f_name == '.' || f_name == 'thumb'
f_path = File.join(path, f_name)
key = parse_key(f_path)
obj = get_file(key) || file_parse(key)
cache_item(obj, objects)
browser_files(File.join(prefix, obj['name']), objects) if obj['format'] == 'folder'
end
@current_site.set_meta(cache_key, objects) if prefix == '/'
objects
end
|
#delete_file(key) ⇒ Object
89
90
91
92
|
# File 'app/uploaders/camaleon_cms_local_uploader.rb', line 89
def delete_file(key)
FileUtils.rm(File.join(@root_folder, key))
reload
end
|
#delete_folder(key) ⇒ Object
84
85
86
87
|
# File 'app/uploaders/camaleon_cms_local_uploader.rb', line 84
def delete_folder(key)
FileUtils.rm_rf(File.join(@root_folder, key))
reload
end
|
#file_parse(key) ⇒ Object
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'app/uploaders/camaleon_cms_local_uploader.rb', line 38
def file_parse(key)
file_path = File.join(@root_folder, key)
url_path, is_dir = file_path.sub(Rails.root.join('public').to_s, ''), File.directory?(file_path)
res = {
"name" => File.basename(file_path),
"key" => parse_key(file_path),
"url" => is_dir ? '' : (is_private_uploader? ? url_path.sub("#{@root_folder}/", '') : File.join(@current_site.decorate.the_url(locale: false), url_path)),
"is_folder" => is_dir,
"size" => is_dir ? 0 : File.size(file_path).round(2),
"format" => is_dir ? 'folder' : self.class.get_file_format(file_path),
"deleteUrl" => '',
"thumb" => '',
'type' => (MIME::Types.type_for(file_path).first.content_type rescue ""),
'created_at' => File.mtime(file_path),
'dimension' => ''
}.with_indifferent_access
res["thumb"] = version_path(res['url']) if res['format'] == 'image' && File.extname(file_path).downcase != '.gif'
if res['format'] == 'image'
im = MiniMagick::Image.open(file_path)
res['dimension'] = "#{im[:width]}x#{im[:height]}"
end
res
end
|
#is_private_uploader? ⇒ Boolean
check if this uploader is private mode
25
26
27
|
# File 'app/uploaders/camaleon_cms_local_uploader.rb', line 25
def is_private_uploader?
@args[:private]
end
|
#parse_key(file_path) ⇒ Object
convert a real file path into file key
95
96
97
98
99
|
# File 'app/uploaders/camaleon_cms_local_uploader.rb', line 95
def parse_key(file_path)
r = file_path.sub(@root_folder, '')
r = "/#{r}" unless r.starts_with?('/')
r
end
|