]> git.openstreetmap.org Git - rails.git/blob - app/controllers/trace_controller.rb
Make notes GPX valid and add useful official elements
[rails.git] / app / controllers / trace_controller.rb
1 class TraceController < ApplicationController
2   layout "site"
3
4   skip_before_action :verify_authenticity_token, :only => [:api_create, :api_read, :api_update, :api_delete, :api_data]
5   before_action :authorize_web
6   before_action :set_locale
7   before_action :require_user, :only => [:mine, :create, :edit, :delete]
8   before_action :authorize, :only => [:api_create, :api_read, :api_update, :api_delete, :api_data]
9   before_action :check_database_readable, :except => [:api_read, :api_data]
10   before_action :check_database_writable, :only => [:create, :edit, :delete, :api_create, :api_update, :api_delete]
11   before_action :check_api_readable, :only => [:api_read, :api_data]
12   before_action :check_api_writable, :only => [:api_create, :api_update, :api_delete]
13   before_action :require_allow_read_gpx, :only => [:api_read, :api_data]
14   before_action :require_allow_write_gpx, :only => [:api_create, :api_update, :api_delete]
15   before_action :offline_warning, :only => [:mine, :view]
16   before_action :offline_redirect, :only => [:create, :edit, :delete, :data, :api_create, :api_delete, :api_data]
17   around_action :api_call_handle_error, :only => [:api_create, :api_read, :api_update, :api_delete, :api_data]
18
19   # Counts and selects pages of GPX traces for various criteria (by user, tags, public etc.).
20   #  target_user - if set, specifies the user to fetch traces for.  if not set will fetch all traces
21   def list
22     # from display name, pick up user id if one user's traces only
23     display_name = params[:display_name]
24     unless display_name.blank?
25       target_user = User.active.where(:display_name => display_name).first
26       if target_user.nil?
27         render_unknown_user display_name
28         return
29       end
30     end
31
32     # set title
33     if target_user.nil?
34       @title = t "trace.list.public_traces"
35     elsif @user && @user == target_user
36       @title = t "trace.list.your_traces"
37     else
38       @title = t "trace.list.public_traces_from", :user => target_user.display_name
39     end
40
41     @title += t "trace.list.tagged_with", :tags => params[:tag] if params[:tag]
42
43     # four main cases:
44     # 1 - all traces, logged in = all public traces + all user's (i.e + all mine)
45     # 2 - all traces, not logged in = all public traces
46     # 3 - user's traces, logged in as same user = all user's traces
47     # 4 - user's traces, not logged in as that user = all user's public traces
48     if target_user.nil? # all traces
49       if @user
50         @traces = Trace.visible_to(@user) # 1
51       else
52         @traces = Trace.visible_to_all # 2
53       end
54     else
55       if @user && @user == target_user
56         @traces = @user.traces # 3 (check vs user id, so no join + can't pick up non-public traces by changing name)
57       else
58         @traces = target_user.traces.visible_to_all # 4
59       end
60     end
61
62     @traces = @traces.tagged(params[:tag]) if params[:tag]
63
64     @page = (params[:page] || 1).to_i
65     @page_size = 20
66
67     @traces = @traces.visible
68     @traces = @traces.order("timestamp DESC")
69     @traces = @traces.offset((@page - 1) * @page_size)
70     @traces = @traces.limit(@page_size)
71     @traces = @traces.includes(:user, :tags)
72
73     # put together SET of tags across traces, for related links
74     tagset = {}
75     @traces.each do |trace|
76       trace.tags.reload if params[:tag] # if searched by tag, ActiveRecord won't bring back other tags, so do explicitly here
77       trace.tags.each do |tag|
78         tagset[tag.tag] = tag.tag
79       end
80     end
81
82     # final helper vars for view
83     @target_user = target_user
84     @display_name = target_user.display_name if target_user
85     @all_tags = tagset.values
86   end
87
88   def mine
89     redirect_to :action => :list, :display_name => @user.display_name
90   end
91
92   def view
93     @trace = Trace.find(params[:id])
94
95     if @trace && @trace.visible? &&
96        (@trace.public? || @trace.user == @user)
97       @title = t "trace.view.title", :name => @trace.name
98     else
99       flash[:error] = t "trace.view.trace_not_found"
100       redirect_to :controller => "trace", :action => "list"
101     end
102   rescue ActiveRecord::RecordNotFound
103     flash[:error] = t "trace.view.trace_not_found"
104     redirect_to :controller => "trace", :action => "list"
105   end
106
107   def create
108     if params[:trace]
109       logger.info(params[:trace][:gpx_file].class.name)
110
111       if params[:trace][:gpx_file].respond_to?(:read)
112         begin
113           do_create(params[:trace][:gpx_file], params[:trace][:tagstring],
114                     params[:trace][:description], params[:trace][:visibility])
115         rescue => ex
116           logger.debug ex
117         end
118
119         if @trace.id
120           flash[:notice] = t "trace.create.trace_uploaded"
121
122           if @user.traces.where(:inserted => false).count > 4
123             flash[:warning] = t "trace.trace_header.traces_waiting", :count => @user.traces.where(:inserted => false).count
124           end
125
126           redirect_to :action => :list, :display_name => @user.display_name
127         end
128       else
129         @trace = Trace.new(:name => "Dummy",
130                            :tagstring => params[:trace][:tagstring],
131                            :description => params[:trace][:description],
132                            :visibility => params[:trace][:visibility],
133                            :inserted => false, :user => @user,
134                            :timestamp => Time.now.getutc)
135         @trace.valid?
136         @trace.errors.add(:gpx_file, "can't be blank")
137       end
138     else
139       @trace = Trace.new(:visibility => default_visibility)
140     end
141
142     @title = t "trace.create.upload_trace"
143   end
144
145   def data
146     trace = Trace.find(params[:id])
147
148     if trace.visible? && (trace.public? || (@user && @user == trace.user))
149       if Acl.no_trace_download(request.remote_ip)
150         render :text => "", :status => :forbidden
151       elsif request.format == Mime::XML
152         send_file(trace.xml_file, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
153       elsif request.format == Mime::GPX
154         send_file(trace.xml_file, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
155       else
156         send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
157       end
158     else
159       render :text => "", :status => :not_found
160     end
161   rescue ActiveRecord::RecordNotFound
162     render :text => "", :status => :not_found
163   end
164
165   def edit
166     @trace = Trace.find(params[:id])
167
168     if !@trace.visible?
169       render :text => "", :status => :not_found
170     elsif @user.nil? || @trace.user != @user
171       render :text => "", :status => :forbidden
172     else
173       @title = t "trace.edit.title", :name => @trace.name
174
175       if params[:trace]
176         @trace.description = params[:trace][:description]
177         @trace.tagstring = params[:trace][:tagstring]
178         @trace.visibility = params[:trace][:visibility]
179         if @trace.save
180           redirect_to :action => "view", :display_name => @user.display_name
181         end
182       end
183     end
184   rescue ActiveRecord::RecordNotFound
185     render :text => "", :status => :not_found
186   end
187
188   def delete
189     trace = Trace.find(params[:id])
190
191     if !trace.visible?
192       render :text => "", :status => :not_found
193     elsif @user.nil? || trace.user != @user
194       render :text => "", :status => :forbidden
195     else
196       trace.visible = false
197       trace.save
198       flash[:notice] = t "trace.delete.scheduled_for_deletion"
199       redirect_to :action => :list, :display_name => @user.display_name
200     end
201   rescue ActiveRecord::RecordNotFound
202     render :text => "", :status => :not_found
203   end
204
205   def georss
206     @traces = Trace.visible_to_all.visible
207
208     if params[:display_name]
209       @traces = @traces.joins(:user).where(:users => { :display_name => params[:display_name] })
210     end
211
212     @traces = @traces.tagged(params[:tag]) if params[:tag]
213     @traces = @traces.order("timestamp DESC")
214     @traces = @traces.limit(20)
215     @traces = @traces.includes(:user)
216   end
217
218   def picture
219     trace = Trace.find(params[:id])
220
221     if trace.visible? && trace.inserted?
222       if trace.public? || (@user && @user == trace.user)
223         expires_in 7.days, :private => !trace.public?, :public => trace.public?
224         send_file(trace.large_picture_name, :filename => "#{trace.id}.gif", :type => "image/gif", :disposition => "inline")
225       else
226         render :text => "", :status => :forbidden
227       end
228     else
229       render :text => "", :status => :not_found
230     end
231   rescue ActiveRecord::RecordNotFound
232     render :text => "", :status => :not_found
233   end
234
235   def icon
236     trace = Trace.find(params[:id])
237
238     if  trace.visible? && trace.inserted?
239       if trace.public? || (@user && @user == trace.user)
240         expires_in 7.days, :private => !trace.public?, :public => trace.public?
241         send_file(trace.icon_picture_name, :filename => "#{trace.id}_icon.gif", :type => "image/gif", :disposition => "inline")
242       else
243         render :text => "", :status => :forbidden
244       end
245     else
246       render :text => "", :status => :not_found
247     end
248   rescue ActiveRecord::RecordNotFound
249     render :text => "", :status => :not_found
250   end
251
252   def api_read
253     trace = Trace.visible.find(params[:id])
254
255     if trace.public? || trace.user == @user
256       render :text => trace.to_xml.to_s, :content_type => "text/xml"
257     else
258       render :text => "", :status => :forbidden
259     end
260   end
261
262   def api_update
263     trace = Trace.visible.find(params[:id])
264
265     if trace.user == @user
266       new_trace = Trace.from_xml(request.raw_post)
267
268       unless new_trace && new_trace.id == trace.id
269         fail OSM::APIBadUserInput.new("The id in the url (#{trace.id}) is not the same as provided in the xml (#{new_trace.id})")
270       end
271
272       trace.description = new_trace.description
273       trace.tags = new_trace.tags
274       trace.visibility = new_trace.visibility
275       trace.save!
276
277       render :text => "", :status => :ok
278     else
279       render :text => "", :status => :forbidden
280     end
281   end
282
283   def api_delete
284     trace = Trace.visible.find(params[:id])
285
286     if trace.user == @user
287       trace.visible = false
288       trace.save!
289
290       render :text => "", :status => :ok
291     else
292       render :text => "", :status => :forbidden
293     end
294   end
295
296   def api_data
297     trace = Trace.visible.find(params[:id])
298
299     if trace.public? || trace.user == @user
300       if request.format == Mime::XML
301         send_file(trace.xml_file, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
302       elsif request.format == Mime::GPX
303         send_file(trace.xml_file, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
304       else
305         send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
306       end
307     else
308       render :text => "", :status => :forbidden
309     end
310   end
311
312   def api_create
313     tags = params[:tags] || ""
314     description = params[:description] || ""
315     visibility = params[:visibility]
316
317     if visibility.nil?
318       if params[:public] && params[:public].to_i.nonzero?
319         visibility = "public"
320       else
321         visibility = "private"
322       end
323     end
324
325     if params[:file].respond_to?(:read)
326       do_create(params[:file], tags, description, visibility)
327
328       if @trace.id
329         render :text => @trace.id.to_s, :content_type => "text/plain"
330       elsif @trace.valid?
331         render :text => "", :status => :internal_server_error
332       else
333         render :text => "", :status => :bad_request
334       end
335     else
336       render :text => "", :status => :bad_request
337     end
338   end
339
340   private
341
342   def do_create(file, tags, description, visibility)
343     # Sanitise the user's filename
344     name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
345
346     # Get a temporary filename...
347     filename = "/tmp/#{rand}"
348
349     # ...and save the uploaded file to that location
350     File.open(filename, "wb") { |f| f.write(file.read) }
351
352     # Create the trace object, falsely marked as already
353     # inserted to stop the import daemon trying to load it
354     @trace = Trace.new(
355       :name => name,
356       :tagstring => tags,
357       :description => description,
358       :visibility => visibility,
359       :inserted => true,
360       :user => @user,
361       :timestamp => Time.now.getutc
362     )
363
364     Trace.transaction do
365       begin
366         # Save the trace object
367         @trace.save!
368
369         # Rename the temporary file to the final name
370         FileUtils.mv(filename, @trace.trace_name)
371       rescue StandardError
372         # Remove the file as we have failed to update the database
373         FileUtils.rm_f(filename)
374
375         # Pass the exception on
376         raise
377       end
378
379       begin
380         # Clear the inserted flag to make the import daemon load the trace
381         @trace.inserted = false
382         @trace.save!
383       rescue StandardError
384         # Remove the file as we have failed to update the database
385         FileUtils.rm_f(@trace.trace_name)
386
387         # Pass the exception on
388         raise
389       end
390     end
391
392     # Finally save the user's preferred privacy level
393     if pref = @user.preferences.where(:k => "gps.trace.visibility").first
394       pref.v = visibility
395       pref.save
396     else
397       @user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
398     end
399   end
400
401   def offline_warning
402     flash.now[:warning] = t "trace.offline_warning.message" if STATUS == :gpx_offline
403   end
404
405   def offline_redirect
406     redirect_to :action => :offline if STATUS == :gpx_offline
407   end
408
409   def default_visibility
410     visibility = @user.preferences.where(:k => "gps.trace.visibility").first
411
412     if visibility
413       visibility.v
414     elsif @user.preferences.where(:k => "gps.trace.public", :v => "default").first.nil?
415       "private"
416     else
417       "public"
418     end
419   end
420 end