Class: Aspera::TempFileManager

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/aspera/temp_file_manager.rb

Overview

create a temp file name for a given folder files can be deleted on process exit by calling cleanup

Instance Method Summary collapse

Constructor Details

#initializeTempFileManager



10
11
12
# File 'lib/aspera/temp_file_manager.rb', line 10

def initialize
  @created_files=[]
end

Instance Method Details

#cleanupObject

call this on process exit



15
16
17
18
19
20
# File 'lib/aspera/temp_file_manager.rb', line 15

def cleanup
  @created_files.each do |filepath|
    File.delete(filepath) if File.file?(filepath)
  end
  @created_files=[]
end

#new_file_path_global(base_name) ⇒ Object

same as above but in global temp folder



32
33
34
35
# File 'lib/aspera/temp_file_manager.rb', line 32

def new_file_path_global(base_name)
  username = Etc.getlogin || Etc.getpwuid(Process.uid).name || 'unknown_user' rescue 'unknown_user'
  return new_file_path_in_folder(Etc.systmpdir,base_name+'_'+username+'_')
end

#new_file_path_in_folder(temp_folder, add_base = '') ⇒ Object

ensure that provided folder exists, or create it, generate a unique filename



24
25
26
27
28
29
# File 'lib/aspera/temp_file_manager.rb', line 24

def new_file_path_in_folder(temp_folder,add_base='')
  FileUtils.mkdir_p(temp_folder) unless Dir.exist?(temp_folder)
  new_file=File.join(temp_folder,add_base+SecureRandom.uuid)
  @created_files.push(new_file)
  return new_file
end