module NauktisUtils::FileBrowser

Provide some utility methods for file handling.

Public Class Methods

contains_glob_character?(path) click to toggle source

Returns true if the string provided contains characters that will be interpreted in a glob operation.

# File lib/nauktis_utils/file_browser.rb, line 31
def self.contains_glob_character?(path)
  full_path = File.expand_path(path)
  ['*', '?', '[', '{'].each do |s|
    return true if full_path.include?(s)
  end
  return false
end
copy_file(file, destination_folder) click to toggle source

Copy a file to destination appending a number if the file already exists at destination.

# File lib/nauktis_utils/file_browser.rb, line 49
def self.copy_file(file, destination_folder)
  destination_folder = self.ensure_valid_directory(destination_folder)
  file_path = self.ensure_valid_file(file)

  file_ext = File.extname(file_path)
  file_basename = File.basename(file_path)
  file_base = File.basename(file_path, file_ext)
  final_file = File.join(destination_folder, file_basename)
  i = 0
  while File.exist?(final_file) do
    i += 1
    final_file = File.join(destination_folder, "#{file_base}#{i}#{file_ext}")
  end
  FileUtils.cp(file_path, final_file)
end
delete_ds_store(directory) click to toggle source

Deletes all the .DS_Store

# File lib/nauktis_utils/file_browser.rb, line 66
def self.delete_ds_store(directory)
  %x(find #{File.expand_path(directory)} -name \.DS_Store -exec rm {} \;)
end
delete_empty_directories(directory) click to toggle source

Recursively remove all empty directories

# File lib/nauktis_utils/file_browser.rb, line 71
def self.delete_empty_directories(directory)
  %x(find #{File.expand_path(directory)} -type d -empty -delete)
end
each_file(directory) { |expand_path| ... } click to toggle source

Recursively goes through all the files contained in a directory.

# File lib/nauktis_utils/file_browser.rb, line 40
def self.each_file(directory)
  raise "Can't use glob on #{directory_path}, dangerous character #{s}" if contains_glob_character?(directory)
  Dir.glob(File.join(File.expand_path(directory), '**', '*'), File::FNM_DOTMATCH).each do |entry|
    next if File.directory?(entry)
    yield(File.expand_path(entry))
  end
end
ensure_valid_directory(directory) click to toggle source

Raises an exception if the path provided is not an existing directory. Returns the expanded path of the directory

# File lib/nauktis_utils/file_browser.rb, line 25
def self.ensure_valid_directory(directory)
  raise "#{directory} is not a valid directory." unless self.valid_directory?(directory)
  File.expand_path(directory)
end
ensure_valid_file(filename) click to toggle source

Raises an exception if the path provided is not an existing file. Returns the expanded path of the file

# File lib/nauktis_utils/file_browser.rb, line 18
def self.ensure_valid_file(filename)
  raise "#{filename} is not a valid file." unless self.valid_file?(filename)
  File.expand_path(filename)
end
sanitize_filename(filename) click to toggle source
# File lib/nauktis_utils/file_browser.rb, line 83
def self.sanitize_filename(filename)
  name = File.basename(filename, File.extname(filename))
  name = self.sanitize_name(name)
  dirname = File.dirname(filename)
  if dirname != '.'
    File.join(dirname, "#{name}#{File.extname(filename).downcase}")
  else
    "#{name}#{File.extname(filename).downcase}"
  end
end
sanitize_name(name) click to toggle source

Only keeps alpha numeric characters in a String. Also replaces spaces by underscores.

# File lib/nauktis_utils/file_browser.rb, line 76
def self.sanitize_name(name)
  sanitized = name.strip
  sanitized.gsub!(/[^\w\s\-\.]+/, '')
  sanitized.gsub!(/[[:space:]]+/, '_')
  sanitized
end
valid_directory?(directory) click to toggle source

Returns true if the file provided is a valid (i.e. existing) directory.

# File lib/nauktis_utils/file_browser.rb, line 11
def self.valid_directory?(directory)
  full_path = File.expand_path(directory)
  File.exist?(full_path) and File.directory?(full_path)
end
valid_file?(filename) click to toggle source

Returns true if the file provided is a valid (i.e. existing) file.

# File lib/nauktis_utils/file_browser.rb, line 5
def self.valid_file?(filename)
  full_path = File.expand_path(filename)
  File.exist?(full_path) and not File.directory?(full_path)
end