Method: CampfireExport::IO#export_file

Defined in:
lib/campfire_export.rb

#export_file(content, filename, mode = 'w') ⇒ Object

Requires that room_name and date be defined in the calling object.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/campfire_export.rb', line 62

def export_file(content, filename, mode='w')
  # Check to make sure we're writing into the target directory tree.
  true_path = File.expand_path(File.join(export_dir, filename))
  
  unless true_path.start_with?(File.expand_path(export_dir))
    raise CampfireExport::Exception.new("#{export_dir}/#{filename}",
      "can't export file to a directory higher than target directory; " +
      "expected: #{File.expand_path(export_dir)}, actual: #{true_path}.")
  end
  
  if File.exists?("#{export_dir}/#{filename}")
    log(:error, "#{export_dir}/#{filename} failed: file already exists")
  else
    open("#{export_dir}/#{filename}", mode) do |file|
      file.write content
    end
  end
end