2 class TracesController < ApiController
3 layout "site", :except => :georss
5 before_action :authorize_web
6 before_action :set_locale
7 before_action :authorize
11 before_action :check_database_readable, :except => [:api_read, :api_data]
12 before_action :check_database_writable, :only => [:api_create, :api_update, :api_delete]
13 before_action :check_api_readable, :only => [:api_read, :api_data]
14 before_action :check_api_writable, :only => [:api_create, :api_update, :api_delete]
15 before_action :offline_redirect, :only => [:api_create, :api_delete, :api_data]
16 around_action :api_call_handle_error
19 trace = Trace.visible.find(params[:id])
21 if trace.public? || trace.user == current_user
22 render :xml => trace.to_xml.to_s
29 trace = Trace.visible.find(params[:id])
31 if trace.user == current_user
32 trace.update_from_xml(request.raw_post)
42 trace = Trace.visible.find(params[:id])
44 if trace.user == current_user
55 trace = Trace.visible.find(params[:id])
57 if trace.public? || trace.user == current_user
58 if request.format == Mime[:xml]
59 send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
60 elsif request.format == Mime[:gpx]
61 send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
63 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
71 tags = params[:tags] || ""
72 description = params[:description] || ""
73 visibility = params[:visibility]
76 visibility = if params[:public]&.to_i&.nonzero?
83 if params[:file].respond_to?(:read)
84 trace = do_create(params[:file], tags, description, visibility)
87 render :plain => trace.id.to_s
89 head :internal_server_error
100 def do_create(file, tags, description, visibility)
101 # Sanitise the user's filename
102 name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
104 # Get a temporary filename...
105 filename = "/tmp/#{rand}"
107 # ...and save the uploaded file to that location
108 File.open(filename, "wb") { |f| f.write(file.read) }
110 # Create the trace object, falsely marked as already
111 # inserted to stop the import daemon trying to load it
115 :description => description,
116 :visibility => visibility,
118 :user => current_user,
119 :timestamp => Time.now.getutc
125 # Save the trace object
128 # Rename the temporary file to the final name
129 FileUtils.mv(filename, trace.trace_name)
131 # Remove the file as we have failed to update the database
132 FileUtils.rm_f(filename)
134 # Pass the exception on
139 # Clear the inserted flag to make the import daemon load the trace
140 trace.inserted = false
143 # Remove the file as we have failed to update the database
144 FileUtils.rm_f(trace.trace_name)
146 # Pass the exception on
152 # Finally save the user's preferred privacy level
153 if pref = current_user.preferences.where(:k => "gps.trace.visibility").first
157 current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
164 redirect_to :action => :offline if Settings.status == "gpx_offline"