]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/traces_controller.rb
Merge remote-tracking branch 'upstream/pull/1926'
[rails.git] / app / controllers / api / traces_controller.rb
1 module Api
2   class TracesController < ApiController
3     layout "site", :except => :georss
4
5     before_action :authorize_web
6     before_action :set_locale
7     before_action :authorize
8
9     authorize_resource
10
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
17
18     def show
19       @trace = Trace.visible.find(params[:id])
20
21       head :forbidden unless @trace.public? || @trace.user == current_user
22     end
23
24     def update
25       trace = Trace.visible.find(params[:id])
26
27       if trace.user == current_user
28         trace.update_from_xml(request.raw_post)
29         trace.save!
30
31         head :ok
32       else
33         head :forbidden
34       end
35     end
36
37     def destroy
38       trace = Trace.visible.find(params[:id])
39
40       if trace.user == current_user
41         trace.visible = false
42         trace.save!
43         TraceDestroyerJob.perform_later(trace) if Settings.trace_use_job_queue
44
45         head :ok
46       else
47         head :forbidden
48       end
49     end
50
51     def data
52       trace = Trace.visible.find(params[:id])
53
54       if trace.public? || trace.user == current_user
55         if request.format == Mime[:xml]
56           send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
57         elsif request.format == Mime[:gpx]
58           send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
59         else
60           send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
61         end
62       else
63         head :forbidden
64       end
65     end
66
67     def create
68       tags = params[:tags] || ""
69       description = params[:description] || ""
70       visibility = params[:visibility]
71
72       if visibility.nil?
73         visibility = if params[:public]&.to_i&.nonzero?
74                        "public"
75                      else
76                        "private"
77                      end
78       end
79
80       if params[:file].respond_to?(:read)
81         trace = do_create(params[:file], tags, description, visibility)
82
83         if trace.id
84           TraceImporterJob.perform_later(trace) if Settings.trace_use_job_queue
85           render :plain => trace.id.to_s
86         elsif trace.valid?
87           head :internal_server_error
88         else
89           head :bad_request
90         end
91       else
92         head :bad_request
93       end
94     end
95
96     private
97
98     def do_create(file, tags, description, visibility)
99       # Sanitise the user's filename
100       name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
101
102       # Get a temporary filename...
103       filename = "/tmp/#{rand}"
104
105       # ...and save the uploaded file to that location
106       File.open(filename, "wb") { |f| f.write(file.read) }
107
108       # Create the trace object, falsely marked as already
109       # inserted to stop the import daemon trying to load it
110       trace = Trace.new(
111         :name => name,
112         :tagstring => tags,
113         :description => description,
114         :visibility => visibility,
115         :inserted => true,
116         :user => current_user,
117         :timestamp => Time.now.getutc
118       )
119
120       if trace.valid?
121         Trace.transaction do
122           begin
123             # Save the trace object
124             trace.save!
125
126             # Rename the temporary file to the final name
127             FileUtils.mv(filename, trace.trace_name)
128           rescue StandardError
129             # Remove the file as we have failed to update the database
130             FileUtils.rm_f(filename)
131
132             # Pass the exception on
133             raise
134           end
135
136           begin
137             # Clear the inserted flag to make the import daemon load the trace
138             trace.inserted = false
139             trace.save!
140           rescue StandardError
141             # Remove the file as we have failed to update the database
142             FileUtils.rm_f(trace.trace_name)
143
144             # Pass the exception on
145             raise
146           end
147         end
148       end
149
150       # Finally save the user's preferred privacy level
151       if pref = current_user.preferences.where(:k => "gps.trace.visibility").first
152         pref.v = visibility
153         pref.save
154       else
155         current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
156       end
157
158       trace
159     end
160
161     def offline_redirect
162       redirect_to :action => :offline if Settings.status == "gpx_offline"
163     end
164   end
165 end