1 class TracesController < ApplicationController
2 layout "site", :except => :georss
4 skip_before_action :verify_authenticity_token, :only => [:api_create, :api_read, :api_update, :api_delete, :api_data]
5 before_action :authorize_web
6 before_action :set_locale
7 before_action :require_user, :only => [:mine, :create, :edit, :delete]
8 before_action :authorize, :only => [:api_create, :api_read, :api_update, :api_delete, :api_data]
9 before_action :check_database_readable, :except => [:api_read, :api_data]
10 before_action :check_database_writable, :only => [:create, :edit, :delete, :api_create, :api_update, :api_delete]
11 before_action :check_api_readable, :only => [:api_read, :api_data]
12 before_action :check_api_writable, :only => [:api_create, :api_update, :api_delete]
13 before_action :require_allow_read_gpx, :only => [:api_read, :api_data]
14 before_action :require_allow_write_gpx, :only => [:api_create, :api_update, :api_delete]
15 before_action :offline_warning, :only => [:mine, :view]
16 before_action :offline_redirect, :only => [:create, :edit, :delete, :data, :api_create, :api_delete, :api_data]
17 around_action :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.present?
25 target_user = User.active.where(:display_name => display_name).first
27 render_unknown_user display_name
33 @title = if target_user.nil?
35 elsif current_user && current_user == target_user
38 t ".public_traces_from", :user => target_user.display_name
41 @title += t ".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 @traces = if target_user.nil? # all traces
50 Trace.visible_to(current_user) # 1
52 Trace.visible_to_all # 2
54 elsif current_user && current_user == target_user
55 current_user.traces # 3 (check vs user id, so no join + can't pick up non-public traces by changing name)
57 target_user.traces.visible_to_all # 4
60 @traces = @traces.tagged(params[:tag]) if params[:tag]
62 @params = params.permit(:display_name, :tag)
64 @page = (params[:page] || 1).to_i
67 @traces = @traces.visible
68 @traces = @traces.order(:id => :desc)
69 @traces = @traces.offset((@page - 1) * @page_size)
70 @traces = @traces.limit(@page_size)
71 @traces = @traces.includes(:user, :tags)
73 # put together SET of tags across traces, for related links
75 @traces.each do |trace|
76 trace.tags.reload if params[:tag] # if searched by tag, ActiveRecord won't bring back other tags, so do explicitly here
77 trace.tags.each do |tag|
78 tagset[tag.tag] = tag.tag
82 # final helper vars for view
83 @target_user = target_user
84 @display_name = target_user.display_name if target_user
85 @all_tags = tagset.values
89 redirect_to :action => :list, :display_name => current_user.display_name
93 @trace = Trace.find(params[:id])
95 if @trace && @trace.visible? &&
96 (@trace.public? || @trace.user == current_user)
97 @title = t ".title", :name => @trace.name
99 flash[:error] = t ".trace_not_found"
100 redirect_to :action => "list"
102 rescue ActiveRecord::RecordNotFound
103 flash[:error] = t ".trace_not_found"
104 redirect_to :action => "list"
109 logger.info(params[:trace][:gpx_file].class.name)
111 if params[:trace][:gpx_file].respond_to?(:read)
113 do_create(params[:trace][:gpx_file], params[:trace][:tagstring],
114 params[:trace][:description], params[:trace][:visibility])
115 rescue StandardError => ex
120 flash[:notice] = t ".trace_uploaded"
121 flash[:warning] = t ".traces_waiting", :count => current_user.traces.where(:inserted => false).count if current_user.traces.where(:inserted => false).count > 4
123 redirect_to :action => :list, :display_name => current_user.display_name
126 @trace = Trace.new(:name => "Dummy",
127 :tagstring => params[:trace][:tagstring],
128 :description => params[:trace][:description],
129 :visibility => params[:trace][:visibility],
130 :inserted => false, :user => current_user,
131 :timestamp => Time.now.getutc)
133 @trace.errors.add(:gpx_file, "can't be blank")
136 @trace = Trace.new(:visibility => default_visibility)
139 @title = t ".upload_trace"
143 trace = Trace.find(params[:id])
145 if trace.visible? && (trace.public? || (current_user && current_user == trace.user))
146 if Acl.no_trace_download(request.remote_ip)
148 elsif request.format == Mime[:xml]
149 send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
150 elsif request.format == Mime[:gpx]
151 send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
153 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
158 rescue ActiveRecord::RecordNotFound
163 @trace = Trace.find(params[:id])
167 elsif current_user.nil? || @trace.user != current_user
170 @title = t ".title", :name => @trace.name
172 if request.post? && params[:trace]
173 @trace.description = params[:trace][:description]
174 @trace.tagstring = params[:trace][:tagstring]
175 @trace.visibility = params[:trace][:visibility]
176 redirect_to :action => "view", :display_name => current_user.display_name if @trace.save
179 rescue ActiveRecord::RecordNotFound
184 trace = Trace.find(params[:id])
188 elsif current_user.nil? || (trace.user != current_user && !current_user.administrator? && !current_user.moderator?)
191 trace.visible = false
193 flash[:notice] = t ".scheduled_for_deletion"
194 redirect_to :action => :list, :display_name => trace.user.display_name
196 rescue ActiveRecord::RecordNotFound
201 @traces = Trace.visible_to_all.visible
203 @traces = @traces.joins(:user).where(:users => { :display_name => params[:display_name] }) if params[:display_name]
205 @traces = @traces.tagged(params[:tag]) if params[:tag]
206 @traces = @traces.order("timestamp DESC")
207 @traces = @traces.limit(20)
208 @traces = @traces.includes(:user)
212 trace = Trace.find(params[:id])
214 if trace.visible? && trace.inserted?
215 if trace.public? || (current_user && current_user == trace.user)
216 expires_in 7.days, :private => !trace.public?, :public => trace.public?
217 send_file(trace.large_picture_name, :filename => "#{trace.id}.gif", :type => "image/gif", :disposition => "inline")
224 rescue ActiveRecord::RecordNotFound
229 trace = Trace.find(params[:id])
231 if trace.visible? && trace.inserted?
232 if trace.public? || (current_user && current_user == trace.user)
233 expires_in 7.days, :private => !trace.public?, :public => trace.public?
234 send_file(trace.icon_picture_name, :filename => "#{trace.id}_icon.gif", :type => "image/gif", :disposition => "inline")
241 rescue ActiveRecord::RecordNotFound
246 trace = Trace.visible.find(params[:id])
248 if trace.public? || trace.user == current_user
249 render :xml => trace.to_xml.to_s
256 trace = Trace.visible.find(params[:id])
258 if trace.user == current_user
259 trace.update_from_xml(request.raw_post)
269 trace = Trace.visible.find(params[:id])
271 if trace.user == current_user
272 trace.visible = false
282 trace = Trace.visible.find(params[:id])
284 if trace.public? || trace.user == current_user
285 if request.format == Mime[:xml]
286 send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
287 elsif request.format == Mime[:gpx]
288 send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
290 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
298 tags = params[:tags] || ""
299 description = params[:description] || ""
300 visibility = params[:visibility]
303 visibility = if params[:public] && params[:public].to_i.nonzero?
310 if params[:file].respond_to?(:read)
311 do_create(params[:file], tags, description, visibility)
314 render :plain => @trace.id.to_s
316 head :internal_server_error
327 def do_create(file, tags, description, visibility)
328 # Sanitise the user's filename
329 name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
331 # Get a temporary filename...
332 filename = "/tmp/#{rand}"
334 # ...and save the uploaded file to that location
335 File.open(filename, "wb") { |f| f.write(file.read) }
337 # Create the trace object, falsely marked as already
338 # inserted to stop the import daemon trying to load it
342 :description => description,
343 :visibility => visibility,
345 :user => current_user,
346 :timestamp => Time.now.getutc
351 # Save the trace object
354 # Rename the temporary file to the final name
355 FileUtils.mv(filename, @trace.trace_name)
357 # Remove the file as we have failed to update the database
358 FileUtils.rm_f(filename)
360 # Pass the exception on
365 # Clear the inserted flag to make the import daemon load the trace
366 @trace.inserted = false
369 # Remove the file as we have failed to update the database
370 FileUtils.rm_f(@trace.trace_name)
372 # Pass the exception on
377 # Finally save the user's preferred privacy level
378 if pref = current_user.preferences.where(:k => "gps.trace.visibility").first
382 current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
387 flash.now[:warning] = t "traces.offline_warning.message" if STATUS == :gpx_offline
391 redirect_to :action => :offline if STATUS == :gpx_offline
394 def default_visibility
395 visibility = current_user.preferences.where(:k => "gps.trace.visibility").first
399 elsif current_user.preferences.where(:k => "gps.trace.public", :v => "default").first.nil?