2 class TracesController < ApiController
3 before_action :check_api_readable
4 before_action :check_api_writable, :only => [:create, :update, :destroy]
5 before_action :set_locale
6 before_action :authorize
10 before_action :offline_error, :only => [:create, :destroy, :data]
11 around_action :api_call_handle_error
14 @trace = Trace.visible.find(params[:id])
16 head :forbidden unless @trace.public? || @trace.user == current_user
20 tags = params[:tags] || ""
21 description = params[:description] || ""
22 visibility = params[:visibility]
25 visibility = if params[:public]&.to_i&.nonzero?
32 if params[:file].respond_to?(:read)
33 trace = do_create(params[:file], tags, description, visibility)
37 render :plain => trace.id.to_s
39 head :internal_server_error
49 trace = Trace.visible.find(params[:id])
51 if trace.user == current_user
52 trace.update_from_xml(request.raw_post)
62 trace = Trace.visible.find(params[:id])
64 if trace.user == current_user
67 trace.schedule_destruction
76 trace = Trace.visible.find(params[:id])
78 if trace.public? || trace.user == current_user
79 if request.format == Mime[:xml]
80 send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
81 elsif request.format == Mime[:gpx]
82 send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
83 elsif trace.file.attached?
84 redirect_to rails_blob_path(trace.file, :disposition => "attachment")
86 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
95 def do_create(file, tags, description, visibility)
96 # Sanitise the user's filename
97 name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
99 # Create the trace object, falsely marked as already
100 # inserted to stop the import daemon trying to load it
104 :description => description,
105 :visibility => visibility,
107 :user => current_user,
108 :timestamp => Time.now.utc,
112 # Save the trace object
115 # Finally save the user's preferred privacy level
116 if pref = current_user.preferences.find_by(:k => "gps.trace.visibility")
120 current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
127 report_error "GPX files offline for maintenance", :service_unavailable if Settings.status == "gpx_offline"