From: Tom Hughes Date: Thu, 6 Mar 2008 12:43:10 +0000 (+0000) Subject: Work round ruby's half assed Tempfile class which, because for some X-Git-Tag: live~7848 X-Git-Url: https://git.openstreetmap.org/rails.git/commitdiff_plain/c0d2ad40c30e5a0837b6012d7b9067d69ce41dd0?hp=58082d128a9eef5037c5b46e17ff59715a9bd6e8 Work round ruby's half assed Tempfile class which, because for some reason it is implemented by delegating to File (by inheriting from DelegateClass(File)) rather than by inheriting from it, does not actually look like a File, and hence does not look like an IO (in that it does not respond to kind_of? IO calls). In ruby 1.8.5 this doesn't matter as REXML checks that the source it has been given responds to the methods it wants (using respond_to? which is handled properly by DelegateClass) but in 1.8.4 which we are running on rails2 REXML uses kind_of? to see if the source is one it can use. --- diff --git a/app/models/trace.rb b/app/models/trace.rb index fef9983dd..ab99dc290 100644 --- a/app/models/trace.rb +++ b/app/models/trace.rb @@ -145,23 +145,25 @@ class Trace < ActiveRecord::Base tarred = filetype =~ /tar archive/ if gzipped or bzipped or zipped or tarred then - file = Tempfile.new("trace.#{id}"); + tmpfile = Tempfile.new("trace.#{id}"); if tarred and gzipped then - system("tar -zxOf #{trace_name} > #{file.path}") + system("tar -zxOf #{trace_name} > #{tmpfile.path}") elsif tarred and bzipped then - system("tar -jxOf #{trace_name} > #{file.path}") + system("tar -jxOf #{trace_name} > #{tmpfile.path}") elsif tarred - system("tar -xOf #{trace_name} > #{file.path}") + system("tar -xOf #{trace_name} > #{tmpfile.path}") elsif gzipped - system("gunzip -c #{trace_name} > #{file.path}") + system("gunzip -c #{trace_name} > #{tmpfile.path}") elsif bzipped - system("bunzip2 -c #{trace_name} > #{file.path}") + system("bunzip2 -c #{trace_name} > #{tmpfile.path}") elsif zipped - system("unzip -p #{trace_name} > #{file.path}") + system("unzip -p #{trace_name} > #{tmpfile.path}") end - file.unlink + tmpfile.unlink + + file = tmpfile.file else file = File.open(trace_name) end diff --git a/config/initializers/tempfile.rb b/config/initializers/tempfile.rb new file mode 100644 index 000000000..11416b086 --- /dev/null +++ b/config/initializers/tempfile.rb @@ -0,0 +1,7 @@ +# Hack TempFile to let us get at the underlying File object as ruby +# does a half assed job of making TempFile act as a File +class Tempfile + def file + return @tmpfile + end +end