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
71 files = Tracetag.where(:tag => params[:tag]).select(:gpx_id).all
74 @traces = @traces.where(:id => files.collect { |tt| tt.gpx_id })
78 @page = (params[:page] || 1).to_i
81 @traces = @traces.visible
82 @traces = @traces.order("timestamp DESC")
83 @traces = @traces.offset((@page - 1) * @page_size)
84 @traces = @traces.limit(@page_size)
85 @traces = @traces.includes(:user, :tags)
87 # put together SET of tags across traces, for related links
89 @traces.each do |trace|
90 trace.tags.reload if params[:tag] # if searched by tag, ActiveRecord won't bring back other tags, so do explicitly here
91 trace.tags.each do |tag|
92 tagset[tag.tag] = tag.tag
96 # final helper vars for view
97 @target_user = target_user
98 @display_name = target_user.display_name if target_user
99 @all_tags = tagset.values
103 redirect_to :action => :list, :display_name => @user.display_name
107 @trace = Trace.find(params[:id])
109 if @trace and @trace.visible? and
110 (@trace.public? or @trace.user == @user)
111 @title = t 'trace.view.title', :name => @trace.name
113 flash[:error] = t 'trace.view.trace_not_found'
114 redirect_to :controller => 'trace', :action => 'list'
116 rescue ActiveRecord::RecordNotFound
117 flash[:error] = t 'trace.view.trace_not_found'
118 redirect_to :controller => 'trace', :action => 'list'
123 logger.info(params[:trace][:gpx_file].class.name)
125 if params[:trace][:gpx_file].respond_to?(:read)
127 do_create(params[:trace][:gpx_file], params[:trace][:tagstring],
128 params[:trace][:description], params[:trace][:visibility])
134 logger.info("id is #{@trace.id}")
135 flash[:notice] = t 'trace.create.trace_uploaded'
137 if @user.traces.count(:conditions => { :inserted => false }) > 4
138 flash[:warning] = t 'trace.trace_header.traces_waiting', :count => @user.traces.count(:conditions => { :inserted => false })
141 redirect_to :action => :list, :display_name => @user.display_name
144 @trace = Trace.new({:name => "Dummy",
145 :tagstring => params[:trace][:tagstring],
146 :description => params[:trace][:description],
147 :visibility => params[:trace][:visibility],
148 :inserted => false, :user => @user,
149 :timestamp => Time.now.getutc})
151 @trace.errors.add(:gpx_file, "can't be blank")
154 @trace = Trace.new({:visibility => default_visibility}, :without_protection => true)
157 @title = t 'trace.create.upload_trace'
161 trace = Trace.find(params[:id])
163 if trace.visible? and (trace.public? or (@user and @user == trace.user))
164 if Acl.no_trace_download(request.remote_ip)
165 render :nothing => true, :status => :forbidden
166 elsif request.format == Mime::XML
167 send_file(trace.xml_file, :filename => "#{trace.id}.xml", :type => Mime::XML.to_s, :disposition => 'attachment')
169 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => 'attachment')
172 render :nothing => true, :status => :not_found
174 rescue ActiveRecord::RecordNotFound
175 render :nothing => true, :status => :not_found
179 @trace = Trace.find(params[:id])
181 if @user and @trace.user == @user
182 @title = t 'trace.edit.title', :name => @trace.name
184 @trace.description = params[:trace][:description]
185 @trace.tagstring = params[:trace][:tagstring]
186 @trace.visibility = params[:trace][:visibility]
188 redirect_to :action => 'view', :display_name => @user.display_name
192 render :nothing => true, :status => :forbidden
194 rescue ActiveRecord::RecordNotFound
195 render :nothing => true, :status => :not_found
199 trace = Trace.find(params[:id])
201 if @user and trace.user == @user
203 trace.visible = false
205 flash[:notice] = t 'trace.delete.scheduled_for_deletion'
206 redirect_to :action => :list, :display_name => @user.display_name
208 render :nothing => true, :status => :not_found
211 render :nothing => true, :status => :forbidden
213 rescue ActiveRecord::RecordNotFound
214 render :nothing => true, :status => :not_found
218 traces = Trace.public
220 if params[:display_name]
221 traces = traces.joins(:user).where(:users => {:display_name => params[:display_name]})
225 traces = traces.where("EXISTS (SELECT * FROM gpx_file_tags AS gft WHERE gft.gpx_id = gpx_files.id AND gft.tag = ?)", params[:tag])
228 traces = traces.order("timestamp DESC")
229 traces = traces.limit(20)
230 traces = traces.includes(:user)
232 rss = OSM::GeoRSS.new
234 traces.each do |trace|
235 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)
238 render :text => rss.to_s, :content_type => "application/rss+xml"
242 trace = Trace.find(params[:id])
245 if trace.public? or (@user and @user == trace.user)
246 expires_in 7.days, :private => !trace.public?, :public => trace.public?
247 send_file(trace.large_picture_name, :filename => "#{trace.id}.gif", :type => 'image/gif', :disposition => 'inline')
249 render :nothing => true, :status => :forbidden
252 render :nothing => true, :status => :not_found
254 rescue ActiveRecord::RecordNotFound
255 render :nothing => true, :status => :not_found
259 trace = Trace.find(params[:id])
262 if trace.public? or (@user and @user == trace.user)
263 expires_in 7.days, :private => !trace.public?, :public => trace.public?
264 send_file(trace.icon_picture_name, :filename => "#{trace.id}_icon.gif", :type => 'image/gif', :disposition => 'inline')
266 render :nothing => true, :status => :forbidden
269 render :nothing => true, :status => :not_found
271 rescue ActiveRecord::RecordNotFound
272 render :nothing => true, :status => :not_found
276 trace = Trace.visible.find(params[:id])
278 if trace.public? or trace.user == @user
279 render :text => trace.to_xml.to_s, :content_type => "text/xml"
281 render :nothing => true, :status => :forbidden
286 trace = Trace.visible.find(params[:id])
288 if trace.user == @user
289 new_trace = Trace.from_xml(request.raw_post)
291 unless new_trace and new_trace.id == trace.id
292 raise OSM::APIBadUserInput.new("The id in the url (#{trace.id}) is not the same as provided in the xml (#{new_trace.id})")
295 trace.description = new_trace.description
296 trace.tags = new_trace.tags
297 trace.visibility = new_trace.visibility
300 render :nothing => true, :status => :ok
302 render :nothing => true, :status => :forbidden
307 trace = Trace.visible.find(params[:id])
309 if trace.user == @user
310 trace.visible = false
313 render :nothing => true, :status => :ok
315 render :nothing => true, :status => :forbidden
320 trace = Trace.find(params[:id])
322 if trace.public? or trace.user == @user
323 if request.format == Mime::XML
324 send_file(trace.xml_file, :filename => "#{trace.id}.xml", :type => Mime::XML.to_s, :disposition => 'attachment')
326 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => 'attachment')
329 render :nothing => true, :status => :forbidden
334 tags = params[:tags] || ""
335 description = params[:description] || ""
336 visibility = params[:visibility]
339 if params[:public] && params[:public].to_i.nonzero?
340 visibility = "public"
342 visibility = "private"
346 if params[:file].respond_to?(:read)
347 do_create(params[:file], tags, description, visibility)
350 render :text => @trace.id.to_s, :content_type => "text/plain"
352 render :nothing => true, :status => :internal_server_error
354 render :nothing => true, :status => :bad_request
357 render :nothing => true, :status => :bad_request
363 def do_create(file, tags, description, visibility)
364 # Sanitise the user's filename
365 name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, '_')
367 # Get a temporary filename...
368 filename = "/tmp/#{rand}"
370 # ...and save the uploaded file to that location
371 File.open(filename, "w") { |f| f.write(file.read) }
373 # Create the trace object, falsely marked as already
374 # inserted to stop the import daemon trying to load it
378 :description => description,
379 :visibility => visibility,
382 :timestamp => Time.now.getutc
383 }, :without_protection => true)
387 # Save the trace object
390 # Rename the temporary file to the final name
391 FileUtils.mv(filename, @trace.trace_name)
392 rescue Exception => ex
393 # Remove the file as we have failed to update the database
394 FileUtils.rm_f(filename)
396 # Pass the exception on
401 # Clear the inserted flag to make the import daemon load the trace
402 @trace.inserted = false
404 rescue Exception => ex
405 # Remove the file as we have failed to update the database
406 FileUtils.rm_f(@trace.trace_name)
408 # Pass the exception on
413 # Finally save the user's preferred privacy level
414 if pref = @user.preferences.where(:k => "gps.trace.visibility").first
418 @user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
424 flash.now[:warning] = t 'trace.offline_warning.message' if STATUS == :gpx_offline
428 redirect_to :action => :offline if STATUS == :gpx_offline
431 def default_visibility
432 visibility = @user.preferences.where(:k => "gps.trace.visibility").first
436 elsif @user.preferences.where(:k => "gps.trace.public", :v => "default").first.nil?