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 :authorize_web
6 before_action :set_locale
7 before_action :authorize
11 before_action :check_api_readable, :only => [:show, :data]
12 before_action :check_api_writable, :only => [:create, :update, :destroy]
13 before_action :offline_error, :only => [:create, :destroy, :data]
14 around_action :api_call_handle_error
17 @trace = Trace.visible.find(params[:id])
19 head :forbidden unless @trace.public? || @trace.user == current_user
23 tags = params[:tags] || ""
24 description = params[:description] || ""
25 visibility = params[:visibility]
28 visibility = if params[:public]&.to_i&.nonzero?
35 if params[:file].respond_to?(:read)
36 trace = do_create(params[:file], tags, description, visibility)
39 TraceImporterJob.perform_later(trace)
40 render :plain => trace.id.to_s
42 head :internal_server_error
52 trace = Trace.visible.find(params[:id])
54 if trace.user == current_user
55 trace.update_from_xml(request.raw_post)
65 trace = Trace.visible.find(params[:id])
67 if trace.user == current_user
70 TraceDestroyerJob.perform_later(trace)
79 trace = Trace.visible.find(params[:id])
81 if trace.public? || trace.user == current_user
82 if request.format == Mime[:xml]
83 send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
84 elsif request.format == Mime[:gpx]
85 send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
86 elsif trace.file.attached?
87 redirect_to rails_blob_path(trace.file, :disposition => "attachment")
89 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
98 def do_create(file, tags, description, visibility)
99 # Sanitise the user's filename
100 name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
102 # Create the trace object, falsely marked as already
103 # inserted to stop the import daemon trying to load it
107 :description => description,
108 :visibility => visibility,
110 :user => current_user,
111 :timestamp => Time.now.utc,
115 # Save the trace object
118 # Finally save the user's preferred privacy level
119 if pref = current_user.preferences.where(:k => "gps.trace.visibility").first
123 current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
130 report_error "GPX files offline for maintenance", :service_unavailable if Settings.status == "gpx_offline"