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]
22 cache_sweeper :tracetag_sweeper, :only => [:create, :edit, :delete, :api_create]
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
26 def list(target_user = nil, action = "list")
27 # from display name, pick up user id if one user's traces only
28 display_name = params[:display_name]
29 if target_user.nil? and !display_name.blank?
30 target_user = User.find(:first, :conditions => [ "visible = ? and display_name = ?", true, 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 @trace_pages, @traces = paginate(:traces,
85 :include => [:user, :tags],
86 :conditions => conditions,
87 :order => "gpx_files.timestamp DESC",
90 # put together SET of tags across traces, for related links
93 @traces.each do |trace|
94 trace.tags.reload if params[:tag] # if searched by tag, ActiveRecord won't bring back other tags, so do explicitly here
95 trace.tags.each do |tag|
96 tagset[tag.tag] = tag.tag
101 # final helper vars for view
103 @display_name = target_user.display_name if target_user
104 @all_tags = tagset.values
105 @trace = Trace.new(:visibility => default_visibility) if @user
109 redirect_to :action => :list, :display_name => @user.display_name
113 @trace = Trace.find(params[:id])
115 if @trace and @trace.visible? and
116 (@trace.public? or @trace.user == @user)
117 @title = t 'trace.view.title', :name => @trace.name
119 flash[:error] = t 'trace.view.trace_not_found'
120 redirect_to :controller => 'trace', :action => 'list'
122 rescue ActiveRecord::RecordNotFound
123 flash[:error] = t 'trace.view.trace_not_found'
124 redirect_to :controller => 'trace', :action => 'list'
129 logger.info(params[:trace][:gpx_file].class.name)
130 if params[:trace][:gpx_file].respond_to?(:read)
132 do_create(params[:trace][:gpx_file], params[:trace][:tagstring],
133 params[:trace][:description], params[:trace][:visibility])
139 logger.info("id is #{@trace.id}")
140 flash[:notice] = t 'trace.create.trace_uploaded'
142 redirect_to :action => 'mine'
145 @trace = Trace.new({:name => "Dummy",
146 :tagstring => params[:trace][:tagstring],
147 :description => params[:trace][:description],
148 :visibility => params[:trace][:visibility],
149 :inserted => false, :user => @user,
150 :timestamp => Time.now.getutc})
152 @trace.errors.add(:gpx_file, "can't be blank")
155 @title = t 'trace.create.upload_trace'
159 trace = Trace.find(params[:id])
161 if trace.visible? and (trace.public? or (@user and @user == trace.user))
162 if request.format == Mime::XML
163 send_file(trace.xml_file, :filename => "#{trace.id}.xml", :type => Mime::XML.to_s, :disposition => 'attachment')
165 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => 'attachment')
168 render :nothing => true, :status => :not_found
170 rescue ActiveRecord::RecordNotFound
171 render :nothing => true, :status => :not_found
175 @trace = Trace.find(params[:id])
177 if @user and @trace.user == @user
178 @title = t 'trace.edit.title', :name => @trace.name
180 @trace.description = params[:trace][:description]
181 @trace.tagstring = params[:trace][:tagstring]
182 @trace.visibility = params[:trace][:visibility]
184 redirect_to :action => 'view'
188 render :nothing => true, :status => :forbidden
190 rescue ActiveRecord::RecordNotFound
191 render :nothing => true, :status => :not_found
195 trace = Trace.find(params[:id])
197 if @user and trace.user == @user
198 if request.post? and trace.visible?
199 trace.visible = false
201 flash[:notice] = t 'trace.delete.scheduled_for_deletion'
202 redirect_to :controller => 'traces', :action => 'mine'
204 render :nothing => true, :status => :bad_request
207 render :nothing => true, :status => :forbidden
209 rescue ActiveRecord::RecordNotFound
210 render :nothing => true, :status => :not_found
214 conditions = ["gpx_files.visibility in ('public', 'identifiable')"]
216 if params[:display_name]
217 conditions[0] += " AND users.display_name = ?"
218 conditions << params[:display_name]
222 conditions[0] += " AND EXISTS (SELECT * FROM gpx_file_tags AS gft WHERE gft.gpx_id = gpx_files.id AND gft.tag = ?)"
223 conditions << params[:tag]
226 traces = Trace.find(:all, :include => :user, :conditions => conditions,
227 :order => "timestamp DESC", :limit => 20)
229 rss = OSM::GeoRSS.new
231 traces.each do |trace|
232 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, :user_login => trace.user.display_name})}'> GPX file with #{trace.size} points from #{trace.user.display_name}", trace.timestamp)
235 render :text => rss.to_s, :content_type => "application/rss+xml"
239 trace = Trace.find(params[:id])
242 if trace.public? or (@user and @user == trace.user)
243 expires_in 7.days, :private => !trace.public?, :public => trace.public?
244 send_file(trace.large_picture_name, :filename => "#{trace.id}.gif", :type => 'image/gif', :disposition => 'inline')
246 render :nothing => true, :status => :forbidden
249 render :nothing => true, :status => :not_found
251 rescue ActiveRecord::RecordNotFound
252 render :nothing => true, :status => :not_found
256 trace = Trace.find(params[:id])
259 if trace.public? or (@user and @user == trace.user)
260 expires_in 7.days, :private => !trace.public?, :public => trace.public?
261 send_file(trace.icon_picture_name, :filename => "#{trace.id}_icon.gif", :type => 'image/gif', :disposition => 'inline')
263 render :nothing => true, :status => :forbidden
266 render :nothing => true, :status => :not_found
268 rescue ActiveRecord::RecordNotFound
269 render :nothing => true, :status => :not_found
273 trace = Trace.find(params[:id])
275 if trace.public? or trace.user == @user
276 render :text => trace.to_xml.to_s, :content_type => "text/xml"
278 render :nothing => true, :status => :forbidden
280 rescue ActiveRecord::RecordNotFound
281 render :nothing => true, :status => :not_found
285 trace = Trace.find(params[:id])
287 if trace.public? or trace.user == @user
288 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => 'attachment')
290 render :nothing => true, :status => :forbidden
292 rescue ActiveRecord::RecordNotFound
293 render :nothing => true, :status => :not_found
298 tags = params[:tags] || ""
299 description = params[:description] || ""
300 visibility = params[:visibility]
303 if params[:public] && params[:public].to_i.nonzero?
304 visibility = "public"
306 visibility = "private"
310 if params[:file].respond_to?(:read)
311 do_create(params[:file], tags, description, visibility)
314 render :text => @trace.id.to_s, :content_type => "text/plain"
316 render :nothing => true, :status => :internal_server_error
318 render :nothing => true, :status => :bad_request
321 render :nothing => true, :status => :bad_request
324 render :nothing => true, :status => :method_not_allowed
330 def do_create(file, tags, description, visibility)
331 # Sanitise the user's filename
332 name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, '_')
334 # Get a temporary filename...
335 filename = "/tmp/#{rand}"
337 # ...and save the uploaded file to that location
338 File.open(filename, "w") { |f| f.write(file.read) }
340 # Create the trace object, falsely marked as already
341 # inserted to stop the import daemon trying to load it
345 :description => description,
346 :visibility => visibility,
349 :timestamp => Time.now.getutc
354 # Save the trace object
357 # Rename the temporary file to the final name
358 FileUtils.mv(filename, @trace.trace_name)
359 rescue Exception => ex
360 # Remove the file as we have failed to update the database
361 FileUtils.rm_f(filename)
363 # Pass the exception on
368 # Clear the inserted flag to make the import daemon load the trace
369 @trace.inserted = false
371 rescue Exception => ex
372 # Remove the file as we have failed to update the database
373 FileUtils.rm_f(@trace.trace_name)
375 # Pass the exception on
380 # Finally save the user's preferred privacy level
381 if pref = @user.preferences.find(:first, :conditions => {:k => "gps.trace.visibility"})
385 @user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
391 flash.now[:warning] = t 'trace.offline_warning.message' if OSM_STATUS == :gpx_offline
395 redirect_to :action => :offline if OSM_STATUS == :gpx_offline
398 def default_visibility
399 visibility = @user.preferences.find(:first, :conditions => {:k => "gps.trace.visibility"})
403 elsif @user.preferences.find(:first, :conditions => {:k => "gps.trace.public", :v => "default"}).nil?