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 :authorize, :only => [:api_create, :api_read, :api_update, :api_delete, :api_data]
8 before_action :api_deny_access_handler, :only => [:api_create, :api_read, :api_update, :api_delete, :api_data]
12 before_action :check_database_readable, :except => [:api_read, :api_data]
13 before_action :check_database_writable, :only => [:new, :create, :edit, :delete, :api_create, :api_update, :api_delete]
14 before_action :check_api_readable, :only => [:api_read, :api_data]
15 before_action :check_api_writable, :only => [:api_create, :api_update, :api_delete]
16 before_action :offline_warning, :only => [:mine, :show]
17 before_action :offline_redirect, :only => [:new, :create, :edit, :delete, :data, :api_create, :api_delete, :api_data]
18 around_action :api_call_handle_error, :only => [:api_create, :api_read, :api_update, :api_delete, :api_data]
20 # Counts and selects pages of GPX traces for various criteria (by user, tags, public etc.).
21 # target_user - if set, specifies the user to fetch traces for. if not set will fetch all traces
23 # from display name, pick up user id if one user's traces only
24 display_name = params[:display_name]
25 if display_name.present?
26 target_user = User.active.where(:display_name => display_name).first
28 render_unknown_user display_name
34 @title = if target_user.nil?
36 elsif current_user && current_user == target_user
39 t ".public_traces_from", :user => target_user.display_name
42 @title += t ".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 @traces = if target_user.nil? # all traces
51 Trace.visible_to(current_user) # 1
53 Trace.visible_to_all # 2
55 elsif current_user && current_user == target_user
56 current_user.traces # 3 (check vs user id, so no join + can't pick up non-public traces by changing name)
58 target_user.traces.visible_to_all # 4
61 @traces = @traces.tagged(params[:tag]) if params[:tag]
63 @params = params.permit(:display_name, :tag)
65 @page = (params[:page] || 1).to_i
68 @traces = @traces.visible
69 @traces = @traces.order(:id => :desc)
70 @traces = @traces.offset((@page - 1) * @page_size)
71 @traces = @traces.limit(@page_size)
72 @traces = @traces.includes(:user, :tags)
74 # put together SET of tags across traces, for related links
76 @traces.each do |trace|
77 trace.tags.reload if params[:tag] # if searched by tag, ActiveRecord won't bring back other tags, so do explicitly here
78 trace.tags.each do |tag|
79 tagset[tag.tag] = tag.tag
83 # final helper vars for view
84 @target_user = target_user
85 @display_name = target_user.display_name if target_user
86 @all_tags = tagset.values
90 redirect_to :action => :index, :display_name => current_user.display_name
94 @trace = Trace.find(params[:id])
96 if @trace&.visible? &&
97 (@trace&.public? || @trace&.user == current_user)
98 @title = t ".title", :name => @trace.name
100 flash[:error] = t ".trace_not_found"
101 redirect_to :action => "index"
103 rescue ActiveRecord::RecordNotFound
104 flash[:error] = t ".trace_not_found"
105 redirect_to :action => "index"
109 @title = t ".upload_trace"
110 @trace = Trace.new(:visibility => default_visibility)
114 @title = t ".upload_trace"
116 logger.info(params[:trace][:gpx_file].class.name)
118 if params[:trace][:gpx_file].respond_to?(:read)
120 @trace = do_create(params[:trace][:gpx_file], params[:trace][:tagstring],
121 params[:trace][:description], params[:trace][:visibility])
122 rescue StandardError => ex
127 flash[:notice] = t ".trace_uploaded"
128 flash[:warning] = t ".traces_waiting", :count => current_user.traces.where(:inserted => false).count if current_user.traces.where(:inserted => false).count > 4
130 TraceImporterJob.perform_later(@trace)
131 redirect_to :action => :index, :display_name => current_user.display_name
133 flash[:error] = t("traces.create.upload_failed") if @trace.valid?
135 render :action => "new"
138 @trace = Trace.new(:name => "Dummy",
139 :tagstring => params[:trace][:tagstring],
140 :description => params[:trace][:description],
141 :visibility => params[:trace][:visibility],
142 :inserted => false, :user => current_user,
143 :timestamp => Time.now.getutc)
145 @trace.errors.add(:gpx_file, "can't be blank")
147 render :action => "new"
152 trace = Trace.find(params[:id])
154 if trace.visible? && (trace.public? || (current_user && current_user == trace.user))
155 if Acl.no_trace_download(request.remote_ip)
157 elsif request.format == Mime[:xml]
158 send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
159 elsif request.format == Mime[:gpx]
160 send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
162 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
167 rescue ActiveRecord::RecordNotFound
172 @trace = Trace.find(params[:id])
176 elsif current_user.nil? || @trace.user != current_user
179 @title = t ".title", :name => @trace.name
181 rescue ActiveRecord::RecordNotFound
186 @trace = Trace.find(params[:id])
190 elsif current_user.nil? || @trace.user != current_user
192 elsif @trace.update(trace_params)
193 flash[:notice] = t ".updated"
194 redirect_to :action => "show", :display_name => current_user.display_name
196 @title = t ".title", :name => @trace.name
197 render :action => "edit"
199 rescue ActiveRecord::RecordNotFound
204 trace = Trace.find(params[:id])
208 elsif current_user.nil? || (trace.user != current_user && !current_user.administrator? && !current_user.moderator?)
211 trace.visible = false
213 flash[:notice] = t ".scheduled_for_deletion"
214 TraceDestroyerJob.perform_later(trace)
215 redirect_to :action => :index, :display_name => trace.user.display_name
217 rescue ActiveRecord::RecordNotFound
222 @traces = Trace.visible_to_all.visible
224 @traces = @traces.joins(:user).where(:users => { :display_name => params[:display_name] }) if params[:display_name]
226 @traces = @traces.tagged(params[:tag]) if params[:tag]
227 @traces = @traces.order("timestamp DESC")
228 @traces = @traces.limit(20)
229 @traces = @traces.includes(:user)
233 trace = Trace.find(params[:id])
235 if trace.visible? && trace.inserted?
236 if trace.public? || (current_user && current_user == trace.user)
237 expires_in 7.days, :private => !trace.public?, :public => trace.public?
238 send_file(trace.large_picture_name, :filename => "#{trace.id}.gif", :type => "image/gif", :disposition => "inline")
245 rescue ActiveRecord::RecordNotFound
250 trace = Trace.find(params[:id])
252 if trace.visible? && trace.inserted?
253 if trace.public? || (current_user && current_user == trace.user)
254 expires_in 7.days, :private => !trace.public?, :public => trace.public?
255 send_file(trace.icon_picture_name, :filename => "#{trace.id}_icon.gif", :type => "image/gif", :disposition => "inline")
262 rescue ActiveRecord::RecordNotFound
267 trace = Trace.visible.find(params[:id])
269 if trace.public? || trace.user == current_user
270 render :xml => trace.to_xml.to_s
277 trace = Trace.visible.find(params[:id])
279 if trace.user == current_user
280 trace.update_from_xml(request.raw_post)
290 trace = Trace.visible.find(params[:id])
292 if trace.user == current_user
293 trace.visible = false
303 trace = Trace.visible.find(params[:id])
305 if trace.public? || trace.user == current_user
306 if request.format == Mime[:xml]
307 send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
308 elsif request.format == Mime[:gpx]
309 send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
311 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
319 tags = params[:tags] || ""
320 description = params[:description] || ""
321 visibility = params[:visibility]
324 visibility = if params[:public]&.to_i&.nonzero?
331 if params[:file].respond_to?(:read)
332 trace = do_create(params[:file], tags, description, visibility)
335 render :plain => trace.id.to_s
337 head :internal_server_error
348 def do_create(file, tags, description, visibility)
349 # Sanitise the user's filename
350 name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
352 # Get a temporary filename...
353 filename = "/tmp/#{rand}"
355 # ...and save the uploaded file to that location
356 File.open(filename, "wb") { |f| f.write(file.read) }
358 # Create the trace object, falsely marked as already
359 # inserted to stop the import daemon trying to load it
363 :description => description,
364 :visibility => visibility,
366 :user => current_user,
367 :timestamp => Time.now.getutc
373 # Save the trace object
376 # Rename the temporary file to the final name
377 FileUtils.mv(filename, trace.trace_name)
379 # Remove the file as we have failed to update the database
380 FileUtils.rm_f(filename)
382 # Pass the exception on
387 # Clear the inserted flag to make the import daemon load the trace
388 trace.inserted = false
391 # Remove the file as we have failed to update the database
392 FileUtils.rm_f(trace.trace_name)
394 # Pass the exception on
400 # Finally save the user's preferred privacy level
401 if pref = current_user.preferences.where(:k => "gps.trace.visibility").first
405 current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
412 flash.now[:warning] = t "traces.offline_warning.message" if STATUS == :gpx_offline
416 redirect_to :action => :offline if STATUS == :gpx_offline
419 def default_visibility
420 visibility = current_user.preferences.where(:k => "gps.trace.visibility").first
424 elsif current_user.preferences.where(:k => "gps.trace.public", :v => "default").first.nil?
432 params.require(:trace).permit(:description, :tagstring, :visibility)