2   class TracesController < ApiController
 
   3     before_action :check_api_writable, :only => [:create, :update, :destroy]
 
   4     before_action :set_request_formats, :only => [:show]
 
   5     before_action :set_locale
 
   6     before_action :authorize
 
  10     before_action :offline_error, :only => [:create, :destroy]
 
  11     skip_around_action :api_call_timeout, :only => :create
 
  14       @trace = Trace.visible.find(params[:id])
 
  16       return head :forbidden unless @trace.public? || @trace.user == current_user
 
  18       respond_to do |format|
 
  25       tags = params[:tags] || ""
 
  26       description = params[:description] || ""
 
  27       visibility = params[:visibility]
 
  30         visibility = if params[:public]&.to_i&.nonzero?
 
  37       if params[:file].respond_to?(:read)
 
  38         trace = do_create(params[:file], tags, description, visibility)
 
  42           render :plain => trace.id.to_s
 
  44           head :internal_server_error
 
  54       trace = Trace.visible.find(params[:id])
 
  56       if trace.user == current_user
 
  57         trace.update_from_xml(request.raw_post)
 
  67       trace = Trace.visible.find(params[:id])
 
  69       if trace.user == current_user
 
  72         trace.schedule_destruction
 
  82     def do_create(file, tags, description, visibility)
 
  83       # Sanitise the user's filename
 
  84       name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
 
  86       # Create the trace object, falsely marked as already
 
  87       # inserted to stop the import daemon trying to load it
 
  91         :description => description,
 
  92         :visibility => visibility,
 
  94         :user => current_user,
 
  95         :timestamp => Time.now.utc,
 
  99       # Save the trace object
 
 102       # Finally save the user's preferred privacy level
 
 103       if pref = current_user.preferences.find_by(:k => "gps.trace.visibility")
 
 107         current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
 
 114       report_error "GPX files offline for maintenance", :service_unavailable if Settings.status == "gpx_offline"