Class: LocalFileManager

Inherits:
FileManager show all
Defined in:
lib/local_file_manager.rb

Instance Attribute Summary

Attributes inherited from FileManager

#options

Instance Method Summary collapse

Methods inherited from FileManager

#initialize

Constructor Details

This class inherits a constructor from FileManager

Instance Method Details

#copy_file(source_file_name, target_file_name) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/local_file_manager.rb', line 68

def copy_file source_file_name, target_file_name
  @logger.print "Copying file \"#{source_file_name}\" to \"#{target_file_name}\" from local folder \"#{root_path}\"..."
  full_source_file_name = File.join(root_path, source_file_name)
  full_target_file_name = Pathname(File.join(root_path, target_file_name))
  FileUtils.mkdir_p(full_target_file_name.dirname)
  FileUtils.cp full_source_file_name, full_target_file_name
  @logger.puts 'done.'
rescue Errno::ENOENT
  raise FileNotFoundError.new(full_source_file_name)
end

#delete_file(file_name) ⇒ Object



50
51
52
53
54
55
# File 'lib/local_file_manager.rb', line 50

def delete_file file_name
  @logger.print "Deleting file \"#{file_name}\" from local folder \"#{root_path}\"..."
  full_file_name = File.join(root_path, file_name)
  File.delete(full_file_name) if File.exist?(full_file_name)
  @logger.puts 'done.'
end

#download_to_temp_file(file_name) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/local_file_manager.rb', line 26

def download_to_temp_file(file_name)
  @logger.print "Copying local file \"#{file_name}\" to folder \"#{root_path}\"..."
  full_file_name = Pathname(File.join(root_path, file_name))
  Dir.mktmpdir do |dir|
    temp_file = "#{dir}/#{full_file_name.basename}"
    FileUtils.cp(full_file_name, temp_file)
    yield(temp_file)
  end
  @logger.puts 'done.'
rescue Errno::ENOENT
  raise FileNotFoundError.new(full_file_name)
end

#list_files(prefix = '', file_extension = '*') ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/local_file_manager.rb', line 39

def list_files(prefix = '', file_extension = '*')
  FileUtils.mkdir_p(root_path)
  @logger.print "Listing \"#{prefix}*.#{file_extension}\" from local folder \"#{root_path}\"..."
  files = Dir[File.join(root_path, '**', prefix, "*.#{file_extension}"),
              File.join(root_path, "#{prefix}*.#{file_extension}")].map do |f|
    f[root_path.size..-1]
  end.uniq
  @logger.puts 'done.'
  files
end

#read_file(file_name) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/local_file_manager.rb', line 7

def read_file file_name
  @logger.print "Reading file \"#{file_name}\" from local folder \"#{root_path}\"..."
  full_file_name = File.join(root_path, file_name)
  contents = File.open(full_file_name, 'r:UTF-8') { |f| f.read }
  @logger.puts 'done.'

  contents
rescue Errno::ENOENT
  raise FileNotFoundError.new(full_file_name)
end

#rename_file(original_file_name, target_file_name) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/local_file_manager.rb', line 57

def rename_file original_file_name, target_file_name
  @logger.print "Renaming file \"#{original_file_name}\" to \"#{target_file_name}\" from local folder \"#{root_path}\"..."
  full_original_file_name = File.join(root_path, original_file_name)
  full_target_file_name = Pathname(File.join(root_path, target_file_name))
  FileUtils.mkdir_p(full_target_file_name.dirname)
  FileUtils.mv full_original_file_name, full_target_file_name
  @logger.puts 'done.'
rescue Errno::ENOENT
  raise FileNotFoundError.new(full_original_file_name)
end

#save_file(file_name, file_contents, write_options = {}) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/local_file_manager.rb', line 18

def save_file(file_name, file_contents, write_options = {})
  @logger.print "Saving file \"#{file_name}\" to local folder \"#{root_path}\"..."
  full_file_name = Pathname(File.join(root_path, file_name))
  FileUtils.mkdir_p(full_file_name.dirname)
  File.open(full_file_name, 'wb') { |f| f.write(file_contents) }
  @logger.puts 'done.'
end