2   class TracesController < ApiController
 
   3     before_action :check_database_readable, :except => [:show, :data]
 
   4     before_action :check_database_writable, :only => [:create, :update, :destroy]
 
   5     before_action :set_locale
 
   6     before_action :authorize
 
  10     before_action :check_api_readable, :only => [:show, :data]
 
  11     before_action :check_api_writable, :only => [:create, :update, :destroy]
 
  12     before_action :offline_error, :only => [:create, :destroy, :data]
 
  13     around_action :api_call_handle_error
 
  16       @trace = Trace.visible.find(params[:id])
 
  18       head :forbidden unless @trace.public? || @trace.user == current_user
 
  22       tags = params[:tags] || ""
 
  23       description = params[:description] || ""
 
  24       visibility = params[:visibility]
 
  27         visibility = if params[:public]&.to_i&.nonzero?
 
  34       if params[:file].respond_to?(:read)
 
  35         trace = do_create(params[:file], tags, description, visibility)
 
  38           TraceImporterJob.perform_later(trace)
 
  39           render :plain => trace.id.to_s
 
  41           head :internal_server_error
 
  51       trace = Trace.visible.find(params[:id])
 
  53       if trace.user == current_user
 
  54         trace.update_from_xml(request.raw_post)
 
  64       trace = Trace.visible.find(params[:id])
 
  66       if trace.user == current_user
 
  69         TraceDestroyerJob.perform_later(trace)
 
  78       trace = Trace.visible.find(params[:id])
 
  80       if trace.public? || trace.user == current_user
 
  81         if request.format == Mime[:xml]
 
  82           send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
 
  83         elsif request.format == Mime[:gpx]
 
  84           send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
 
  85         elsif trace.file.attached?
 
  86           redirect_to rails_blob_path(trace.file, :disposition => "attachment")
 
  88           send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
 
  97     def do_create(file, tags, description, visibility)
 
  98       # Sanitise the user's filename
 
  99       name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
 
 101       # Create the trace object, falsely marked as already
 
 102       # inserted to stop the import daemon trying to load it
 
 106         :description => description,
 
 107         :visibility => visibility,
 
 109         :user => current_user,
 
 110         :timestamp => Time.now.utc,
 
 114       # Save the trace object
 
 117       # Finally save the user's preferred privacy level
 
 118       if pref = current_user.preferences.find_by(:k => "gps.trace.visibility")
 
 122         current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
 
 129       report_error "GPX files offline for maintenance", :service_unavailable if Settings.status == "gpx_offline"