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