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, :view, :layout => false
19 caches_action :georss, :layout => true
20 cache_sweeper :trace_sweeper, :only => [:create, :edit, :delete, :api_create], :unless => STATUS == :database_offline
21 cache_sweeper :tracetag_sweeper, :only => [:create, :edit, :delete, :api_create], :unless => STATUS == :database_offline
23 # Counts and selects pages of GPX traces for various criteria (by user, tags, public etc.).
24 # target_user - if set, specifies the user to fetch traces for. if not set will fetch all traces
26 # from display name, pick up user id if one user's traces only
27 display_name = params[:display_name]
28 if !display_name.blank?
29 target_user = User.find(:first, :conditions => { :status => ["active", "confirmed"], :display_name => display_name })
31 @title = t'trace.no_such_user.title'
32 @not_found_user = display_name
33 render :action => 'no_such_user', :status => :not_found
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 conditions = ["(gpx_files.visibility in ('public', 'identifiable') OR gpx_files.user_id = ?)", @user.id] #1
58 conditions = ["gpx_files.visibility in ('public', 'identifiable')"] #2
61 if @user and @user == target_user
62 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)
64 conditions = ["gpx_files.visibility in ('public', 'identifiable') AND gpx_files.user_id = ?", target_user.id] #4
71 files = Tracetag.find_all_by_tag(params[:tag]).collect { |tt| tt.gpx_id }
74 conditions[0] += " AND gpx_files.id IN (#{files.join(',')})"
76 conditions[0] += " AND 0 = 1"
80 conditions[0] += " AND gpx_files.visible = ?"
83 @page = (params[:page] || 1).to_i
86 @traces = Trace.find(:all,
87 :include => [:user, :tags],
88 :conditions => conditions,
89 :order => "gpx_files.timestamp DESC",
90 :offset => (@page - 1) * @page_size,
93 # put together SET of tags across traces, for related links
96 @traces.each do |trace|
97 trace.tags.reload if params[:tag] # if searched by tag, ActiveRecord won't bring back other tags, so do explicitly here
98 trace.tags.each do |tag|
99 tagset[tag.tag] = tag.tag
104 # final helper vars for view
105 @target_user = target_user
106 @display_name = target_user.display_name if target_user
107 @all_tags = tagset.values
111 redirect_to :action => :list, :display_name => @user.display_name
115 @trace = Trace.find(params[:id])
117 if @trace and @trace.visible? and
118 (@trace.public? or @trace.user == @user)
119 @title = t 'trace.view.title', :name => @trace.name
121 flash[:error] = t 'trace.view.trace_not_found'
122 redirect_to :controller => 'trace', :action => 'list'
124 rescue ActiveRecord::RecordNotFound
125 flash[:error] = t 'trace.view.trace_not_found'
126 redirect_to :controller => 'trace', :action => 'list'
131 logger.info(params[:trace][:gpx_file].class.name)
133 if params[:trace][:gpx_file].respond_to?(:read)
135 do_create(params[:trace][:gpx_file], params[:trace][:tagstring],
136 params[:trace][:description], params[:trace][:visibility])
142 logger.info("id is #{@trace.id}")
143 flash[:notice] = t 'trace.create.trace_uploaded'
145 if @user.traces.count(:conditions => { :inserted => false }) > 4
146 flash[:warning] = t 'trace.trace_header.traces_waiting', :count => @user.traces.count(:conditions => { :inserted => false })
149 redirect_to :action => :list, :display_name => @user.display_name
152 @trace = Trace.new({:name => "Dummy",
153 :tagstring => params[:trace][:tagstring],
154 :description => params[:trace][:description],
155 :visibility => params[:trace][:visibility],
156 :inserted => false, :user => @user,
157 :timestamp => Time.now.getutc})
159 @trace.errors.add(:gpx_file, "can't be blank")
162 @trace = Trace.new(:visibility => default_visibility)
165 @title = t 'trace.create.upload_trace'
169 trace = Trace.find(params[:id])
171 if trace.visible? and (trace.public? or (@user and @user == trace.user))
172 if request.format == Mime::XML
173 send_file(trace.xml_file, :filename => "#{trace.id}.xml", :type => Mime::XML.to_s, :disposition => 'attachment')
175 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => 'attachment')
178 render :nothing => true, :status => :not_found
180 rescue ActiveRecord::RecordNotFound
181 render :nothing => true, :status => :not_found
185 @trace = Trace.find(params[:id])
187 if @user and @trace.user == @user
188 @title = t 'trace.edit.title', :name => @trace.name
190 @trace.description = params[:trace][:description]
191 @trace.tagstring = params[:trace][:tagstring]
192 @trace.visibility = params[:trace][:visibility]
194 redirect_to :action => 'view'
198 render :nothing => true, :status => :forbidden
200 rescue ActiveRecord::RecordNotFound
201 render :nothing => true, :status => :not_found
205 trace = Trace.find(params[:id])
207 if @user and trace.user == @user
208 if request.post? and trace.visible?
209 trace.visible = false
211 flash[:notice] = t 'trace.delete.scheduled_for_deletion'
212 redirect_to :action => :list, :display_name => @user.display_name
214 render :nothing => true, :status => :bad_request
217 render :nothing => true, :status => :forbidden
219 rescue ActiveRecord::RecordNotFound
220 render :nothing => true, :status => :not_found
224 conditions = ["gpx_files.visibility in ('public', 'identifiable')"]
226 if params[:display_name]
227 conditions[0] += " AND users.display_name = ?"
228 conditions << params[:display_name]
232 conditions[0] += " AND EXISTS (SELECT * FROM gpx_file_tags AS gft WHERE gft.gpx_id = gpx_files.id AND gft.tag = ?)"
233 conditions << params[:tag]
236 traces = Trace.find(:all, :include => :user, :conditions => conditions,
237 :order => "timestamp DESC", :limit => 20)
239 rss = OSM::GeoRSS.new
241 traces.each do |trace|
242 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)
245 render :text => rss.to_s, :content_type => "application/rss+xml"
249 trace = Trace.find(params[:id])
252 if trace.public? or (@user and @user == trace.user)
253 expires_in 7.days, :private => !trace.public?, :public => trace.public?
254 send_file(trace.large_picture_name, :filename => "#{trace.id}.gif", :type => 'image/gif', :disposition => 'inline')
256 render :nothing => true, :status => :forbidden
259 render :nothing => true, :status => :not_found
261 rescue ActiveRecord::RecordNotFound
262 render :nothing => true, :status => :not_found
266 trace = Trace.find(params[:id])
269 if trace.public? or (@user and @user == trace.user)
270 expires_in 7.days, :private => !trace.public?, :public => trace.public?
271 send_file(trace.icon_picture_name, :filename => "#{trace.id}_icon.gif", :type => 'image/gif', :disposition => 'inline')
273 render :nothing => true, :status => :forbidden
276 render :nothing => true, :status => :not_found
278 rescue ActiveRecord::RecordNotFound
279 render :nothing => true, :status => :not_found
283 trace = Trace.find(params[:id])
285 if trace.public? or trace.user == @user
286 render :text => trace.to_xml.to_s, :content_type => "text/xml"
288 render :nothing => true, :status => :forbidden
290 rescue ActiveRecord::RecordNotFound
291 render :nothing => true, :status => :not_found
295 trace = Trace.find(params[:id])
297 if trace.public? or trace.user == @user
298 if request.format == Mime::XML
299 send_file(trace.xml_file, :filename => "#{trace.id}.xml", :type => Mime::XML.to_s, :disposition => 'attachment')
301 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => 'attachment')
304 render :nothing => true, :status => :forbidden
306 rescue ActiveRecord::RecordNotFound
307 render :nothing => true, :status => :not_found
312 tags = params[:tags] || ""
313 description = params[:description] || ""
314 visibility = params[:visibility]
317 if params[:public] && params[:public].to_i.nonzero?
318 visibility = "public"
320 visibility = "private"
324 if params[:file].respond_to?(:read)
325 do_create(params[:file], tags, description, visibility)
328 render :text => @trace.id.to_s, :content_type => "text/plain"
330 render :nothing => true, :status => :internal_server_error
332 render :nothing => true, :status => :bad_request
335 render :nothing => true, :status => :bad_request
338 render :nothing => true, :status => :method_not_allowed
344 def do_create(file, tags, description, visibility)
345 # Sanitise the user's filename
346 name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, '_')
348 # Get a temporary filename...
349 filename = "/tmp/#{rand}"
351 # ...and save the uploaded file to that location
352 File.open(filename, "w") { |f| f.write(file.read) }
354 # Create the trace object, falsely marked as already
355 # inserted to stop the import daemon trying to load it
359 :description => description,
360 :visibility => visibility,
363 :timestamp => Time.now.getutc
368 # Save the trace object
371 # Rename the temporary file to the final name
372 FileUtils.mv(filename, @trace.trace_name)
373 rescue Exception => ex
374 # Remove the file as we have failed to update the database
375 FileUtils.rm_f(filename)
377 # Pass the exception on
382 # Clear the inserted flag to make the import daemon load the trace
383 @trace.inserted = false
385 rescue Exception => ex
386 # Remove the file as we have failed to update the database
387 FileUtils.rm_f(@trace.trace_name)
389 # Pass the exception on
394 # Finally save the user's preferred privacy level
395 if pref = @user.preferences.find(:first, :conditions => {:k => "gps.trace.visibility"})
399 @user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
405 flash.now[:warning] = t 'trace.offline_warning.message' if STATUS == :gpx_offline
409 redirect_to :action => :offline if STATUS == :gpx_offline
412 def default_visibility
413 visibility = @user.preferences.find(:first, :conditions => {:k => "gps.trace.visibility"})
417 elsif @user.preferences.find(:first, :conditions => {:k => "gps.trace.public", :v => "default"}).nil?