1 class TraceController < ApplicationController
4 skip_before_filter :verify_authenticity_token, :only => [:api_create, :api_read, :api_update, :api_delete, :api_data]
5 before_filter :authorize_web
6 before_filter :set_locale
7 before_filter :require_user, :only => [:mine, :create, :edit, :delete]
8 before_filter :authorize, :only => [:api_create, :api_read, :api_update, :api_delete, :api_data]
9 before_filter :check_database_readable, :except => [:api_read, :api_data]
10 before_filter :check_database_writable, :only => [:create, :edit, :delete, :api_create, :api_update, :api_delete]
11 before_filter :check_api_readable, :only => [:api_read, :api_data]
12 before_filter :check_api_writable, :only => [:api_create, :api_update, :api_delete]
13 before_filter :require_allow_read_gpx, :only => [:api_read, :api_data]
14 before_filter :require_allow_write_gpx, :only => [:api_create, :api_update, :api_delete]
15 before_filter :offline_warning, :only => [:mine, :view]
16 before_filter :offline_redirect, :only => [:create, :edit, :delete, :data, :api_create, :api_delete, :api_data]
17 around_filter :api_call_handle_error, :only => [:api_create, :api_read, :api_update, :api_delete, :api_data]
19 # caches_action :list, :unless => :logged_in?, :layout => false
20 # caches_action :view, :layout => false
21 caches_action :georss, :layout => true
22 cache_sweeper :trace_sweeper, :only => [:create, :edit, :delete, :api_create, :api_update, :api_delete]
23 cache_sweeper :tracetag_sweeper, :only => [:create, :edit, :delete, :api_create, :api_update, :api_delete]
25 # Counts and selects pages of GPX traces for various criteria (by user, tags, public etc.).
26 # target_user - if set, specifies the user to fetch traces for. if not set will fetch all traces
28 # from display name, pick up user id if one user's traces only
29 display_name = params[:display_name]
30 if !display_name.blank?
31 target_user = User.active.where(:display_name => display_name).first
33 render_unknown_user display_name
40 @title = t 'trace.list.public_traces'
41 elsif @user and @user == target_user
42 @title = t 'trace.list.your_traces'
44 @title = t 'trace.list.public_traces_from', :user => target_user.display_name
47 @title += t 'trace.list.tagged_with', :tags => params[:tag] if params[:tag]
50 # 1 - all traces, logged in = all public traces + all user's (i.e + all mine)
51 # 2 - all traces, not logged in = all public traces
52 # 3 - user's traces, logged in as same user = all user's traces
53 # 4 - user's traces, not logged in as that user = all user's public traces
54 if target_user.nil? # all traces
56 @traces = Trace.visible_to(@user) #1
58 @traces = Trace.public #2
61 if @user and @user == target_user
62 @traces = @user.traces #3 (check vs user id, so no join + can't pick up non-public traces by changing name)
64 @traces = target_user.traces.public #4
69 @traces = @traces.tagged(params[:tag])
72 @page = (params[:page] || 1).to_i
75 @traces = @traces.visible
76 @traces = @traces.order("timestamp DESC")
77 @traces = @traces.offset((@page - 1) * @page_size)
78 @traces = @traces.limit(@page_size)
79 @traces = @traces.includes(:user, :tags)
81 # put together SET of tags across traces, for related links
83 @traces.each do |trace|
84 trace.tags.reload if params[:tag] # if searched by tag, ActiveRecord won't bring back other tags, so do explicitly here
85 trace.tags.each do |tag|
86 tagset[tag.tag] = tag.tag
90 # final helper vars for view
91 @target_user = target_user
92 @display_name = target_user.display_name if target_user
93 @all_tags = tagset.values
97 redirect_to :action => :list, :display_name => @user.display_name
101 @trace = Trace.find(params[:id])
103 if @trace and @trace.visible? and
104 (@trace.public? or @trace.user == @user)
105 @title = t 'trace.view.title', :name => @trace.name
107 flash[:error] = t 'trace.view.trace_not_found'
108 redirect_to :controller => 'trace', :action => 'list'
110 rescue ActiveRecord::RecordNotFound
111 flash[:error] = t 'trace.view.trace_not_found'
112 redirect_to :controller => 'trace', :action => 'list'
117 logger.info(params[:trace][:gpx_file].class.name)
119 if params[:trace][:gpx_file].respond_to?(:read)
121 do_create(params[:trace][:gpx_file], params[:trace][:tagstring],
122 params[:trace][:description], params[:trace][:visibility])
128 logger.info("id is #{@trace.id}")
129 flash[:notice] = t 'trace.create.trace_uploaded'
131 if @user.traces.count(:conditions => { :inserted => false }) > 4
132 flash[:warning] = t 'trace.trace_header.traces_waiting', :count => @user.traces.count(:conditions => { :inserted => false })
135 redirect_to :action => :list, :display_name => @user.display_name
138 @trace = Trace.new({:name => "Dummy",
139 :tagstring => params[:trace][:tagstring],
140 :description => params[:trace][:description],
141 :visibility => params[:trace][:visibility],
142 :inserted => false, :user => @user,
143 :timestamp => Time.now.getutc})
145 @trace.errors.add(:gpx_file, "can't be blank")
148 @trace = Trace.new({:visibility => default_visibility}, :without_protection => true)
151 @title = t 'trace.create.upload_trace'
155 trace = Trace.find(params[:id])
157 if trace.visible? and (trace.public? or (@user and @user == trace.user))
158 if Acl.no_trace_download(request.remote_ip)
159 render :nothing => true, :status => :forbidden
160 elsif request.format == Mime::XML
161 send_file(trace.xml_file, :filename => "#{trace.id}.xml", :type => Mime::XML.to_s, :disposition => 'attachment')
163 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => 'attachment')
166 render :nothing => true, :status => :not_found
168 rescue ActiveRecord::RecordNotFound
169 render :nothing => true, :status => :not_found
173 @trace = Trace.find(params[:id])
175 if @user and @trace.user == @user
176 @title = t 'trace.edit.title', :name => @trace.name
178 @trace.description = params[:trace][:description]
179 @trace.tagstring = params[:trace][:tagstring]
180 @trace.visibility = params[:trace][:visibility]
182 redirect_to :action => 'view', :display_name => @user.display_name
186 render :nothing => true, :status => :forbidden
188 rescue ActiveRecord::RecordNotFound
189 render :nothing => true, :status => :not_found
193 trace = Trace.find(params[:id])
195 if @user and trace.user == @user
197 trace.visible = false
199 flash[:notice] = t 'trace.delete.scheduled_for_deletion'
200 redirect_to :action => :list, :display_name => @user.display_name
202 render :nothing => true, :status => :not_found
205 render :nothing => true, :status => :forbidden
207 rescue ActiveRecord::RecordNotFound
208 render :nothing => true, :status => :not_found
212 traces = Trace.public.visible
214 if params[:display_name]
215 traces = traces.joins(:user).where(:users => {:display_name => params[:display_name]})
219 traces = traces.tagged(params[:tag])
222 traces = traces.order("timestamp DESC")
223 traces = traces.limit(20)
224 traces = traces.includes(:user)
226 rss = OSM::GeoRSS.new
228 traces.each do |trace|
229 rss.add(trace.latitude, trace.longitude, trace.name, trace.user.display_name, url_for({:controller => 'trace', :action => 'view', :id => trace.id, :display_name => trace.user.display_name}), "<img src='#{url_for({:controller => 'trace', :action => 'icon', :id => trace.id, :display_name => trace.user.display_name})}'> GPX file with #{trace.size} points from #{trace.user.display_name}", trace.timestamp)
232 render :text => rss.to_s, :content_type => "application/rss+xml"
236 trace = Trace.find(params[:id])
239 if trace.public? or (@user and @user == trace.user)
240 expires_in 7.days, :private => !trace.public?, :public => trace.public?
241 send_file(trace.large_picture_name, :filename => "#{trace.id}.gif", :type => 'image/gif', :disposition => 'inline')
243 render :nothing => true, :status => :forbidden
246 render :nothing => true, :status => :not_found
248 rescue ActiveRecord::RecordNotFound
249 render :nothing => true, :status => :not_found
253 trace = Trace.find(params[:id])
256 if trace.public? or (@user and @user == trace.user)
257 expires_in 7.days, :private => !trace.public?, :public => trace.public?
258 send_file(trace.icon_picture_name, :filename => "#{trace.id}_icon.gif", :type => 'image/gif', :disposition => 'inline')
260 render :nothing => true, :status => :forbidden
263 render :nothing => true, :status => :not_found
265 rescue ActiveRecord::RecordNotFound
266 render :nothing => true, :status => :not_found
270 trace = Trace.visible.find(params[:id])
272 if trace.public? or trace.user == @user
273 render :text => trace.to_xml.to_s, :content_type => "text/xml"
275 render :nothing => true, :status => :forbidden
280 trace = Trace.visible.find(params[:id])
282 if trace.user == @user
283 new_trace = Trace.from_xml(request.raw_post)
285 unless new_trace and new_trace.id == trace.id
286 raise OSM::APIBadUserInput.new("The id in the url (#{trace.id}) is not the same as provided in the xml (#{new_trace.id})")
289 trace.description = new_trace.description
290 trace.tags = new_trace.tags
291 trace.visibility = new_trace.visibility
294 render :nothing => true, :status => :ok
296 render :nothing => true, :status => :forbidden
301 trace = Trace.visible.find(params[:id])
303 if trace.user == @user
304 trace.visible = false
307 render :nothing => true, :status => :ok
309 render :nothing => true, :status => :forbidden
314 trace = Trace.find(params[:id])
316 if trace.public? or trace.user == @user
317 if request.format == Mime::XML
318 send_file(trace.xml_file, :filename => "#{trace.id}.xml", :type => Mime::XML.to_s, :disposition => 'attachment')
320 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => 'attachment')
323 render :nothing => true, :status => :forbidden
328 tags = params[:tags] || ""
329 description = params[:description] || ""
330 visibility = params[:visibility]
333 if params[:public] && params[:public].to_i.nonzero?
334 visibility = "public"
336 visibility = "private"
340 if params[:file].respond_to?(:read)
341 do_create(params[:file], tags, description, visibility)
344 render :text => @trace.id.to_s, :content_type => "text/plain"
346 render :nothing => true, :status => :internal_server_error
348 render :nothing => true, :status => :bad_request
351 render :nothing => true, :status => :bad_request
357 def do_create(file, tags, description, visibility)
358 # Sanitise the user's filename
359 name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, '_')
361 # Get a temporary filename...
362 filename = "/tmp/#{rand}"
364 # ...and save the uploaded file to that location
365 File.open(filename, "wb") { |f| f.write(file.read) }
367 # Create the trace object, falsely marked as already
368 # inserted to stop the import daemon trying to load it
372 :description => description,
373 :visibility => visibility,
376 :timestamp => Time.now.getutc
377 }, :without_protection => true)
381 # Save the trace object
384 # Rename the temporary file to the final name
385 FileUtils.mv(filename, @trace.trace_name)
386 rescue Exception => ex
387 # Remove the file as we have failed to update the database
388 FileUtils.rm_f(filename)
390 # Pass the exception on
395 # Clear the inserted flag to make the import daemon load the trace
396 @trace.inserted = false
398 rescue Exception => ex
399 # Remove the file as we have failed to update the database
400 FileUtils.rm_f(@trace.trace_name)
402 # Pass the exception on
407 # Finally save the user's preferred privacy level
408 if pref = @user.preferences.where(:k => "gps.trace.visibility").first
412 @user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
418 flash.now[:warning] = t 'trace.offline_warning.message' if STATUS == :gpx_offline
422 redirect_to :action => :offline if STATUS == :gpx_offline
425 def default_visibility
426 visibility = @user.preferences.where(:k => "gps.trace.visibility").first
430 elsif @user.preferences.where(:k => "gps.trace.public", :v => "default").first.nil?