]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/traces_controller.rb
Merge remote-tracking branch 'upstream/pull/7618'
[rails.git] / app / controllers / api / traces_controller.rb
1 # frozen_string_literal: true
2
3 module Api
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
9
10     authorize_resource
11
12     before_action :offline_error, :only => [:create, :destroy]
13     skip_around_action :api_call_timeout, :only => :create
14
15     def show
16       @trace = Trace.visible.find(params.expect(:id))
17
18       return head :forbidden unless @trace.public? || @trace.user == current_user
19
20       respond_to do |format|
21         format.xml
22         format.json
23       end
24     end
25
26     def create
27       tags = params[:tags] || ""
28       description = params[:description] || ""
29       visibility = params[:visibility]
30
31       if params.expect(:file).respond_to?(:read)
32         trace = do_create(params[:file], tags, description, visibility)
33
34         if trace.id
35           trace.schedule_import
36           render :plain => trace.id.to_s
37         elsif trace.valid?
38           head :internal_server_error
39         else
40           head :bad_request
41         end
42       else
43         head :bad_request
44       end
45     end
46
47     def update
48       trace = Trace.visible.find(params.expect(:id))
49
50       if trace.user == current_user
51         Trace.transaction do
52           trace.update_from_xml(request.raw_post)
53           trace.save!
54         end
55
56         head :ok
57       else
58         head :forbidden
59       end
60     end
61
62     def destroy
63       trace = Trace.visible.find(params.expect(:id))
64
65       if trace.user == current_user
66         trace.visible = false
67         trace.save!
68         trace.schedule_destruction
69
70         head :ok
71       else
72         head :forbidden
73       end
74     end
75
76     private
77
78     def do_create(file, tags, description, visibility)
79       # Sanitise the user's filename
80       name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
81
82       # Create the trace object, falsely marked as already
83       # inserted to stop the import daemon trying to load it
84       trace = Trace.new(
85         :name => name,
86         :tagstring => tags,
87         :description => description,
88         :visibility => visibility,
89         :inserted => false,
90         :user => current_user,
91         :timestamp => Time.now.utc,
92         :file => file
93       )
94
95       # Save the trace object
96       trace.save!
97
98       # Finally save the user's preferred privacy level
99       if pref = current_user.preferences.find_by(:k => "gps.trace.visibility")
100         pref.v = visibility
101         pref.save
102       else
103         current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
104       end
105
106       trace
107     end
108
109     def offline_error
110       report_error "GPX files offline for maintenance", :service_unavailable if Settings.status == "gpx_offline"
111     end
112   end
113 end