]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/traces_controller.rb
Bump the dependencies group with 6 updates
[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.update_from_xml(request.raw_post)
52         trace.save!
53
54         head :ok
55       else
56         head :forbidden
57       end
58     end
59
60     def destroy
61       trace = Trace.visible.find(params.expect(:id))
62
63       if trace.user == current_user
64         trace.visible = false
65         trace.save!
66         trace.schedule_destruction
67
68         head :ok
69       else
70         head :forbidden
71       end
72     end
73
74     private
75
76     def do_create(file, tags, description, visibility)
77       # Sanitise the user's filename
78       name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
79
80       # Create the trace object, falsely marked as already
81       # inserted to stop the import daemon trying to load it
82       trace = Trace.new(
83         :name => name,
84         :tagstring => tags,
85         :description => description,
86         :visibility => visibility,
87         :inserted => false,
88         :user => current_user,
89         :timestamp => Time.now.utc,
90         :file => file
91       )
92
93       # Save the trace object
94       trace.save!
95
96       # Finally save the user's preferred privacy level
97       if pref = current_user.preferences.find_by(:k => "gps.trace.visibility")
98         pref.v = visibility
99         pref.save
100       else
101         current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
102       end
103
104       trace
105     end
106
107     def offline_error
108       report_error "GPX files offline for maintenance", :service_unavailable if Settings.status == "gpx_offline"
109     end
110   end
111 end