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 trace = Trace.visible.find(params[:id])
25 if trace.user == current_user
26 trace.update_from_xml(request.raw_post)
36 trace = Trace.visible.find(params[:id])
38 if trace.user == current_user
41 TraceDestroyerJob.perform_later(trace)
50 trace = Trace.visible.find(params[:id])
52 if trace.public? || trace.user == current_user
53 if request.format == Mime[:xml]
54 send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
55 elsif request.format == Mime[:gpx]
56 send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
57 elsif trace.file.attached?
58 redirect_to rails_blob_path(trace.file, :disposition => "attachment")
60 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
68 tags = params[:tags] || ""
69 description = params[:description] || ""
70 visibility = params[:visibility]
73 visibility = if params[:public]&.to_i&.nonzero?
80 if params[:file].respond_to?(:read)
81 trace = do_create(params[:file], tags, description, visibility)
84 TraceImporterJob.perform_later(trace)
85 render :plain => trace.id.to_s
87 head :internal_server_error
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"