From 7e7cd1b28a5d50433afd308f821a0459bf686755 Mon Sep 17 00:00:00 2001 From: Maurizio De Magnis Date: Fri, 22 Aug 2014 11:21:01 +0200 Subject: Preserves extension in the created tempfile This is handy when you have to pass the generated tempfile to gems that relies on the detection of filename extension ([simple-spreadsheet](https://github.com/zenkay/simple-spreadsheet), [roo](https://github.com/Empact/roo) to name my use case). --- lib/rack/multipart/parser.rb | 3 ++- test/spec_multipart.rb | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/rack/multipart/parser.rb b/lib/rack/multipart/parser.rb index 22f9734b..0efe0939 100644 --- a/lib/rack/multipart/parser.rb +++ b/lib/rack/multipart/parser.rb @@ -117,7 +117,8 @@ module Rack end if filename - (@env['rack.tempfiles'] ||= []) << body = Tempfile.new("RackMultipart") + extname = ::File.extname(filename) + (@env['rack.tempfiles'] ||= []) << body = Tempfile.new(["RackMultipart", extname]) body.binmode if body.respond_to?(:binmode) end diff --git a/test/spec_multipart.rb b/test/spec_multipart.rb index 2acb6e0d..01e0d814 100644 --- a/test/spec_multipart.rb +++ b/test/spec_multipart.rb @@ -153,6 +153,12 @@ describe Rack::Multipart do params["files"][:tempfile].read.should.equal "contents" end + should "preserve extension in the created tempfile" do + env = Rack::MockRequest.env_for("/", multipart_fixture(:text)) + params = Rack::Multipart.parse_multipart(env) + File.extname(params["files"][:tempfile].path).should.equal ".txt" + end + should "parse multipart upload with text file with no name field" do env = Rack::MockRequest.env_for("/", multipart_fixture(:filename_and_no_name)) params = Rack::Multipart.parse_multipart(env) -- cgit v1.2.3-24-ge0c7