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 => OSM_STATUS == :database_offline
21 cache_sweeper :tracetag_sweeper, :only => [:create, :edit, :delete, :api_create], :unless => OSM_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
108 @trace = Trace.new(:visibility => default_visibility) if @user
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)
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 => 'mine'
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 @title = t 'trace.create.upload_trace'
166 trace = Trace.find(params[:id])
168 if trace.visible? and (trace.public? or (@user and @user == trace.user))
169 if request.format == Mime::XML
170 send_file(trace.xml_file, :filename => "#{trace.id}.xml", :type => Mime::XML.to_s, :disposition => 'attachment')
172 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => 'attachment')
175 render :nothing => true, :status => :not_found
177 rescue ActiveRecord::RecordNotFound
178 render :nothing => true, :status => :not_found
182 @trace = Trace.find(params[:id])
184 if @user and @trace.user == @user
185 @title = t 'trace.edit.title', :name => @trace.name
187 @trace.description = params[:trace][:description]
188 @trace.tagstring = params[:trace][:tagstring]
189 @trace.visibility = params[:trace][:visibility]
191 redirect_to :action => 'view'
195 render :nothing => true, :status => :forbidden
197 rescue ActiveRecord::RecordNotFound
198 render :nothing => true, :status => :not_found
202 trace = Trace.find(params[:id])
204 if @user and trace.user == @user
205 if request.post? and trace.visible?
206 trace.visible = false
208 flash[:notice] = t 'trace.delete.scheduled_for_deletion'
209 redirect_to :controller => 'traces', :action => 'mine'
211 render :nothing => true, :status => :bad_request
214 render :nothing => true, :status => :forbidden
216 rescue ActiveRecord::RecordNotFound
217 render :nothing => true, :status => :not_found
221 conditions = ["gpx_files.visibility in ('public', 'identifiable')"]
223 if params[:display_name]
224 conditions[0] += " AND users.display_name = ?"
225 conditions << params[:display_name]
229 conditions[0] += " AND EXISTS (SELECT * FROM gpx_file_tags AS gft WHERE gft.gpx_id = gpx_files.id AND gft.tag = ?)"
230 conditions << params[:tag]
233 traces = Trace.find(:all, :include => :user, :conditions => conditions,
234 :order => "timestamp DESC", :limit => 20)
236 rss = OSM::GeoRSS.new
238 traces.each do |trace|
239 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)
242 render :text => rss.to_s, :content_type => "application/rss+xml"
246 trace = Trace.find(params[:id])
249 if trace.public? or (@user and @user == trace.user)
250 expires_in 7.days, :private => !trace.public?, :public => trace.public?
251 send_file(trace.large_picture_name, :filename => "#{trace.id}.gif", :type => 'image/gif', :disposition => 'inline')
253 render :nothing => true, :status => :forbidden
256 render :nothing => true, :status => :not_found
258 rescue ActiveRecord::RecordNotFound
259 render :nothing => true, :status => :not_found
263 trace = Trace.find(params[:id])
266 if trace.public? or (@user and @user == trace.user)
267 expires_in 7.days, :private => !trace.public?, :public => trace.public?
268 send_file(trace.icon_picture_name, :filename => "#{trace.id}_icon.gif", :type => 'image/gif', :disposition => 'inline')
270 render :nothing => true, :status => :forbidden
273 render :nothing => true, :status => :not_found
275 rescue ActiveRecord::RecordNotFound
276 render :nothing => true, :status => :not_found
280 trace = Trace.find(params[:id])
282 if trace.public? or trace.user == @user
283 render :text => trace.to_xml.to_s, :content_type => "text/xml"
285 render :nothing => true, :status => :forbidden
287 rescue ActiveRecord::RecordNotFound
288 render :nothing => true, :status => :not_found
292 trace = Trace.find(params[:id])
294 if trace.public? or trace.user == @user
295 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => 'attachment')
297 render :nothing => true, :status => :forbidden
299 rescue ActiveRecord::RecordNotFound
300 render :nothing => true, :status => :not_found
305 tags = params[:tags] || ""
306 description = params[:description] || ""
307 visibility = params[:visibility]
310 if params[:public] && params[:public].to_i.nonzero?
311 visibility = "public"
313 visibility = "private"
317 if params[:file].respond_to?(:read)
318 do_create(params[:file], tags, description, visibility)
321 render :text => @trace.id.to_s, :content_type => "text/plain"
323 render :nothing => true, :status => :internal_server_error
325 render :nothing => true, :status => :bad_request
328 render :nothing => true, :status => :bad_request
331 render :nothing => true, :status => :method_not_allowed
337 def do_create(file, tags, description, visibility)
338 # Sanitise the user's filename
339 name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, '_')
341 # Get a temporary filename...
342 filename = "/tmp/#{rand}"
344 # ...and save the uploaded file to that location
345 File.open(filename, "w") { |f| f.write(file.read) }
347 # Create the trace object, falsely marked as already
348 # inserted to stop the import daemon trying to load it
352 :description => description,
353 :visibility => visibility,
356 :timestamp => Time.now.getutc
361 # Save the trace object
364 # Rename the temporary file to the final name
365 FileUtils.mv(filename, @trace.trace_name)
366 rescue Exception => ex
367 # Remove the file as we have failed to update the database
368 FileUtils.rm_f(filename)
370 # Pass the exception on
375 # Clear the inserted flag to make the import daemon load the trace
376 @trace.inserted = false
378 rescue Exception => ex
379 # Remove the file as we have failed to update the database
380 FileUtils.rm_f(@trace.trace_name)
382 # Pass the exception on
387 # Finally save the user's preferred privacy level
388 if pref = @user.preferences.find(:first, :conditions => {:k => "gps.trace.visibility"})
392 @user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
398 flash.now[:warning] = t 'trace.offline_warning.message' if OSM_STATUS == :gpx_offline
402 redirect_to :action => :offline if OSM_STATUS == :gpx_offline
405 def default_visibility
406 visibility = @user.preferences.find(:first, :conditions => {:k => "gps.trace.visibility"})
410 elsif @user.preferences.find(:first, :conditions => {:k => "gps.trace.public", :v => "default"}).nil?