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_redirect, :only => [:create, :destroy, :data]
16 around_action :api_call_handle_error
19 trace = Trace.visible.find(params[:id])
21 if trace.public? || trace.user == current_user
30 trace = Trace.visible.find(params[:id])
32 if trace.user == current_user
33 trace.update_from_xml(request.raw_post)
43 trace = Trace.visible.find(params[:id])
45 if trace.user == current_user
48 TraceDestroyerJob.perform_later(trace) if Settings.trace_use_job_queue
57 trace = Trace.visible.find(params[:id])
59 if trace.public? || trace.user == current_user
60 if request.format == Mime[:xml]
61 send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
62 elsif request.format == Mime[:gpx]
63 send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
65 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
73 tags = params[:tags] || ""
74 description = params[:description] || ""
75 visibility = params[:visibility]
78 visibility = if params[:public]&.to_i&.nonzero?
85 if params[:file].respond_to?(:read)
86 trace = do_create(params[:file], tags, description, visibility)
89 TraceImporterJob.perform_later(trace) if Settings.trace_use_job_queue
90 render :plain => trace.id.to_s
92 head :internal_server_error
103 def do_create(file, tags, description, visibility)
104 # Sanitise the user's filename
105 name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
107 # Get a temporary filename...
108 filename = "/tmp/#{rand}"
110 # ...and save the uploaded file to that location
111 File.open(filename, "wb") { |f| f.write(file.read) }
113 # Create the trace object, falsely marked as already
114 # inserted to stop the import daemon trying to load it
118 :description => description,
119 :visibility => visibility,
121 :user => current_user,
122 :timestamp => Time.now.getutc
128 # Save the trace object
131 # Rename the temporary file to the final name
132 FileUtils.mv(filename, trace.trace_name)
134 # Remove the file as we have failed to update the database
135 FileUtils.rm_f(filename)
137 # Pass the exception on
142 # Clear the inserted flag to make the import daemon load the trace
143 trace.inserted = false
146 # Remove the file as we have failed to update the database
147 FileUtils.rm_f(trace.trace_name)
149 # Pass the exception on
155 # Finally save the user's preferred privacy level
156 if pref = current_user.preferences.where(:k => "gps.trace.visibility").first
160 current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
167 redirect_to :action => :offline if Settings.status == "gpx_offline"