From bce2c88336880c96b24b5ddd9e835528da27ddea Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Tue, 17 Jul 2007 14:34:22 +0000 Subject: [PATCH] Provide a more sensible suggested filename when downloading traces. --- app/controllers/trace_controller.rb | 2 +- app/models/trace.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/app/controllers/trace_controller.rb b/app/controllers/trace_controller.rb index 5b1702948..288a4a67b 100644 --- a/app/controllers/trace_controller.rb +++ b/app/controllers/trace_controller.rb @@ -118,7 +118,7 @@ class TraceController < ApplicationController def data trace = Trace.find(params[:id]) if trace and (trace.public? or (@user and @user == trace.user)) - send_file(trace.trace_name, :filename => "#{trace.id}.gpx", :type => trace.mime_type, :disposition => 'attachment') + send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => 'attachment') else render :nothing, :status => :not_found end diff --git a/app/models/trace.rb b/app/models/trace.rb index 92cc39678..7097e0973 100644 --- a/app/models/trace.rb +++ b/app/models/trace.rb @@ -76,6 +76,32 @@ class Trace < ActiveRecord::Base return `file -bi #{trace_name}`.chomp end + def extension_name + filetype = `file -bz #{trace_name}`.chomp + gzipped = filetype =~ /gzip compressed/ + bzipped = filetype =~ /bzip2 compressed/ + zipped = filetype =~ /Zip archive/ + tarred = filetype =~ /tar archive/ + + if tarred and gzipped then + extension = ".tar.gz" + elsif tarred and bzipped then + extension = ".tar.bz2" + elsif tarred + extension = ".tar" + elsif gzipped + extension = ".gpx.gz" + elsif bzipped + extension = ".gpx.bz2" + elsif zipped + extension = ".zip" + else + extension = ".gpx" + end + + return extension + end + def to_xml doc = OSM::API.new.get_xml_doc doc.root << to_xml_node() -- 2.39.5