Class: CamaleonCmsLocalUploader

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

Constant Summary

Constants inherited from CamaleonCmsUploader

CamaleonCmsUploader::PRIVATE_DIRECTORY, CamaleonCmsUploader::SVG_EXT_PATTERN

Instance Attribute Summary

Attributes inherited from CamaleonCmsUploader

#thumb

Instance Method Summary collapse

Methods inherited from CamaleonCmsUploader

#cache_item, #clear_cache, delete_block, #disable_private_mode!, #enable_private_mode!, #file_exists?, #get_file, get_file_format, get_file_format_extensions, #get_media_collection, #initialize, #objects, #reload, #search, #search_new_key, #valid_folder_path?, 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

save a file into local folder



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/uploaders/camaleon_cms_local_uploader.rb', line 92

def add_file(uploaded_io_or_file_path, key, args = {})
  args = { same_name: false, is_thumb: false }.merge(args)
  res = nil
  key = search_new_key(key) unless args[:same_name]

  if @instance # private hook to upload files by different way, add file data into result_data
    _args = { result_data: nil, file: uploaded_io_or_file_path, key: key, args: args, klass: self }
    @instance.hooks_run('uploader_local_before_upload', _args)
    return _args[:result_data] if _args[:result_data].present?
  end

  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

create a new folder into local directory



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/uploaders/camaleon_cms_local_uploader.rb', line 111

def add_folder(key)
  return { error: 'Invalid folder path' } unless valid_folder_path?(key)

  d = File.join(@root_folder, key).to_s
  is_new_folder = 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_initializeObject



2
3
4
5
6
# File 'app/uploaders/camaleon_cms_local_uploader.rb', line 2

def after_initialize
  @root_folder = @current_site.upload_directory

  FileUtils.mkdir_p(@root_folder)
end

#browser_files(prefix = '/', _objects = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/uploaders/camaleon_cms_local_uploader.rb', line 14

def browser_files(prefix = '/', _objects = {})
  path = File.join(@root_folder, prefix)

  Dir.entries(path).each do |f_name|
    next if ['..', '.', 'thumb'].include?(f_name)

    obj = file_parse(File.join(path, f_name).sub(@root_folder, '').cama_fix_media_key)
    cache_item(obj)

    browser_files(File.join(prefix, obj['name'])) if obj['is_folder']
  end
end

#cama_prepare_browser_page(items) ⇒ Object

Repairs legacy thumbnail URLs (see #cama_compat_legacy_thumb) for one rendered page of media items. The cache (media DB records) may still hold thumb URLs computed from the source extension (sample: ".jpg") while the on-disk thumbnail is a legacy ".png"; without this the admin media browser would render 404 thumbnails for such files. Applied by the media controller to the paginated page only — #objects stays a lazy relation so the browser paginates at the database instead of materializing (and stat-ing) the whole folder.



41
42
43
44
# File 'app/uploaders/camaleon_cms_local_uploader.rb', line 41

def cama_prepare_browser_page(items)
  items.each { |item| cama_fix_legacy_thumb_item(item) }
  items
end

#delete_file(key) ⇒ Object

Remove an existent file



135
136
137
138
139
140
141
142
# File 'app/uploaders/camaleon_cms_local_uploader.rb', line 135

def delete_file(key)
  return { error: 'Invalid file path' } if key.include?('..')

  file = File.join(@root_folder, key)
  FileUtils.rm(file) if File.exist? file
  @instance.hooks_run('after_delete', key)
  get_media_collection.by_key(key).take.destroy
end

#delete_folder(key) ⇒ Object

Remove an existent folder



126
127
128
129
130
131
132
# File 'app/uploaders/camaleon_cms_local_uploader.rb', line 126

def delete_folder(key)
  return { error: 'Invalid folder path' } if key.include?('..')

  folder = File.join(@root_folder, key)
  FileUtils.rm_rf(folder) if Dir.exist? folder
  get_media_collection.by_key(key).take.destroy
end

#fetch_file(file_name) ⇒ Object



27
28
29
30
31
32
33
# File 'app/uploaders/camaleon_cms_local_uploader.rb', line 27

def fetch_file(file_name)
  return { error: 'Invalid file path' } unless valid_folder_path?(file_name)

  return file_name if file_exists?(file_name)

  { error: 'File not found' }
end

#file_parse(key) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/uploaders/camaleon_cms_local_uploader.rb', line 46

def file_parse(key)
  file_path = File.join(@root_folder, key)
  url_path = file_path.sub(Rails.root.join('public').to_s, '')
  is_dir = File.directory?(file_path)
  res = {
    'name' => File.basename(key),
    'folder_path' => File.dirname(key),
    'url' => if is_dir
               ''
             elsif is_private_uploader?
               url_path.sub("#{@root_folder}/", '')
             else
               File.join(
                 @current_site.decorate.the_url(as_path: true, locale: false,
                                                skip_relative_url_root: true), url_path
               )
             end,
    'is_folder' => is_dir,
    'file_size' => is_dir ? 0 : File.size(file_path).round(2),
    'thumb' => '',
    'file_type' => self.class.get_file_format(file_path),
    'dimension' => ''
  }.with_indifferent_access
  res['key'] = File.join(res['folder_path'], res['name'])
  if res['file_type'] == 'image' && File.extname(file_path).downcase != '.gif'
    res['thumb'] = if is_private_uploader?
                     "/admin/media/download_private_file?file=#{version_path(key).slice(1..-1)}"
                   else
                     version_path(res['url'])
                   end
  end
  if res['file_type'] == 'image'
    res['thumb'].sub!(SVG_EXT_PATTERN, '.jpg')
    res['thumb'] = cama_compat_legacy_thumb(res['thumb'], version_path(key).sub(SVG_EXT_PATTERN, '.jpg'),
                                            res['url'])
    im = MiniMagick::Image.open(file_path)
    res['dimension'] = begin
      "#{im[:width]}x#{im[:height]}"
    rescue StandardError
      '0x0'
    end
  end
  res
end

#parse_key(file_path) ⇒ Object

Convert a real file path into a file key



145
146
147
# File 'app/uploaders/camaleon_cms_local_uploader.rb', line 145

def parse_key(file_path)
  file_path.sub(@root_folder, '').cama_fix_media_key
end

#setup_private_folderObject



8
9
10
11
12
# File 'app/uploaders/camaleon_cms_local_uploader.rb', line 8

def setup_private_folder
  @root_folder = Rails.root.join(self.class::PRIVATE_DIRECTORY).to_s

  FileUtils.mkdir_p(@root_folder)
end