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 # Counts and selects pages of GPX traces for various criteria (by user, tags, public etc.).
19 # target_user - if set, specifies the user to fetch traces for. if not set will fetch all traces
20 def list(target_user = nil, action = "list")
21 # from display name, pick up user id if one user's traces only
22 display_name = params[:display_name]
23 if target_user.nil? and !display_name.blank?
24 target_user = User.find(:first, :conditions => [ "visible = ? and display_name = ?", true, display_name])
26 @title = t'trace.no_such_user.title'
27 @not_found_user = display_name
28 render :action => 'no_such_user', :status => :not_found
35 @title = t 'trace.list.public_traces'
36 elsif @user and @user == target_user
37 @title = t 'trace.list.your_traces'
39 @title = t 'trace.list.public_traces_from', :user => target_user.display_name
42 @title += t 'trace.list.tagged_with', :tags => params[:tag] if params[:tag]
45 # 1 - all traces, logged in = all public traces + all user's (i.e + all mine)
46 # 2 - all traces, not logged in = all public traces
47 # 3 - user's traces, logged in as same user = all user's traces
48 # 4 - user's traces, not logged in as that user = all user's public traces
49 if target_user.nil? # all traces
51 conditions = ["(gpx_files.visibility in ('public', 'identifiable') OR gpx_files.user_id = ?)", @user.id] #1
53 conditions = ["gpx_files.visibility in ('public', 'identifiable')"] #2
56 if @user and @user == target_user
57 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)
59 conditions = ["gpx_files.visibility in ('public', 'identifiable') AND gpx_files.user_id = ?", target_user.id] #4
66 files = Tracetag.find_all_by_tag(params[:tag]).collect { |tt| tt.gpx_id }
69 conditions[0] += " AND gpx_files.id IN (#{files.join(',')})"
71 conditions[0] += " AND 0 = 1"
75 conditions[0] += " AND gpx_files.visible = ?"
78 @trace_pages, @traces = paginate(:traces,
79 :include => [:user, :tags],
80 :conditions => conditions,
81 :order => "gpx_files.timestamp DESC",
84 # put together SET of tags across traces, for related links
87 @traces.each do |trace|
88 trace.tags.reload if params[:tag] # if searched by tag, ActiveRecord won't bring back other tags, so do explicitly here
89 trace.tags.each do |tag|
90 tagset[tag.tag] = tag.tag
95 # final helper vars for view
97 @display_name = target_user.display_name if target_user
98 @all_tags = tagset.values
102 # Load the preference of whether the user set the trace public the last time
104 visibility = @user.preferences.find(:first, :conditions => {:k => "gps.trace.visibility"})
106 @trace.visibility = visibility.v
107 elsif @user.preferences.find(:first, :conditions => {:k => "gps.trace.public", :v => "default"}).nil?
108 @trace.visibility = "private"
110 @trace.visibility = "public"
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 redirect_to :action => 'mine'
148 @trace = Trace.new({:name => "Dummy",
149 :tagstring => params[:trace][:tagstring],
150 :description => params[:trace][:description],
151 :visibility => params[:trace][:visibility],
152 :inserted => false, :user => @user,
153 :timestamp => Time.now.getutc})
155 @trace.errors.add(:gpx_file, "can't be blank")
158 @title = t 'trace.create.upload_trace'
162 trace = Trace.find(params[:id])
164 if trace.visible? and (trace.public? or (@user and @user == trace.user))
165 if request.format == Mime::XML
166 send_file(trace.xml_file, :filename => "#{trace.id}.xml", :type => Mime::XML.to_s, :disposition => 'attachment')
168 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => 'attachment')
171 render :nothing => true, :status => :not_found
173 rescue ActiveRecord::RecordNotFound
174 render :nothing => true, :status => :not_found
178 @trace = Trace.find(params[:id])
180 if @user and @trace.user == @user
181 @title = t 'trace.edit.title', :name => @trace.name
183 @trace.description = params[:trace][:description]
184 @trace.tagstring = params[:trace][:tagstring]
185 @trace.visibility = params[:trace][:visibility]
187 redirect_to :action => 'view'
191 render :nothing => true, :status => :forbidden
193 rescue ActiveRecord::RecordNotFound
194 render :nothing => true, :status => :not_found
198 trace = Trace.find(params[:id])
200 if @user and trace.user == @user
201 if request.post? and trace.visible?
202 trace.visible = false
204 flash[:notice] = t 'trace.delete.scheduled_for_deletion'
205 redirect_to :controller => 'traces', :action => 'mine'
207 render :nothing => true, :status => :bad_request
210 render :nothing => true, :status => :forbidden
212 rescue ActiveRecord::RecordNotFound
213 render :nothing => true, :status => :not_found
217 conditions = ["gpx_files.visibility in ('public', 'identifiable')"]
219 if params[:display_name]
220 conditions[0] += " AND users.display_name = ?"
221 conditions << params[:display_name]
225 conditions[0] += " AND EXISTS (SELECT * FROM gpx_file_tags AS gft WHERE gft.gpx_id = gpx_files.id AND gft.tag = ?)"
226 conditions << params[:tag]
229 traces = Trace.find(:all, :include => :user, :conditions => conditions,
230 :order => "timestamp DESC", :limit => 20)
232 rss = OSM::GeoRSS.new
234 traces.each do |trace|
235 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)
238 render :text => rss.to_s, :content_type => "application/rss+xml"
242 trace = Trace.find(params[:id])
245 if trace.public? or (@user and @user == trace.user)
246 expires_in 7.days, :private => !trace.public?, :public => trace.public?
247 send_file(trace.large_picture_name, :filename => "#{trace.id}.gif", :type => 'image/gif', :disposition => 'inline')
249 render :nothing => true, :status => :forbidden
252 render :nothing => true, :status => :not_found
254 rescue ActiveRecord::RecordNotFound
255 render :nothing => true, :status => :not_found
259 trace = Trace.find(params[:id])
262 if trace.public? or (@user and @user == trace.user)
263 expires_in 7.days, :private => !trace.public?, :public => trace.public?
264 send_file(trace.icon_picture_name, :filename => "#{trace.id}_icon.gif", :type => 'image/gif', :disposition => 'inline')
266 render :nothing => true, :status => :forbidden
269 render :nothing => true, :status => :not_found
271 rescue ActiveRecord::RecordNotFound
272 render :nothing => true, :status => :not_found
276 trace = Trace.find(params[:id])
278 if trace.public? or trace.user == @user
279 render :text => trace.to_xml.to_s, :content_type => "text/xml"
281 render :nothing => true, :status => :forbidden
283 rescue ActiveRecord::RecordNotFound
284 render :nothing => true, :status => :not_found
288 trace = Trace.find(params[:id])
290 if trace.public? or trace.user == @user
291 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => 'attachment')
293 render :nothing => true, :status => :forbidden
295 rescue ActiveRecord::RecordNotFound
296 render :nothing => true, :status => :not_found
301 tags = params[:tags] || ""
302 description = params[:description] || ""
303 visibility = params[:visibility]
306 if params[:public] && params[:public].to_i.nonzero?
307 visibility = "public"
309 visibility = "private"
313 if params[:file].respond_to?(:read)
314 do_create(params[:file], tags, description, visibility)
317 render :text => @trace.id.to_s, :content_type => "text/plain"
319 render :nothing => true, :status => :internal_server_error
321 render :nothing => true, :status => :bad_request
324 render :nothing => true, :status => :bad_request
327 render :nothing => true, :status => :method_not_allowed
333 def do_create(file, tags, description, visibility)
334 # Sanitise the user's filename
335 name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, '_')
337 # Get a temporary filename...
338 filename = "/tmp/#{rand}"
340 # ...and save the uploaded file to that location
341 File.open(filename, "w") { |f| f.write(file.read) }
343 # Create the trace object, falsely marked as already
344 # inserted to stop the import daemon trying to load it
348 :description => description,
349 :visibility => visibility,
352 :timestamp => Time.now.getutc
357 # Save the trace object
360 # Rename the temporary file to the final name
361 FileUtils.mv(filename, @trace.trace_name)
362 rescue Exception => ex
363 # Remove the file as we have failed to update the database
364 FileUtils.rm_f(filename)
366 # Pass the exception on
371 # Clear the inserted flag to make the import daemon load the trace
372 @trace.inserted = false
374 rescue Exception => ex
375 # Remove the file as we have failed to update the database
376 FileUtils.rm_f(@trace.trace_name)
378 # Pass the exception on
383 # Finally save the user's preferred privacy level
384 if pref = @user.preferences.find(:first, :conditions => {:k => "gps.trace.visibility"})
388 @user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
394 flash.now[:warning] = t 'trace.offline_warning.message' if OSM_STATUS == :gpx_offline
398 redirect_to :action => :offline if OSM_STATUS == :gpx_offline