2 class TracesController < ApiController
3 before_action :check_api_writable, :only => [:create, :update, :destroy]
4 before_action :set_locale
5 before_action :authorize
9 before_action :offline_error, :only => [:create, :destroy, :data]
10 around_action :api_call_handle_error
13 @trace = Trace.visible.find(params[:id])
15 head :forbidden unless @trace.public? || @trace.user == current_user
19 tags = params[:tags] || ""
20 description = params[:description] || ""
21 visibility = params[:visibility]
24 visibility = if params[:public]&.to_i&.nonzero?
31 if params[:file].respond_to?(:read)
32 trace = do_create(params[:file], tags, description, visibility)
36 render :plain => trace.id.to_s
38 head :internal_server_error
48 trace = Trace.visible.find(params[:id])
50 if trace.user == current_user
51 trace.update_from_xml(request.raw_post)
61 trace = Trace.visible.find(params[:id])
63 if trace.user == current_user
66 trace.schedule_destruction
75 trace = Trace.visible.find(params[:id])
77 if trace.public? || trace.user == current_user
78 if request.format == Mime[:xml]
79 send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
80 elsif request.format == Mime[:gpx]
81 send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
82 elsif trace.file.attached?
83 redirect_to rails_blob_path(trace.file, :disposition => "attachment")
85 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
94 def do_create(file, tags, description, visibility)
95 # Sanitise the user's filename
96 name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
98 # Create the trace object, falsely marked as already
99 # inserted to stop the import daemon trying to load it
103 :description => description,
104 :visibility => visibility,
106 :user => current_user,
107 :timestamp => Time.now.utc,
111 # Save the trace object
114 # Finally save the user's preferred privacy level
115 if pref = current_user.preferences.find_by(:k => "gps.trace.visibility")
119 current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
126 report_error "GPX files offline for maintenance", :service_unavailable if Settings.status == "gpx_offline"