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]
21 cache_sweeper :tracetag_sweeper, :only => [:create, :edit, :delete, :api_create]
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
25 def list(target_user = nil, action = "list")
26 # from display name, pick up user id if one user's traces only
27 display_name = params[:display_name]
28 if target_user.nil? and !display_name.blank?
29 target_user = User.find(:first, :conditions => [ "visible = ? and display_name = ?", true, 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 @trace_pages, @traces = paginate(:traces,
84 :include => [:user, :tags],
85 :conditions => conditions,
86 :order => "gpx_files.timestamp DESC",
89 # put together SET of tags across traces, for related links
92 @traces.each do |trace|
93 trace.tags.reload if params[:tag] # if searched by tag, ActiveRecord won't bring back other tags, so do explicitly here
94 trace.tags.each do |tag|
95 tagset[tag.tag] = tag.tag
100 # final helper vars for view
102 @display_name = target_user.display_name if target_user
103 @all_tags = tagset.values
104 @trace = Trace.new(:visibility => default_visibility) if @user
108 redirect_to :action => :list, :display_name => @user.display_name
112 @trace = Trace.find(params[:id])
114 if @trace and @trace.visible? and
115 (@trace.public? or @trace.user == @user)
116 @title = t 'trace.view.title', :name => @trace.name
118 flash[:error] = t 'trace.view.trace_not_found'
119 redirect_to :controller => 'trace', :action => 'list'
121 rescue ActiveRecord::RecordNotFound
122 flash[:error] = t 'trace.view.trace_not_found'
123 redirect_to :controller => 'trace', :action => 'list'
128 logger.info(params[:trace][:gpx_file].class.name)
129 if params[:trace][:gpx_file].respond_to?(:read)
131 do_create(params[:trace][:gpx_file], params[:trace][:tagstring],
132 params[:trace][:description], params[:trace][:visibility])
138 logger.info("id is #{@trace.id}")
139 flash[:notice] = t 'trace.create.trace_uploaded'
141 redirect_to :action => 'mine'
144 @trace = Trace.new({:name => "Dummy",
145 :tagstring => params[:trace][:tagstring],
146 :description => params[:trace][:description],
147 :visibility => params[:trace][:visibility],
148 :inserted => false, :user => @user,
149 :timestamp => Time.now.getutc})
151 @trace.errors.add(:gpx_file, "can't be blank")
154 @title = t 'trace.create.upload_trace'
158 trace = Trace.find(params[:id])
160 if trace.visible? and (trace.public? or (@user and @user == trace.user))
161 if request.format == Mime::XML
162 send_file(trace.xml_file, :filename => "#{trace.id}.xml", :type => Mime::XML.to_s, :disposition => 'attachment')
164 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => 'attachment')
167 render :nothing => true, :status => :not_found
169 rescue ActiveRecord::RecordNotFound
170 render :nothing => true, :status => :not_found
174 @trace = Trace.find(params[:id])
176 if @user and @trace.user == @user
177 @title = t 'trace.edit.title', :name => @trace.name
179 @trace.description = params[:trace][:description]
180 @trace.tagstring = params[:trace][:tagstring]
181 @trace.visibility = params[:trace][:visibility]
183 redirect_to :action => 'view'
187 render :nothing => true, :status => :forbidden
189 rescue ActiveRecord::RecordNotFound
190 render :nothing => true, :status => :not_found
194 trace = Trace.find(params[:id])
196 if @user and trace.user == @user
197 if request.post? and trace.visible?
198 trace.visible = false
200 flash[:notice] = t 'trace.delete.scheduled_for_deletion'
201 redirect_to :controller => 'traces', :action => 'mine'
203 render :nothing => true, :status => :bad_request
206 render :nothing => true, :status => :forbidden
208 rescue ActiveRecord::RecordNotFound
209 render :nothing => true, :status => :not_found
213 conditions = ["gpx_files.visibility in ('public', 'identifiable')"]
215 if params[:display_name]
216 conditions[0] += " AND users.display_name = ?"
217 conditions << params[:display_name]
221 conditions[0] += " AND EXISTS (SELECT * FROM gpx_file_tags AS gft WHERE gft.gpx_id = gpx_files.id AND gft.tag = ?)"
222 conditions << params[:tag]
225 traces = Trace.find(:all, :include => :user, :conditions => conditions,
226 :order => "timestamp DESC", :limit => 20)
228 rss = OSM::GeoRSS.new
230 traces.each do |trace|
231 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)
234 render :text => rss.to_s, :content_type => "application/rss+xml"
238 trace = Trace.find(params[:id])
241 if trace.public? or (@user and @user == trace.user)
242 expires_in 7.days, :private => !trace.public?, :public => trace.public?
243 send_file(trace.large_picture_name, :filename => "#{trace.id}.gif", :type => 'image/gif', :disposition => 'inline')
245 render :nothing => true, :status => :forbidden
248 render :nothing => true, :status => :not_found
250 rescue ActiveRecord::RecordNotFound
251 render :nothing => true, :status => :not_found
255 trace = Trace.find(params[:id])
258 if trace.public? or (@user and @user == trace.user)
259 expires_in 7.days, :private => !trace.public?, :public => trace.public?
260 send_file(trace.icon_picture_name, :filename => "#{trace.id}_icon.gif", :type => 'image/gif', :disposition => 'inline')
262 render :nothing => true, :status => :forbidden
265 render :nothing => true, :status => :not_found
267 rescue ActiveRecord::RecordNotFound
268 render :nothing => true, :status => :not_found
272 trace = Trace.find(params[:id])
274 if trace.public? or trace.user == @user
275 render :text => trace.to_xml.to_s, :content_type => "text/xml"
277 render :nothing => true, :status => :forbidden
279 rescue ActiveRecord::RecordNotFound
280 render :nothing => true, :status => :not_found
284 trace = Trace.find(params[:id])
286 if trace.public? or trace.user == @user
287 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => 'attachment')
289 render :nothing => true, :status => :forbidden
291 rescue ActiveRecord::RecordNotFound
292 render :nothing => true, :status => :not_found
297 tags = params[:tags] || ""
298 description = params[:description] || ""
299 visibility = params[:visibility]
302 if params[:public] && params[:public].to_i.nonzero?
303 visibility = "public"
305 visibility = "private"
309 if params[:file].respond_to?(:read)
310 do_create(params[:file], tags, description, visibility)
313 render :text => @trace.id.to_s, :content_type => "text/plain"
315 render :nothing => true, :status => :internal_server_error
317 render :nothing => true, :status => :bad_request
320 render :nothing => true, :status => :bad_request
323 render :nothing => true, :status => :method_not_allowed
329 def do_create(file, tags, description, visibility)
330 # Sanitise the user's filename
331 name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, '_')
333 # Get a temporary filename...
334 filename = "/tmp/#{rand}"
336 # ...and save the uploaded file to that location
337 File.open(filename, "w") { |f| f.write(file.read) }
339 # Create the trace object, falsely marked as already
340 # inserted to stop the import daemon trying to load it
344 :description => description,
345 :visibility => visibility,
348 :timestamp => Time.now.getutc
353 # Save the trace object
356 # Rename the temporary file to the final name
357 FileUtils.mv(filename, @trace.trace_name)
358 rescue Exception => ex
359 # Remove the file as we have failed to update the database
360 FileUtils.rm_f(filename)
362 # Pass the exception on
367 # Clear the inserted flag to make the import daemon load the trace
368 @trace.inserted = false
370 rescue Exception => ex
371 # Remove the file as we have failed to update the database
372 FileUtils.rm_f(@trace.trace_name)
374 # Pass the exception on
379 # Finally save the user's preferred privacy level
380 if pref = @user.preferences.find(:first, :conditions => {:k => "gps.trace.visibility"})
384 @user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
390 flash.now[:warning] = t 'trace.offline_warning.message' if OSM_STATUS == :gpx_offline
394 redirect_to :action => :offline if OSM_STATUS == :gpx_offline
397 def default_visibility
398 visibility = @user.preferences.find(:first, :conditions => {:k => "gps.trace.visibility"})
402 elsif @user.preferences.find(:first, :conditions => {:k => "gps.trace.public", :v => "default"}).nil?