1 # frozen_string_literal: true
4 class TracesController < ApiController
5 before_action :check_api_writable, :only => [:create, :update, :destroy]
6 before_action :set_request_formats, :only => [:show]
7 before_action :set_locale
8 before_action :authorize
12 before_action :offline_error, :only => [:create, :destroy]
13 skip_around_action :api_call_timeout, :only => :create
16 @trace = Trace.visible.find(params.expect(:id))
18 return head :forbidden unless @trace.public? || @trace.user == current_user
20 respond_to do |format|
27 tags = params[:tags] || ""
28 description = params[:description] || ""
29 visibility = params[:visibility]
31 if params.expect(:file).respond_to?(:read)
32 trace = do_create(params[:file], tags, description, visibility)
36 render :plain => trace.id.to_s
38 head :internal_server_error
48 trace = Trace.visible.find(params.expect(:id))
50 if trace.user == current_user
51 trace.update_from_xml(request.raw_post)
61 trace = Trace.visible.find(params.expect(:id))
63 if trace.user == current_user
66 trace.schedule_destruction
76 def do_create(file, tags, description, visibility)
77 # Sanitise the user's filename
78 name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
80 # Create the trace object, falsely marked as already
81 # inserted to stop the import daemon trying to load it
85 :description => description,
86 :visibility => visibility,
88 :user => current_user,
89 :timestamp => Time.now.utc,
93 # Save the trace object
96 # Finally save the user's preferred privacy level
97 if pref = current_user.preferences.find_by(:k => "gps.trace.visibility")
101 current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
108 report_error "GPX files offline for maintenance", :service_unavailable if Settings.status == "gpx_offline"