2 class TracesController < ApiController
3 layout "site", :except => :georss
5 before_action :authorize_web
6 before_action :set_locale
7 before_action :authorize
11 before_action :check_database_readable, :except => [:show, :data]
12 before_action :check_database_writable, :only => [:create, :update, :destroy]
13 before_action :check_api_readable, :only => [:show, :data]
14 before_action :check_api_writable, :only => [:create, :update, :destroy]
15 before_action :offline_error, :only => [:create, :destroy, :data]
16 around_action :api_call_handle_error
19 @trace = Trace.visible.find(params[:id])
21 head :forbidden unless @trace.public? || @trace.user == current_user
25 trace = Trace.visible.find(params[:id])
27 if trace.user == current_user
28 trace.update_from_xml(request.raw_post)
38 trace = Trace.visible.find(params[:id])
40 if trace.user == current_user
43 TraceDestroyerJob.perform_later(trace) if Settings.trace_use_job_queue
52 trace = Trace.visible.find(params[:id])
54 if trace.public? || trace.user == current_user
55 if request.format == Mime[:xml]
56 send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
57 elsif request.format == Mime[:gpx]
58 send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :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) if Settings.trace_use_job_queue
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 # Get a temporary filename...
103 filename = "/tmp/#{rand}"
105 # ...and save the uploaded file to that location
106 File.open(filename, "wb") { |f| f.write(file.read) }
108 # Create the trace object, falsely marked as already
109 # inserted to stop the import daemon trying to load it
113 :description => description,
114 :visibility => visibility,
116 :user => current_user,
117 :timestamp => Time.now.getutc
123 # Save the trace object
126 # Rename the temporary file to the final name
127 FileUtils.mv(filename, trace.trace_name)
129 # Remove the file as we have failed to update the database
130 FileUtils.rm_f(filename)
132 # Pass the exception on
137 # Clear the inserted flag to make the import daemon load the trace
138 trace.inserted = false
141 # Remove the file as we have failed to update the database
142 FileUtils.rm_f(trace.trace_name)
144 # Pass the exception on
150 # Finally save the user's preferred privacy level
151 if pref = current_user.preferences.where(:k => "gps.trace.visibility").first
155 current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
162 report_error "GPX files offline for maintenance", :service_unavailable if Settings.status == "gpx_offline"