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 @title = t'trace.no_such_user.title'
34 @not_found_user = display_name
35 render :action => 'no_such_user', :status => :not_found
42 @title = t 'trace.list.public_traces'
43 elsif @user and @user == target_user
44 @title = t 'trace.list.your_traces'
46 @title = t 'trace.list.public_traces_from', :user => target_user.display_name
49 @title += t 'trace.list.tagged_with', :tags => params[:tag] if params[:tag]
52 # 1 - all traces, logged in = all public traces + all user's (i.e + all mine)
53 # 2 - all traces, not logged in = all public traces
54 # 3 - user's traces, logged in as same user = all user's traces
55 # 4 - user's traces, not logged in as that user = all user's public traces
56 if target_user.nil? # all traces
58 @traces = Trace.visible_to(@user) #1
60 @traces = Trace.public #2
63 if @user and @user == target_user
64 @traces = @user.traces #3 (check vs user id, so no join + can't pick up non-public traces by changing name)
66 @traces = target_user.traces.public #4
73 files = Tracetag.where(:tag => params[:tag]).select(:gpx_id).all
76 @traces = @traces.where(:id => files.collect { |tt| tt.gpx_id })
80 @page = (params[:page] || 1).to_i
83 @traces = @traces.visible
84 @traces = @traces.order("timestamp DESC")
85 @traces = @traces.offset((@page - 1) * @page_size)
86 @traces = @traces.limit(@page_size)
87 @traces = @traces.includes(:user, :tags)
89 # put together SET of tags across traces, for related links
91 @traces.each do |trace|
92 trace.tags.reload if params[:tag] # if searched by tag, ActiveRecord won't bring back other tags, so do explicitly here
93 trace.tags.each do |tag|
94 tagset[tag.tag] = tag.tag
98 # final helper vars for view
99 @target_user = target_user
100 @display_name = target_user.display_name if target_user
101 @all_tags = tagset.values
105 redirect_to :action => :list, :display_name => @user.display_name
109 @trace = Trace.find(params[:id])
111 if @trace and @trace.visible? and
112 (@trace.public? or @trace.user == @user)
113 @title = t 'trace.view.title', :name => @trace.name
115 flash[:error] = t 'trace.view.trace_not_found'
116 redirect_to :controller => 'trace', :action => 'list'
118 rescue ActiveRecord::RecordNotFound
119 flash[:error] = t 'trace.view.trace_not_found'
120 redirect_to :controller => 'trace', :action => 'list'
125 logger.info(params[:trace][:gpx_file].class.name)
127 if params[:trace][:gpx_file].respond_to?(:read)
129 do_create(params[:trace][:gpx_file], params[:trace][:tagstring],
130 params[:trace][:description], params[:trace][:visibility])
136 logger.info("id is #{@trace.id}")
137 flash[:notice] = t 'trace.create.trace_uploaded'
139 if @user.traces.count(:conditions => { :inserted => false }) > 4
140 flash[:warning] = t 'trace.trace_header.traces_waiting', :count => @user.traces.count(:conditions => { :inserted => false })
143 redirect_to :action => :list, :display_name => @user.display_name
146 @trace = Trace.new({:name => "Dummy",
147 :tagstring => params[:trace][:tagstring],
148 :description => params[:trace][:description],
149 :visibility => params[:trace][:visibility],
150 :inserted => false, :user => @user,
151 :timestamp => Time.now.getutc})
153 @trace.errors.add(:gpx_file, "can't be blank")
156 @trace = Trace.new(:visibility => default_visibility)
159 @title = t 'trace.create.upload_trace'
163 trace = Trace.find(params[:id])
165 if trace.visible? and (trace.public? or (@user and @user == trace.user))
166 if 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'
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
202 if request.post? and trace.visible?
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 => :bad_request
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 = ?)")
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
335 tags = params[:tags] || ""
336 description = params[:description] || ""
337 visibility = params[:visibility]
340 if params[:public] && params[:public].to_i.nonzero?
341 visibility = "public"
343 visibility = "private"
347 if params[:file].respond_to?(:read)
348 do_create(params[:file], tags, description, visibility)
351 render :text => @trace.id.to_s, :content_type => "text/plain"
353 render :nothing => true, :status => :internal_server_error
355 render :nothing => true, :status => :bad_request
358 render :nothing => true, :status => :bad_request
361 render :nothing => true, :status => :method_not_allowed
367 def do_create(file, tags, description, visibility)
368 # Sanitise the user's filename
369 name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, '_')
371 # Get a temporary filename...
372 filename = "/tmp/#{rand}"
374 # ...and save the uploaded file to that location
375 File.open(filename, "w") { |f| f.write(file.read) }
377 # Create the trace object, falsely marked as already
378 # inserted to stop the import daemon trying to load it
382 :description => description,
383 :visibility => visibility,
386 :timestamp => Time.now.getutc
391 # Save the trace object
394 # Rename the temporary file to the final name
395 FileUtils.mv(filename, @trace.trace_name)
396 rescue Exception => ex
397 # Remove the file as we have failed to update the database
398 FileUtils.rm_f(filename)
400 # Pass the exception on
405 # Clear the inserted flag to make the import daemon load the trace
406 @trace.inserted = false
408 rescue Exception => ex
409 # Remove the file as we have failed to update the database
410 FileUtils.rm_f(@trace.trace_name)
412 # Pass the exception on
417 # Finally save the user's preferred privacy level
418 if pref = @user.preferences.where(:k => "gps.trace.visibility").first
422 @user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
428 flash.now[:warning] = t 'trace.offline_warning.message' if STATUS == :gpx_offline
432 redirect_to :action => :offline if STATUS == :gpx_offline
435 def default_visibility
436 visibility = @user.preferences.where(:k => "gps.trace.visibility").first
440 elsif @user.preferences.where(:k => "gps.trace.public", :v => "default").first.nil?