2 class TracesController < ApplicationController
3 layout "site", :except => :georss
5 skip_before_action :verify_authenticity_token
6 before_action :authorize_web
7 before_action :set_locale
8 before_action :authorize
9 before_action :api_deny_access_handler
13 before_action :check_database_readable, :except => [:api_read, :api_data]
14 before_action :check_database_writable, :only => [:api_create, :api_update, :api_delete]
15 before_action :check_api_readable, :only => [:api_read, :api_data]
16 before_action :check_api_writable, :only => [:api_create, :api_update, :api_delete]
17 before_action :offline_redirect, :only => [:api_create, :api_delete, :api_data]
18 around_action :api_call_handle_error
21 trace = Trace.visible.find(params[:id])
23 if trace.public? || trace.user == current_user
24 render :xml => trace.to_xml.to_s
31 trace = Trace.visible.find(params[:id])
33 if trace.user == current_user
34 trace.update_from_xml(request.raw_post)
44 trace = Trace.visible.find(params[:id])
46 if trace.user == current_user
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 render :plain => trace.id.to_s
91 head :internal_server_error
102 def do_create(file, tags, description, visibility)
103 # Sanitise the user's filename
104 name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
106 # Get a temporary filename...
107 filename = "/tmp/#{rand}"
109 # ...and save the uploaded file to that location
110 File.open(filename, "wb") { |f| f.write(file.read) }
112 # Create the trace object, falsely marked as already
113 # inserted to stop the import daemon trying to load it
117 :description => description,
118 :visibility => visibility,
120 :user => current_user,
121 :timestamp => Time.now.getutc
127 # Save the trace object
130 # Rename the temporary file to the final name
131 FileUtils.mv(filename, trace.trace_name)
133 # Remove the file as we have failed to update the database
134 FileUtils.rm_f(filename)
136 # Pass the exception on
141 # Clear the inserted flag to make the import daemon load the trace
142 trace.inserted = false
145 # Remove the file as we have failed to update the database
146 FileUtils.rm_f(trace.trace_name)
148 # Pass the exception on
154 # Finally save the user's preferred privacy level
155 if pref = current_user.preferences.where(:k => "gps.trace.visibility").first
159 current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
166 redirect_to :action => :offline if Settings.status == "gpx_offline"