Class: AmcmsFilemanager::Filemanager

Inherits:
Object
  • Object
show all
Defined in:
app/lib/amcms_filemanager/filemanager.rb

Overview

Filemanager class for handling all things file related

Class Method Summary collapse

Class Method Details

.copy(filename) ⇒ Sttring

Make a copy of file in it’s current directory

Parameters:

  • dir (String | nil)

Returns:

  • (Sttring)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/lib/amcms_filemanager/filemanager.rb', line 9

def copy(filename)
  source_file = Rails.root.join('public', filename.gsub(/^\//, ''))

  return false if File.directory?(source_file)

  destination_dir = File.dirname(source_file)
  base_name = File.basename(source_file)

  destination_path = File.join(destination_dir, base_name)

  if File.exist?(destination_path)
    i = 1
    while File.exist?(destination_path)
      unique_name = "#{File.basename(source_file, '.*')}_#{i}#{File.extname(source_file)}"
      destination_path = File.join(destination_dir, unique_name)
      i += 1
    end
  end

  FileUtils.cp(source_file, destination_path)
  destination_path
end

.delete(file) ⇒ Integer

Delete a file or folder

Parameters:

  • file (String)

Returns:

  • (Integer)


85
86
87
88
89
90
91
# File 'app/lib/amcms_filemanager/filemanager.rb', line 85

def delete(file)
  filepath = Rails.root.join('public', file.gsub(%r{^/}, ''))

  return File.delete(filepath) unless File.directory?(filepath)

  FileUtils.remove_dir(filepath)
end

.directory?(filename) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
# File 'app/lib/amcms_filemanager/filemanager.rb', line 32

def directory?(filename)
  source_file = Rails.root.join('public', filename.gsub(/^\//, ''))

  File.directory?(source_file)
end

.exists?(dir, filename) ⇒ TrueClass, FalseClass

Check to see if the directory exists

Parameters:

  • dir (String)
  • filename (String)

Returns:

  • (TrueClass, FalseClass)


77
78
79
80
# File 'app/lib/amcms_filemanager/filemanager.rb', line 77

def exists?(dir, filename)
  dir = AmcmsFilemanager.config.root_path unless dir.present?
  File.exist?(File.join(Rails.root.join('public', dir.gsub(%r{^/}, '')), filename))
end

.filepath(dir, filename) ⇒ String

Create the filepath

Parameters:

  • dir (String)
  • filename (String)

Returns:

  • (String)


121
122
123
124
# File 'app/lib/amcms_filemanager/filemanager.rb', line 121

def filepath(dir, filename)
  dir = "#{AmcmsFilemanager.config.root_path}/#{dir}" unless dir.include? AmcmsFilemanager.config.root_path
  Rails.root.join('public', dir.gsub(%r{^/}, ''), filename)
end

.image?(file) ⇒ TrueClass, FalseClass

Detect if the file is an image

Parameters:

  • file (File)

Returns:

  • (TrueClass, FalseClass)


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/lib/amcms_filemanager/filemanager.rb', line 129

def image?(file)
  return false if File.directory?(file)

  web_safe_images = [
    'image/apng',
    'image/avif',
    'image/gif',
    'image/jpeg',
    'image/jpg',
    'image/png',
    'image/webp'
  ].freeze

  web_safe_images.include?(mimetype(file))
end

.list(dir = nil, type = 'image') ⇒ Array

List all files in a given directory

Parameters:

  • dir (String | nil) (defaults to: nil)
  • type (String) (defaults to: 'image')

Returns:

  • (Array)


42
43
44
45
46
47
48
49
50
51
52
# File 'app/lib/amcms_filemanager/filemanager.rb', line 42

def list(dir = nil, type = 'image')
  dir = AmcmsFilemanager.config.root_path unless dir.present?
  file_filter = AmcmsFilemanager.config.extension_allowlist.present? ? "{*/,*.{#{AmcmsFilemanager.config.extension_allowlist[type.to_sym].join(',')}}}" : '*'
  entries = Dir.glob(Rails.root.join('public', dir.gsub(%r{^/}, ''), file_filter))
  entries.map! { |entry| file_details(entry) }

  folders = entries.select { |entry| entry if entry[:directory] }.sort_by { |entry| entry[:filename] }.compact
  files = entries.reject { |entry| entry if entry[:directory] }.sort_by { |entry| entry[:filename] }.compact

  folders + files
end

.mkdir(dir, directory_name) ⇒ Integer

Make a new directory

Parameters:

  • dir (String)
  • directory_name (String)

Returns:

  • (Integer)


68
69
70
71
# File 'app/lib/amcms_filemanager/filemanager.rb', line 68

def mkdir(dir, directory_name)
  dir = AmcmsFilemanager.config.root_path unless dir.present?
  Dir.mkdir(File.join(Rails.root.join('public', dir.gsub(%r{^/}, '')), directory_name), 0o775)
end

.parent_dir(dir = nil) ⇒ String

Return the parent directory

Parameters:

  • dir (String | nil) (defaults to: nil)

Returns:

  • (String)


57
58
59
60
61
62
# File 'app/lib/amcms_filemanager/filemanager.rb', line 57

def parent_dir(dir = nil)
  dir = AmcmsFilemanager.config.root_path if !dir.present? || dir.blank?
  return dir if dir == AmcmsFilemanager.config.root_path

  File.dirname(dir).gsub(%r{^/}, '')
end

.rename(dir, original_filename, new_filename) ⇒ Integer

Renames a file or directory

Parameters:

  • dir (String)
  • original_filename (String)
  • new_filename (String)

Returns:

  • (Integer)


98
99
100
101
102
103
# File 'app/lib/amcms_filemanager/filemanager.rb', line 98

def rename(dir, original_filename, new_filename)
  original_filepath = filepath(dir, original_filename)
  new_filepath = filepath(dir, new_filename)

  File.rename(original_filepath, new_filepath)
end

.upload(dir, file, type = 'image') ⇒ Object

Upload a file to the given directory

Parameters:

  • dir (String)
  • file (ActionDispatch::Http::UploadedFile)
  • type (String) (defaults to: 'image')


109
110
111
112
113
114
115
# File 'app/lib/amcms_filemanager/filemanager.rb', line 109

def upload(dir, file, type = 'image')
  uploader = AmcmsFilemanager::FilemanagerUploader.new(type)
  uploader.define_singleton_method(:store_dir) do
    AmcmsFilemanager::Filemanager.filepath(dir, '')
  end
  uploader.store!(file)
end