From 6a85955642606aa3159ea8e4d24fbc77a1fc5e94 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 25 Apr 2007 17:25:44 +0000 Subject: Added parsing of file type in Hash.xml_in so you can easily do file uploads with base64 from an API [DHH] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6578 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../active_support/core_ext/hash/conversions.rb | 43 ++++++++++++++++------ 1 file changed, 31 insertions(+), 12 deletions(-) (limited to 'activesupport/lib/active_support/core_ext') diff --git a/activesupport/lib/active_support/core_ext/hash/conversions.rb b/activesupport/lib/active_support/core_ext/hash/conversions.rb index 4fb3cbe4d3..a8f2ec4452 100644 --- a/activesupport/lib/active_support/core_ext/hash/conversions.rb +++ b/activesupport/lib/active_support/core_ext/hash/conversions.rb @@ -45,15 +45,22 @@ module ActiveSupport #:nodoc: unless defined?(XML_PARSING) XML_PARSING = { - "date" => Proc.new { |date| ::Date.parse(date) }, - "datetime" => Proc.new { |time| ::Time.parse(time).utc }, - "integer" => Proc.new { |integer| integer.to_i }, - "float" => Proc.new { |float| float.to_f }, - "decimal" => Proc.new { |number| BigDecimal(number) }, - "boolean" => Proc.new { |boolean| %w(1 true).include?(boolean.strip) }, - "string" => Proc.new { |string| string.to_s }, - "yaml" => Proc.new { |yaml| YAML::load(yaml) rescue yaml }, - "base64Binary" => Proc.new { |bin| Base64.decode64(bin) } + "date" => Proc.new { |date| ::Date.parse(date) }, + "datetime" => Proc.new { |time| ::Time.parse(time).utc }, + "integer" => Proc.new { |integer| integer.to_i }, + "float" => Proc.new { |float| float.to_f }, + "decimal" => Proc.new { |number| BigDecimal(number) }, + "boolean" => Proc.new { |boolean| %w(1 true).include?(boolean.strip) }, + "string" => Proc.new { |string| string.to_s }, + "yaml" => Proc.new { |yaml| YAML::load(yaml) rescue yaml }, + "base64Binary" => Proc.new { |bin| Base64.decode64(bin) }, + # FIXME: Get rid of eval and institute a proper decorator here + "file" => Proc.new do |file, entity| + f = StringIO.new(Base64.decode64(file)) + eval "def f.original_filename() '#{entity["name"]}' || 'untitled' end" + eval "def f.content_type() '#{entity["content_type"]}' || 'application/octet-stream' end" + f + end } XML_PARSING.update( @@ -147,18 +154,30 @@ module ActiveSupport #:nodoc: when "Hash" if value.has_key?("__content__") content = translate_xml_entities(value["__content__"]) - if XML_PARSING[value["type"]] - XML_PARSING[value["type"]].call(content) + if parser = XML_PARSING[value["type"]] + if parser.arity == 2 + XML_PARSING[value["type"]].call(content, value) + else + XML_PARSING[value["type"]].call(content) + end else content end elsif value['type'] == 'string' && value['nil'] != 'true' "" else - (value.blank? || value['type'] || value['nil'] == 'true') ? nil : value.inject({}) do |h,(k,v)| + xml_value = (value.blank? || value['type'] || value['nil'] == 'true') ? nil : value.inject({}) do |h,(k,v)| h[k] = typecast_xml_value(v) h end + + # Turn { :files => { :file => # } into { :files => # } so it is compatible with + # how multipart uploaded files from HTML appear + if xml_value["file"].is_a?(StringIO) + xml_value["file"] + else + xml_value + end end when "Array" value.map! { |i| typecast_xml_value(i) } -- cgit v1.2.3