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