1 class TraceController < ApplicationController
4 before_filter :authorize_web
5 before_filter :set_locale
6 before_filter :require_user, :only => [:mine, :create, :edit, :delete, :make_public]
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, :make_public]
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]
15 # Counts and selects pages of GPX traces for various criteria (by user, tags, public etc.).
16 # target_user - if set, specifies the user to fetch traces for. if not set will fetch all traces
17 def list(target_user = nil, action = "list")
18 # from display name, pick up user id if one user's traces only
19 display_name = params[:display_name]
20 if target_user.nil? and !display_name.blank?
21 target_user = User.find(:first, :conditions => [ "visible = ? and display_name = ?", true, display_name])
23 @title = t'trace.no_such_user.title'
24 @not_found_user = display_name
25 render :action => 'no_such_user', :status => :not_found
32 @title = t 'trace.list.public_traces'
33 elsif @user and @user == target_user
34 @title = t 'trace.list.your_traces'
36 @title = t 'trace.list.public_traces_from', :user => target_user.display_name
39 @title += t 'trace.list.tagged_with', :tags => params[:tag] if params[:tag]
42 # 1 - all traces, logged in = all public traces + all user's (i.e + all mine)
43 # 2 - all traces, not logged in = all public traces
44 # 3 - user's traces, logged in as same user = all user's traces
45 # 4 - user's traces, not logged in as that user = all user's public traces
46 if target_user.nil? # all traces
48 conditions = ["(gpx_files.public = ? OR gpx_files.user_id = ?)", true, @user.id] #1
50 conditions = ["gpx_files.public = ?", true] #2
53 if @user and @user == target_user
54 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)
56 conditions = ["gpx_files.public = ? AND gpx_files.user_id = ?", true, target_user.id] #4
63 files = Tracetag.find_all_by_tag(params[:tag]).collect { |tt| tt.gpx_id }
66 conditions[0] += " AND gpx_files.id IN (#{files.join(',')})"
68 conditions[0] += " AND 0 = 1"
72 conditions[0] += " AND gpx_files.visible = ?"
75 @trace_pages, @traces = paginate(:traces,
76 :include => [:user, :tags],
77 :conditions => conditions,
78 :order => "gpx_files.timestamp DESC",
81 # put together SET of tags across traces, for related links
84 @traces.each do |trace|
85 trace.tags.reload if params[:tag] # if searched by tag, ActiveRecord won't bring back other tags, so do explicitly here
86 trace.tags.each do |tag|
87 tagset[tag.tag] = tag.tag
92 # final helper vars for view
94 @display_name = target_user.display_name if target_user
95 @all_tags = tagset.values
99 # Load the preference of whether the user set the trace public the last time
101 if @user.preferences.find(:first, :conditions => {:k => "gps.trace.public", :v => "default"}).nil?
102 @trace.public = false
110 @trace = Trace.find(params[:id])
112 if @trace and @trace.visible? and
113 (@trace.public? or @trace.user == @user)
114 @title = t 'trace.view.viewing_trace', :name => @trace.name
116 flash[:notice] = t 'trace.view.trace_not_found'
117 redirect_to :controller => 'trace', :action => 'list'
119 rescue ActiveRecord::RecordNotFound
120 flash[:notice] = t 'trace.view.trace_not_found'
121 redirect_to :controller => 'trace', :action => 'list'
126 logger.info(params[:trace][:gpx_file].class.name)
127 if params[:trace][:gpx_file].respond_to?(:read)
128 do_create(params[:trace][:gpx_file], params[:trace][:tagstring],
129 params[:trace][:description], params[:trace][:public])
132 logger.info("id is #{@trace.id}")
133 flash[:notice] = t 'trace.create.trace_uploaded'
135 redirect_to :action => 'mine'
138 @trace = Trace.new({:name => "Dummy",
139 :tagstring => params[:trace][:tagstring],
140 :description => params[:trace][:description],
141 :public => params[:trace][:public],
142 :inserted => false, :user => @user,
143 :timestamp => Time.now.getutc})
145 @trace.errors.add(:gpx_file, "can't be blank")
148 @title = t 'trace.create.upload_trace'
152 trace = Trace.find(params[:id])
154 if trace.visible? and (trace.public? or (@user and @user == trace.user))
155 if request.format == Mime::XML
156 send_file(trace.xml_file, :filename => "#{trace.id}.xml", :type => Mime::XML.to_s, :disposition => 'attachment')
158 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => 'attachment')
161 render :nothing => true, :status => :not_found
163 rescue ActiveRecord::RecordNotFound
164 render :nothing => true, :status => :not_found
168 @trace = Trace.find(params[:id])
170 if @user and @trace.user == @user
172 @trace.description = params[:trace][:description]
173 @trace.tagstring = params[:trace][:tagstring]
175 redirect_to :action => 'view'
179 render :nothing => true, :status => :forbidden
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 if request.post? and trace.visible?
190 trace.visible = false
192 flash[:notice] = t 'trace.delete.scheduled_for_deletion'
193 redirect_to :controller => 'traces', :action => 'mine'
195 render :nothing => true, :status => :bad_request
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.public?
211 flash[:notice] = t 'trace.make_public.made_public'
212 redirect_to :controller => 'trace', :action => 'view', :id => params[:id]
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.public = ?", true]
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, :user_login => 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 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => 'attachment')
300 render :nothing => true, :status => :forbidden
302 rescue ActiveRecord::RecordNotFound
303 render :nothing => true, :status => :not_found
308 tags = params[:tags] || ""
309 description = params[:description] || ""
310 pub = params[:public] || false
312 if params[:file].respond_to?(:read)
313 do_create(params[:file], tags, description, pub)
316 render :text => @trace.id.to_s, :content_type => "text/plain"
318 render :nothing => true, :status => :internal_server_error
320 render :nothing => true, :status => :bad_request
323 render :nothing => true, :status => :bad_request
326 render :nothing => true, :status => :method_not_allowed
332 def do_create(file, tags, description, public)
333 # Sanitise the user's filename
334 name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, '_')
336 # Get a temporary filename...
337 filename = "/tmp/#{rand}"
339 # ...and save the uploaded file to that location
340 File.open(filename, "w") { |f| f.write(file.read) }
342 # Create the trace object, falsely marked as already
343 # inserted to stop the import daemon trying to load it
347 :description => description,
351 :timestamp => Time.now.getutc
354 # Save the trace object
356 # Rename the temporary file to the final name
357 FileUtils.mv(filename, @trace.trace_name)
359 # Clear the inserted flag to make the import daemon load the trace
360 @trace.inserted = false
363 # Remove the file as we have failed to update the database
364 FileUtils.rm_f(filename)
367 # Finally save whether the user marked the trace as being public
369 if @user.trace_public_default.nil?
370 @user.preferences.create(:k => "gps.trace.public", :v => "default")
373 pref = @user.trace_public_default
374 pref.destroy unless pref.nil?