Provide a more sensible suggested filename when downloading traces.

This commit is contained in:
Tom Hughes 2007-07-17 14:34:22 +00:00
parent 76df341f66
commit bce2c88336
2 changed files with 27 additions and 1 deletions

View file

@ -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

View file

@ -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()