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