1 # frozen_string_literal: true
 
   4   class TracesController < ApiController
 
   5     before_action :check_api_writable, :only => [:create, :update, :destroy]
 
   6     before_action :set_request_formats, :only => [:show]
 
   7     before_action :set_locale
 
   8     before_action :authorize
 
  12     before_action :offline_error, :only => [:create, :destroy]
 
  13     skip_around_action :api_call_timeout, :only => :create
 
  16       @trace = Trace.visible.find(params[:id])
 
  18       return head :forbidden unless @trace.public? || @trace.user == current_user
 
  20       respond_to do |format|
 
  27       tags = params[:tags] || ""
 
  28       description = params[:description] || ""
 
  29       visibility = params[:visibility]
 
  32         visibility = if params[:public]&.to_i&.nonzero?
 
  39       if params[:file].respond_to?(:read)
 
  40         trace = do_create(params[:file], tags, description, visibility)
 
  44           render :plain => trace.id.to_s
 
  46           head :internal_server_error
 
  56       trace = Trace.visible.find(params[:id])
 
  58       if trace.user == current_user
 
  59         trace.update_from_xml(request.raw_post)
 
  69       trace = Trace.visible.find(params[:id])
 
  71       if trace.user == current_user
 
  74         trace.schedule_destruction
 
  84     def do_create(file, tags, description, visibility)
 
  85       # Sanitise the user's filename
 
  86       name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
 
  88       # Create the trace object, falsely marked as already
 
  89       # inserted to stop the import daemon trying to load it
 
  93         :description => description,
 
  94         :visibility => visibility,
 
  96         :user => current_user,
 
  97         :timestamp => Time.now.utc,
 
 101       # Save the trace object
 
 104       # Finally save the user's preferred privacy level
 
 105       if pref = current_user.preferences.find_by(:k => "gps.trace.visibility")
 
 109         current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
 
 116       report_error "GPX files offline for maintenance", :service_unavailable if Settings.status == "gpx_offline"