2 class TracesController < ApiController
3 layout "site", :except => :georss
5 before_action :authorize_web
6 before_action :set_locale
7 before_action :authorize
8 before_action :api_deny_access_handler
12 before_action :check_database_readable, :except => [:api_read, :api_data]
13 before_action :check_database_writable, :only => [:api_create, :api_update, :api_delete]
14 before_action :check_api_readable, :only => [:api_read, :api_data]
15 before_action :check_api_writable, :only => [:api_create, :api_update, :api_delete]
16 before_action :offline_redirect, :only => [:api_create, :api_delete, :api_data]
17 around_action :api_call_handle_error
20 trace = Trace.visible.find(params[:id])
22 if trace.public? || trace.user == current_user
23 render :xml => trace.to_xml.to_s
30 trace = Trace.visible.find(params[:id])
32 if trace.user == current_user
33 trace.update_from_xml(request.raw_post)
43 trace = Trace.visible.find(params[:id])
45 if trace.user == current_user
56 trace = Trace.visible.find(params[:id])
58 if trace.public? || trace.user == current_user
59 if request.format == Mime[:xml]
60 send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
61 elsif request.format == Mime[:gpx]
62 send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
64 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
72 tags = params[:tags] || ""
73 description = params[:description] || ""
74 visibility = params[:visibility]
77 visibility = if params[:public]&.to_i&.nonzero?
84 if params[:file].respond_to?(:read)
85 trace = do_create(params[:file], tags, description, visibility)
88 render :plain => trace.id.to_s
90 head :internal_server_error
101 def do_create(file, tags, description, visibility)
102 # Sanitise the user's filename
103 name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
105 # Get a temporary filename...
106 filename = "/tmp/#{rand}"
108 # ...and save the uploaded file to that location
109 File.open(filename, "wb") { |f| f.write(file.read) }
111 # Create the trace object, falsely marked as already
112 # inserted to stop the import daemon trying to load it
116 :description => description,
117 :visibility => visibility,
119 :user => current_user,
120 :timestamp => Time.now.getutc
126 # Save the trace object
129 # Rename the temporary file to the final name
130 FileUtils.mv(filename, trace.trace_name)
132 # Remove the file as we have failed to update the database
133 FileUtils.rm_f(filename)
135 # Pass the exception on
140 # Clear the inserted flag to make the import daemon load the trace
141 trace.inserted = false
144 # Remove the file as we have failed to update the database
145 FileUtils.rm_f(trace.trace_name)
147 # Pass the exception on
153 # Finally save the user's preferred privacy level
154 if pref = current_user.preferences.where(:k => "gps.trace.visibility").first
158 current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
165 redirect_to :action => :offline if Settings.status == "gpx_offline"