Module: NauktisUtils::FileBrowser

Defined in:
lib/nauktis_utils/file_browser.rb

Overview

Provide some utility methods for file handling.

Class Method Summary collapse

Class Method Details

.contains_glob_character?(path) ⇒ Boolean

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

Returns:

  • (Boolean)


31
32
33
34
35
36
37
# 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) ⇒ Object

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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) ⇒ Object

Deletes all the .DS_Store



66
67
68
# 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) ⇒ Object

Recursively remove all empty directories



71
72
73
# 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) ⇒ Object

Recursively goes through all the files contained in a directory.



40
41
42
43
44
45
46
# 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) ⇒ Object

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



25
26
27
28
# 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) ⇒ Object

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



18
19
20
21
# 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) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# 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) ⇒ Object

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



76
77
78
79
80
81
# 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) ⇒ Boolean

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

Returns:

  • (Boolean)


11
12
13
14
# 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) ⇒ Boolean

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

Returns:

  • (Boolean)


5
6
7
8
# 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