1 class TraceController < ApplicationController
4 skip_before_filter :verify_authenticity_token, :only => [:api_create, :api_read, :api_update, :api_delete, :api_data]
5 before_filter :authorize_web
6 before_filter :set_locale
7 before_filter :require_user, :only => [:mine, :create, :edit, :delete]
8 before_filter :authorize, :only => [:api_create, :api_read, :api_update, :api_delete, :api_data]
9 before_filter :check_database_readable, :except => [:api_read, :api_data]
10 before_filter :check_database_writable, :only => [:create, :edit, :delete, :api_create, :api_update, :api_delete]
11 before_filter :check_api_readable, :only => [:api_read, :api_data]
12 before_filter :check_api_writable, :only => [:api_create, :api_update, :api_delete]
13 before_filter :require_allow_read_gpx, :only => [:api_read, :api_data]
14 before_filter :require_allow_write_gpx, :only => [:api_create, :api_update, :api_delete]
15 before_filter :offline_warning, :only => [:mine, :view]
16 before_filter :offline_redirect, :only => [:create, :edit, :delete, :data, :api_create, :api_delete, :api_data]
17 around_filter :api_call_handle_error, :only => [:api_create, :api_read, :api_update, :api_delete, :api_data]
19 # Counts and selects pages of GPX traces for various criteria (by user, tags, public etc.).
20 # target_user - if set, specifies the user to fetch traces for. if not set will fetch all traces
22 # from display name, pick up user id if one user's traces only
23 display_name = params[:display_name]
24 if !display_name.blank?
25 target_user = User.active.where(:display_name => display_name).first
27 render_unknown_user display_name
34 @title = t 'trace.list.public_traces'
35 elsif @user and @user == target_user
36 @title = t 'trace.list.your_traces'
38 @title = t 'trace.list.public_traces_from', :user => target_user.display_name
41 @title += t 'trace.list.tagged_with', :tags => params[:tag] if params[:tag]
44 # 1 - all traces, logged in = all public traces + all user's (i.e + all mine)
45 # 2 - all traces, not logged in = all public traces
46 # 3 - user's traces, logged in as same user = all user's traces
47 # 4 - user's traces, not logged in as that user = all user's public traces
48 if target_user.nil? # all traces
50 @traces = Trace.visible_to(@user) #1
52 @traces = Trace.public #2
55 if @user and @user == target_user
56 @traces = @user.traces #3 (check vs user id, so no join + can't pick up non-public traces by changing name)
58 @traces = target_user.traces.public #4
63 @traces = @traces.tagged(params[:tag])
66 @page = (params[:page] || 1).to_i
69 @traces = @traces.visible
70 @traces = @traces.order("timestamp DESC")
71 @traces = @traces.offset((@page - 1) * @page_size)
72 @traces = @traces.limit(@page_size)
73 @traces = @traces.includes(:user, :tags)
75 # put together SET of tags across traces, for related links
77 @traces.each do |trace|
78 trace.tags.reload if params[:tag] # if searched by tag, ActiveRecord won't bring back other tags, so do explicitly here
79 trace.tags.each do |tag|
80 tagset[tag.tag] = tag.tag
84 # final helper vars for view
85 @target_user = target_user
86 @display_name = target_user.display_name if target_user
87 @all_tags = tagset.values
91 redirect_to :action => :list, :display_name => @user.display_name
95 @trace = Trace.find(params[:id])
97 if @trace and @trace.visible? and
98 (@trace.public? or @trace.user == @user)
99 @title = t 'trace.view.title', :name => @trace.name
101 flash[:error] = t 'trace.view.trace_not_found'
102 redirect_to :controller => 'trace', :action => 'list'
104 rescue ActiveRecord::RecordNotFound
105 flash[:error] = t 'trace.view.trace_not_found'
106 redirect_to :controller => 'trace', :action => 'list'
111 logger.info(params[:trace][:gpx_file].class.name)
113 if params[:trace][:gpx_file].respond_to?(:read)
115 do_create(params[:trace][:gpx_file], params[:trace][:tagstring],
116 params[:trace][:description], params[:trace][:visibility])
122 logger.info("id is #{@trace.id}")
123 flash[:notice] = t 'trace.create.trace_uploaded'
125 if @user.traces.where(:inserted => false).count > 4
126 flash[:warning] = t 'trace.trace_header.traces_waiting', :count => @user.traces.where(:inserted => false).count
129 redirect_to :action => :list, :display_name => @user.display_name
132 @trace = Trace.new({:name => "Dummy",
133 :tagstring => params[:trace][:tagstring],
134 :description => params[:trace][:description],
135 :visibility => params[:trace][:visibility],
136 :inserted => false, :user => @user,
137 :timestamp => Time.now.getutc})
139 @trace.errors.add(:gpx_file, "can't be blank")
142 @trace = Trace.new(:visibility => default_visibility)
145 @title = t 'trace.create.upload_trace'
149 trace = Trace.find(params[:id])
151 if trace.visible? and (trace.public? or (@user and @user == trace.user))
152 if Acl.no_trace_download(request.remote_ip)
153 render :text => "", :status => :forbidden
154 elsif request.format == Mime::XML or request.format == Mime::GPX
155 send_file(trace.xml_file, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => 'attachment')
157 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => 'attachment')
160 render :text => "", :status => :not_found
162 rescue ActiveRecord::RecordNotFound
163 render :text => "", :status => :not_found
167 @trace = Trace.find(params[:id])
169 if not @trace.visible?
170 render :text => "", :status => :not_found
171 elsif @user.nil? or @trace.user != @user
172 render :text => "", :status => :forbidden
174 @title = t 'trace.edit.title', :name => @trace.name
177 @trace.description = params[:trace][:description]
178 @trace.tagstring = params[:trace][:tagstring]
179 @trace.visibility = params[:trace][:visibility]
181 redirect_to :action => 'view', :display_name => @user.display_name
185 rescue ActiveRecord::RecordNotFound
186 render :text => "", :status => :not_found
190 trace = Trace.find(params[:id])
192 if not trace.visible?
193 render :text => "", :status => :not_found
194 elsif @user.nil? or trace.user != @user
195 render :text => "", :status => :forbidden
197 trace.visible = false
199 flash[:notice] = t 'trace.delete.scheduled_for_deletion'
200 redirect_to :action => :list, :display_name => @user.display_name
202 rescue ActiveRecord::RecordNotFound
203 render :text => "", :status => :not_found
207 @traces = Trace.public.visible
209 if params[:display_name]
210 @traces = @traces.joins(:user).where(:users => {:display_name => params[:display_name]})
214 @traces = @traces.tagged(params[:tag])
217 @traces = @traces.order("timestamp DESC")
218 @traces = @traces.limit(20)
219 @traces = @traces.includes(:user)
223 trace = Trace.find(params[:id])
226 if trace.public? or (@user and @user == trace.user)
227 expires_in 7.days, :private => !trace.public?, :public => trace.public?
228 send_file(trace.large_picture_name, :filename => "#{trace.id}.gif", :type => 'image/gif', :disposition => 'inline')
230 render :text => "", :status => :forbidden
233 render :text => "", :status => :not_found
235 rescue ActiveRecord::RecordNotFound
236 render :text => "", :status => :not_found
240 trace = Trace.find(params[:id])
243 if trace.public? or (@user and @user == trace.user)
244 expires_in 7.days, :private => !trace.public?, :public => trace.public?
245 send_file(trace.icon_picture_name, :filename => "#{trace.id}_icon.gif", :type => 'image/gif', :disposition => 'inline')
247 render :text => "", :status => :forbidden
250 render :text => "", :status => :not_found
252 rescue ActiveRecord::RecordNotFound
253 render :text => "", :status => :not_found
257 trace = Trace.visible.find(params[:id])
259 if trace.public? or trace.user == @user
260 render :text => trace.to_xml.to_s, :content_type => "text/xml"
262 render :text => "", :status => :forbidden
267 trace = Trace.visible.find(params[:id])
269 if trace.user == @user
270 new_trace = Trace.from_xml(request.raw_post)
272 unless new_trace and new_trace.id == trace.id
273 raise OSM::APIBadUserInput.new("The id in the url (#{trace.id}) is not the same as provided in the xml (#{new_trace.id})")
276 trace.description = new_trace.description
277 trace.tags = new_trace.tags
278 trace.visibility = new_trace.visibility
281 render :text => "", :status => :ok
283 render :text => "", :status => :forbidden
288 trace = Trace.visible.find(params[:id])
290 if trace.user == @user
291 trace.visible = false
294 render :text => "", :status => :ok
296 render :text => "", :status => :forbidden
301 trace = Trace.find(params[:id])
303 if trace.public? or trace.user == @user
304 if request.format == Mime::XML or request.format == Mime::GPX
305 send_file(trace.xml_file, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => 'attachment')
307 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => 'attachment')
310 render :text => "", :status => :forbidden
315 tags = params[:tags] || ""
316 description = params[:description] || ""
317 visibility = params[:visibility]
320 if params[:public] && params[:public].to_i.nonzero?
321 visibility = "public"
323 visibility = "private"
327 if params[:file].respond_to?(:read)
328 do_create(params[:file], tags, description, visibility)
331 render :text => @trace.id.to_s, :content_type => "text/plain"
333 render :text => "", :status => :internal_server_error
335 render :text => "", :status => :bad_request
338 render :text => "", :status => :bad_request
344 def do_create(file, tags, description, visibility)
345 # Sanitise the user's filename
346 name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, '_')
348 # Get a temporary filename...
349 filename = "/tmp/#{rand}"
351 # ...and save the uploaded file to that location
352 File.open(filename, "wb") { |f| f.write(file.read) }
354 # Create the trace object, falsely marked as already
355 # inserted to stop the import daemon trying to load it
359 :description => description,
360 :visibility => visibility,
363 :timestamp => Time.now.getutc
368 # Save the trace object
371 # Rename the temporary file to the final name
372 FileUtils.mv(filename, @trace.trace_name)
373 rescue Exception => ex
374 # Remove the file as we have failed to update the database
375 FileUtils.rm_f(filename)
377 # Pass the exception on
382 # Clear the inserted flag to make the import daemon load the trace
383 @trace.inserted = false
385 rescue Exception => ex
386 # Remove the file as we have failed to update the database
387 FileUtils.rm_f(@trace.trace_name)
389 # Pass the exception on
394 # Finally save the user's preferred privacy level
395 if pref = @user.preferences.where(:k => "gps.trace.visibility").first
399 @user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
405 flash.now[:warning] = t 'trace.offline_warning.message' if STATUS == :gpx_offline
409 redirect_to :action => :offline if STATUS == :gpx_offline
412 def default_visibility
413 visibility = @user.preferences.where(:k => "gps.trace.visibility").first
417 elsif @user.preferences.where(:k => "gps.trace.public", :v => "default").first.nil?