Class: SWFRecompress::Tempfile

Inherits:
Object
  • Object
show all
Defined in:
lib/swf_recompress.rb

Class Method Summary collapse

Class Method Details

.open(temp_stem, write_mode = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/swf_recompress.rb', line 22

def self.open(temp_stem, write_mode = nil)
  begin
    instance_tmp_dir = nil
    begin
      instance_tmp_dir = File.join(TMP_DIR, rand(1_000_000).to_s)
    end until !File.exists?(instance_tmp_dir)
    FileUtils.mkdir_p(instance_tmp_dir)
    ext           = File.extname(temp_stem)
    temp_filename = Pathname.new(
        File.expand_path(File.join(instance_tmp_dir, '%s%s' % [ File.basename(temp_stem, ext), ext ]))
      ).relative_path_from(Pathname.new(Dir.pwd))
    File.open(temp_filename, write_mode || 'w') do |f|
      yield(f)
    end
  rescue => e
    raise "Error during Tempfile open #{e.message}"
  ensure
    FileUtils.rm(temp_filename)
    FileUtils.rm_rf(instance_tmp_dir)
  end
end