Module: Dspace::Builders::TempfileBuilder

Defined in:
lib/dspace/builders/tempfile_builder.rb

Class Method Summary collapse

Class Method Details

.build(filename, contents = nil, bitstreams_path = '/tmp') ⇒ Object



5
6
7
8
9
10
# File 'lib/dspace/builders/tempfile_builder.rb', line 5

def self.build(filename, contents = nil, bitstreams_path = '/tmp')
  Tempfile.new([sanitize_filename(filename), File.extname(filename)], bitstreams_path, encoding: 'ascii-8bit').tap do |f|
    f.write contents
    f.close
  end
end

.sanitize_filename(filename) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/dspace/builders/tempfile_builder.rb', line 12

def self.sanitize_filename(filename)
  filename.strip!
  # NOTE: File.basename doesn't work right with Windows paths on Unix
  # get only the filename, not the whole path
  filename.gsub!(/^.*(\\|\/)/, '')
  # Strip out the non-ascii character
  filename.gsub!(/[^0-9A-Za-z.\-]/, '_')
end