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]
10 skip_around_action :api_call_timeout, :only => :create
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
76 def do_create(file, tags, description, visibility)
77 # Sanitise the user's filename
78 name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
80 # Create the trace object, falsely marked as already
81 # inserted to stop the import daemon trying to load it
85 :description => description,
86 :visibility => visibility,
88 :user => current_user,
89 :timestamp => Time.now.utc,
93 # Save the trace object
96 # Finally save the user's preferred privacy level
97 if pref = current_user.preferences.find_by(:k => "gps.trace.visibility")
101 current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
108 report_error "GPX files offline for maintenance", :service_unavailable if Settings.status == "gpx_offline"