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