1 class TraceController < ApplicationController
4 before_filter :authorize_web
5 before_filter :set_locale
6 before_filter :require_user, :only => [:mine, :create, :edit, :delete]
7 before_filter :authorize, :only => [:api_details, :api_data, :api_create]
8 before_filter :check_database_readable, :except => [:api_details, :api_data, :api_create]
9 before_filter :check_database_writable, :only => [:create, :edit, :delete]
10 before_filter :check_api_readable, :only => [:api_details, :api_data]
11 before_filter :check_api_writable, :only => [:api_create]
12 before_filter :require_allow_read_gpx, :only => [:api_details, :api_data]
13 before_filter :require_allow_write_gpx, :only => [:api_create]
14 before_filter :offline_warning, :only => [:mine, :view]
15 before_filter :offline_redirect, :only => [:create, :edit, :delete, :data, :api_data, :api_create]
16 around_filter :api_call_handle_error, :only => [:api_details, :api_data, :api_create]
18 caches_action :list, :unless => :logged_in?, :layout => false
19 caches_action :view, :layout => false
20 caches_action :georss, :layout => true
21 cache_sweeper :trace_sweeper, :only => [:create, :edit, :delete, :api_create], :unless => STATUS == :database_offline
22 cache_sweeper :tracetag_sweeper, :only => [:create, :edit, :delete, :api_create], :unless => STATUS == :database_offline
24 # Counts and selects pages of GPX traces for various criteria (by user, tags, public etc.).
25 # target_user - if set, specifies the user to fetch traces for. if not set will fetch all traces
27 # from display name, pick up user id if one user's traces only
28 display_name = params[:display_name]
29 if !display_name.blank?
30 target_user = User.find(:first, :conditions => { :status => ["active", "confirmed"], :display_name => display_name })
32 @title = t'trace.no_such_user.title'
33 @not_found_user = display_name
34 render :action => 'no_such_user', :status => :not_found
41 @title = t 'trace.list.public_traces'
42 elsif @user and @user == target_user
43 @title = t 'trace.list.your_traces'
45 @title = t 'trace.list.public_traces_from', :user => target_user.display_name
48 @title += t 'trace.list.tagged_with', :tags => params[:tag] if params[:tag]
51 # 1 - all traces, logged in = all public traces + all user's (i.e + all mine)
52 # 2 - all traces, not logged in = all public traces
53 # 3 - user's traces, logged in as same user = all user's traces
54 # 4 - user's traces, not logged in as that user = all user's public traces
55 if target_user.nil? # all traces
57 conditions = ["(gpx_files.visibility in ('public', 'identifiable') OR gpx_files.user_id = ?)", @user.id] #1
59 conditions = ["gpx_files.visibility in ('public', 'identifiable')"] #2
62 if @user and @user == target_user
63 conditions = ["gpx_files.user_id = ?", @user.id] #3 (check vs user id, so no join + can't pick up non-public traces by changing name)
65 conditions = ["gpx_files.visibility in ('public', 'identifiable') AND gpx_files.user_id = ?", target_user.id] #4
72 files = Tracetag.find_all_by_tag(params[:tag]).collect { |tt| tt.gpx_id }
75 conditions[0] += " AND gpx_files.id IN (#{files.join(',')})"
77 conditions[0] += " AND 0 = 1"
81 conditions[0] += " AND gpx_files.visible = ?"
84 @page = (params[:page] || 1).to_i
87 @traces = Trace.find(:all,
88 :include => [:user, :tags],
89 :conditions => conditions,
90 :order => "gpx_files.timestamp DESC",
91 :offset => (@page - 1) * @page_size,
94 # put together SET of tags across traces, for related links
97 @traces.each do |trace|
98 trace.tags.reload if params[:tag] # if searched by tag, ActiveRecord won't bring back other tags, so do explicitly here
99 trace.tags.each do |tag|
100 tagset[tag.tag] = tag.tag
105 # final helper vars for view
106 @target_user = target_user
107 @display_name = target_user.display_name if target_user
108 @all_tags = tagset.values
112 redirect_to :action => :list, :display_name => @user.display_name
116 @trace = Trace.find(params[:id])
118 if @trace and @trace.visible? and
119 (@trace.public? or @trace.user == @user)
120 @title = t 'trace.view.title', :name => @trace.name
122 flash[:error] = t 'trace.view.trace_not_found'
123 redirect_to :controller => 'trace', :action => 'list'
125 rescue ActiveRecord::RecordNotFound
126 flash[:error] = t 'trace.view.trace_not_found'
127 redirect_to :controller => 'trace', :action => 'list'
132 logger.info(params[:trace][:gpx_file].class.name)
134 if params[:trace][:gpx_file].respond_to?(:read)
136 do_create(params[:trace][:gpx_file], params[:trace][:tagstring],
137 params[:trace][:description], params[:trace][:visibility])
143 logger.info("id is #{@trace.id}")
144 flash[:notice] = t 'trace.create.trace_uploaded'
146 if @user.traces.count(:conditions => { :inserted => false }) > 4
147 flash[:warning] = t 'trace.trace_header.traces_waiting', :count => @user.traces.count(:conditions => { :inserted => false })
150 redirect_to :action => :list, :display_name => @user.display_name
153 @trace = Trace.new({:name => "Dummy",
154 :tagstring => params[:trace][:tagstring],
155 :description => params[:trace][:description],
156 :visibility => params[:trace][:visibility],
157 :inserted => false, :user => @user,
158 :timestamp => Time.now.getutc})
160 @trace.errors.add(:gpx_file, "can't be blank")
163 @trace = Trace.new(:visibility => default_visibility)
166 @title = t 'trace.create.upload_trace'
170 trace = Trace.find(params[:id])
172 if trace.visible? and (trace.public? or (@user and @user == trace.user))
173 if request.format == Mime::XML
174 send_file(trace.xml_file, :filename => "#{trace.id}.xml", :type => Mime::XML.to_s, :disposition => 'attachment')
176 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => 'attachment')
179 render :nothing => true, :status => :not_found
181 rescue ActiveRecord::RecordNotFound
182 render :nothing => true, :status => :not_found
186 @trace = Trace.find(params[:id])
188 if @user and @trace.user == @user
189 @title = t 'trace.edit.title', :name => @trace.name
191 @trace.description = params[:trace][:description]
192 @trace.tagstring = params[:trace][:tagstring]
193 @trace.visibility = params[:trace][:visibility]
195 redirect_to :action => 'view'
199 render :nothing => true, :status => :forbidden
201 rescue ActiveRecord::RecordNotFound
202 render :nothing => true, :status => :not_found
206 trace = Trace.find(params[:id])
208 if @user and trace.user == @user
209 if request.post? and trace.visible?
210 trace.visible = false
212 flash[:notice] = t 'trace.delete.scheduled_for_deletion'
213 redirect_to :action => :list, :display_name => @user.display_name
215 render :nothing => true, :status => :bad_request
218 render :nothing => true, :status => :forbidden
220 rescue ActiveRecord::RecordNotFound
221 render :nothing => true, :status => :not_found
225 conditions = ["gpx_files.visibility in ('public', 'identifiable')"]
227 if params[:display_name]
228 conditions[0] += " AND users.display_name = ?"
229 conditions << params[:display_name]
233 conditions[0] += " AND EXISTS (SELECT * FROM gpx_file_tags AS gft WHERE gft.gpx_id = gpx_files.id AND gft.tag = ?)"
234 conditions << params[:tag]
237 traces = Trace.find(:all, :include => :user, :conditions => conditions,
238 :order => "timestamp DESC", :limit => 20)
240 rss = OSM::GeoRSS.new
242 traces.each do |trace|
243 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)
246 render :text => rss.to_s, :content_type => "application/rss+xml"
250 trace = Trace.find(params[:id])
253 if trace.public? or (@user and @user == trace.user)
254 expires_in 7.days, :private => !trace.public?, :public => trace.public?
255 send_file(trace.large_picture_name, :filename => "#{trace.id}.gif", :type => 'image/gif', :disposition => 'inline')
257 render :nothing => true, :status => :forbidden
260 render :nothing => true, :status => :not_found
262 rescue ActiveRecord::RecordNotFound
263 render :nothing => true, :status => :not_found
267 trace = Trace.find(params[:id])
270 if trace.public? or (@user and @user == trace.user)
271 expires_in 7.days, :private => !trace.public?, :public => trace.public?
272 send_file(trace.icon_picture_name, :filename => "#{trace.id}_icon.gif", :type => 'image/gif', :disposition => 'inline')
274 render :nothing => true, :status => :forbidden
277 render :nothing => true, :status => :not_found
279 rescue ActiveRecord::RecordNotFound
280 render :nothing => true, :status => :not_found
284 trace = Trace.find(params[:id])
286 if trace.public? or trace.user == @user
287 render :text => trace.to_xml.to_s, :content_type => "text/xml"
289 render :nothing => true, :status => :forbidden
291 rescue ActiveRecord::RecordNotFound
292 render :nothing => true, :status => :not_found
296 trace = Trace.find(params[:id])
298 if trace.public? or trace.user == @user
299 if request.format == Mime::XML
300 send_file(trace.xml_file, :filename => "#{trace.id}.xml", :type => Mime::XML.to_s, :disposition => 'attachment')
302 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => 'attachment')
305 render :nothing => true, :status => :forbidden
307 rescue ActiveRecord::RecordNotFound
308 render :nothing => true, :status => :not_found
313 tags = params[:tags] || ""
314 description = params[:description] || ""
315 visibility = params[:visibility]
318 if params[:public] && params[:public].to_i.nonzero?
319 visibility = "public"
321 visibility = "private"
325 if params[:file].respond_to?(:read)
326 do_create(params[:file], tags, description, visibility)
329 render :text => @trace.id.to_s, :content_type => "text/plain"
331 render :nothing => true, :status => :internal_server_error
333 render :nothing => true, :status => :bad_request
336 render :nothing => true, :status => :bad_request
339 render :nothing => true, :status => :method_not_allowed
345 def do_create(file, tags, description, visibility)
346 # Sanitise the user's filename
347 name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, '_')
349 # Get a temporary filename...
350 filename = "/tmp/#{rand}"
352 # ...and save the uploaded file to that location
353 File.open(filename, "w") { |f| f.write(file.read) }
355 # Create the trace object, falsely marked as already
356 # inserted to stop the import daemon trying to load it
360 :description => description,
361 :visibility => visibility,
364 :timestamp => Time.now.getutc
369 # Save the trace object
372 # Rename the temporary file to the final name
373 FileUtils.mv(filename, @trace.trace_name)
374 rescue Exception => ex
375 # Remove the file as we have failed to update the database
376 FileUtils.rm_f(filename)
378 # Pass the exception on
383 # Clear the inserted flag to make the import daemon load the trace
384 @trace.inserted = false
386 rescue Exception => ex
387 # Remove the file as we have failed to update the database
388 FileUtils.rm_f(@trace.trace_name)
390 # Pass the exception on
395 # Finally save the user's preferred privacy level
396 if pref = @user.preferences.find(:first, :conditions => {:k => "gps.trace.visibility"})
400 @user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
406 flash.now[:warning] = t 'trace.offline_warning.message' if STATUS == :gpx_offline
410 redirect_to :action => :offline if STATUS == :gpx_offline
413 def default_visibility
414 visibility = @user.preferences.find(:first, :conditions => {:k => "gps.trace.visibility"})
418 elsif @user.preferences.find(:first, :conditions => {:k => "gps.trace.public", :v => "default"}).nil?