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}, :without_protection => true)
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 Acl.no_trace_download(request.remote_ip)
167 render :nothing => true, :status => :forbidden
168 elsif request.format == Mime::XML
169 send_file(trace.xml_file, :filename => "#{trace.id}.xml", :type => Mime::XML.to_s, :disposition => 'attachment')
171 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => 'attachment')
174 render :nothing => true, :status => :not_found
176 rescue ActiveRecord::RecordNotFound
177 render :nothing => true, :status => :not_found
181 @trace = Trace.find(params[:id])
183 if @user and @trace.user == @user
184 @title = t 'trace.edit.title', :name => @trace.name
186 @trace.description = params[:trace][:description]
187 @trace.tagstring = params[:trace][:tagstring]
188 @trace.visibility = params[:trace][:visibility]
190 redirect_to :action => 'view'
194 render :nothing => true, :status => :forbidden
196 rescue ActiveRecord::RecordNotFound
197 render :nothing => true, :status => :not_found
201 trace = Trace.find(params[:id])
203 if @user and trace.user == @user
204 if request.post? and trace.visible?
205 trace.visible = false
207 flash[:notice] = t 'trace.delete.scheduled_for_deletion'
208 redirect_to :action => :list, :display_name => @user.display_name
210 render :nothing => true, :status => :bad_request
213 render :nothing => true, :status => :forbidden
215 rescue ActiveRecord::RecordNotFound
216 render :nothing => true, :status => :not_found
220 traces = Trace.public
222 if params[:display_name]
223 traces = traces.joins(:user).where(:users => {:display_name => params[:display_name]})
227 traces = traces.where("EXISTS (SELECT * FROM gpx_file_tags AS gft WHERE gft.gpx_id = gpx_files.id AND gft.tag = ?)", params[:tag])
230 traces = traces.order("timestamp DESC")
231 traces = traces.limit(20)
232 traces = traces.includes(:user)
234 rss = OSM::GeoRSS.new
236 traces.each do |trace|
237 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)
240 render :text => rss.to_s, :content_type => "application/rss+xml"
244 trace = Trace.find(params[:id])
247 if trace.public? or (@user and @user == trace.user)
248 expires_in 7.days, :private => !trace.public?, :public => trace.public?
249 send_file(trace.large_picture_name, :filename => "#{trace.id}.gif", :type => 'image/gif', :disposition => 'inline')
251 render :nothing => true, :status => :forbidden
254 render :nothing => true, :status => :not_found
256 rescue ActiveRecord::RecordNotFound
257 render :nothing => true, :status => :not_found
261 trace = Trace.find(params[:id])
264 if trace.public? or (@user and @user == trace.user)
265 expires_in 7.days, :private => !trace.public?, :public => trace.public?
266 send_file(trace.icon_picture_name, :filename => "#{trace.id}_icon.gif", :type => 'image/gif', :disposition => 'inline')
268 render :nothing => true, :status => :forbidden
271 render :nothing => true, :status => :not_found
273 rescue ActiveRecord::RecordNotFound
274 render :nothing => true, :status => :not_found
278 trace = Trace.visible.find(params[:id])
280 if trace.public? or trace.user == @user
281 render :text => trace.to_xml.to_s, :content_type => "text/xml"
283 render :nothing => true, :status => :forbidden
288 trace = Trace.visible.find(params[:id])
290 if trace.user == @user
291 new_trace = Trace.from_xml(request.raw_post)
293 unless new_trace and new_trace.id == trace.id
294 raise OSM::APIBadUserInput.new("The id in the url (#{trace.id}) is not the same as provided in the xml (#{new_trace.id})")
297 trace.description = new_trace.description
298 trace.tags = new_trace.tags
299 trace.visibility = new_trace.visibility
302 render :nothing => true, :status => :ok
304 render :nothing => true, :status => :forbidden
309 trace = Trace.visible.find(params[:id])
311 if trace.user == @user
312 trace.visible = false
315 render :nothing => true, :status => :ok
317 render :nothing => true, :status => :forbidden
322 trace = Trace.find(params[:id])
324 if trace.public? or trace.user == @user
325 if request.format == Mime::XML
326 send_file(trace.xml_file, :filename => "#{trace.id}.xml", :type => Mime::XML.to_s, :disposition => 'attachment')
328 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => 'attachment')
331 render :nothing => true, :status => :forbidden
337 tags = params[:tags] || ""
338 description = params[:description] || ""
339 visibility = params[:visibility]
342 if params[:public] && params[:public].to_i.nonzero?
343 visibility = "public"
345 visibility = "private"
349 if params[:file].respond_to?(:read)
350 do_create(params[:file], tags, description, visibility)
353 render :text => @trace.id.to_s, :content_type => "text/plain"
355 render :nothing => true, :status => :internal_server_error
357 render :nothing => true, :status => :bad_request
360 render :nothing => true, :status => :bad_request
363 render :nothing => true, :status => :method_not_allowed
369 def do_create(file, tags, description, visibility)
370 # Sanitise the user's filename
371 name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, '_')
373 # Get a temporary filename...
374 filename = "/tmp/#{rand}"
376 # ...and save the uploaded file to that location
377 File.open(filename, "w") { |f| f.write(file.read) }
379 # Create the trace object, falsely marked as already
380 # inserted to stop the import daemon trying to load it
384 :description => description,
385 :visibility => visibility,
388 :timestamp => Time.now.getutc
389 }, :without_protection => true)
393 # Save the trace object
396 # Rename the temporary file to the final name
397 FileUtils.mv(filename, @trace.trace_name)
398 rescue Exception => ex
399 # Remove the file as we have failed to update the database
400 FileUtils.rm_f(filename)
402 # Pass the exception on
407 # Clear the inserted flag to make the import daemon load the trace
408 @trace.inserted = false
410 rescue Exception => ex
411 # Remove the file as we have failed to update the database
412 FileUtils.rm_f(@trace.trace_name)
414 # Pass the exception on
419 # Finally save the user's preferred privacy level
420 if pref = @user.preferences.where(:k => "gps.trace.visibility").first
424 @user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
430 flash.now[:warning] = t 'trace.offline_warning.message' if STATUS == :gpx_offline
434 redirect_to :action => :offline if STATUS == :gpx_offline
437 def default_visibility
438 visibility = @user.preferences.where(:k => "gps.trace.visibility").first
442 elsif @user.preferences.where(:k => "gps.trace.public", :v => "default").first.nil?