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 redirect_to :action => :index, :display_name => current_user.display_name
132 flash[:error] = t("traces.create.upload_failed") if @trace.valid?
134 render :action => "new"
137 @trace = Trace.new(:name => "Dummy",
138 :tagstring => params[:trace][:tagstring],
139 :description => params[:trace][:description],
140 :visibility => params[:trace][:visibility],
141 :inserted => false, :user => current_user,
142 :timestamp => Time.now.getutc)
144 @trace.errors.add(:gpx_file, "can't be blank")
146 render :action => "new"
151 trace = Trace.find(params[:id])
153 if trace.visible? && (trace.public? || (current_user && current_user == trace.user))
154 if Acl.no_trace_download(request.remote_ip)
156 elsif request.format == Mime[:xml]
157 send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
158 elsif request.format == Mime[:gpx]
159 send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
161 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
166 rescue ActiveRecord::RecordNotFound
171 @trace = Trace.find(params[:id])
175 elsif current_user.nil? || @trace.user != current_user
178 @title = t ".title", :name => @trace.name
180 rescue ActiveRecord::RecordNotFound
185 @trace = Trace.find(params[:id])
189 elsif current_user.nil? || @trace.user != current_user
191 elsif @trace.update(trace_params)
192 flash[:notice] = t ".updated"
193 redirect_to :action => "show", :display_name => current_user.display_name
195 @title = t ".title", :name => @trace.name
196 render :action => "edit"
198 rescue ActiveRecord::RecordNotFound
203 trace = Trace.find(params[:id])
207 elsif current_user.nil? || (trace.user != current_user && !current_user.administrator? && !current_user.moderator?)
210 trace.visible = false
212 flash[:notice] = t ".scheduled_for_deletion"
213 redirect_to :action => :index, :display_name => trace.user.display_name
215 rescue ActiveRecord::RecordNotFound
220 @traces = Trace.visible_to_all.visible
222 @traces = @traces.joins(:user).where(:users => { :display_name => params[:display_name] }) if params[:display_name]
224 @traces = @traces.tagged(params[:tag]) if params[:tag]
225 @traces = @traces.order("timestamp DESC")
226 @traces = @traces.limit(20)
227 @traces = @traces.includes(:user)
231 trace = Trace.find(params[:id])
233 if trace.visible? && trace.inserted?
234 if trace.public? || (current_user && current_user == trace.user)
235 expires_in 7.days, :private => !trace.public?, :public => trace.public?
236 send_file(trace.large_picture_name, :filename => "#{trace.id}.gif", :type => "image/gif", :disposition => "inline")
243 rescue ActiveRecord::RecordNotFound
248 trace = Trace.find(params[:id])
250 if trace.visible? && trace.inserted?
251 if trace.public? || (current_user && current_user == trace.user)
252 expires_in 7.days, :private => !trace.public?, :public => trace.public?
253 send_file(trace.icon_picture_name, :filename => "#{trace.id}_icon.gif", :type => "image/gif", :disposition => "inline")
260 rescue ActiveRecord::RecordNotFound
265 trace = Trace.visible.find(params[:id])
267 if trace.public? || trace.user == current_user
268 render :xml => trace.to_xml.to_s
275 trace = Trace.visible.find(params[:id])
277 if trace.user == current_user
278 trace.update_from_xml(request.raw_post)
288 trace = Trace.visible.find(params[:id])
290 if trace.user == current_user
291 trace.visible = false
301 trace = Trace.visible.find(params[:id])
303 if trace.public? || trace.user == current_user
304 if request.format == Mime[:xml]
305 send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
306 elsif request.format == Mime[:gpx]
307 send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
309 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
317 tags = params[:tags] || ""
318 description = params[:description] || ""
319 visibility = params[:visibility]
322 visibility = if params[:public]&.to_i&.nonzero?
329 if params[:file].respond_to?(:read)
330 trace = do_create(params[:file], tags, description, visibility)
333 render :plain => trace.id.to_s
335 head :internal_server_error
346 def do_create(file, tags, description, visibility)
347 # Sanitise the user's filename
348 name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
350 # Get a temporary filename...
351 filename = "/tmp/#{rand}"
353 # ...and save the uploaded file to that location
354 File.open(filename, "wb") { |f| f.write(file.read) }
356 # Create the trace object, falsely marked as already
357 # inserted to stop the import daemon trying to load it
361 :description => description,
362 :visibility => visibility,
364 :user => current_user,
365 :timestamp => Time.now.getutc
371 # Save the trace object
374 # Rename the temporary file to the final name
375 FileUtils.mv(filename, trace.trace_name)
377 # Remove the file as we have failed to update the database
378 FileUtils.rm_f(filename)
380 # Pass the exception on
385 # Clear the inserted flag to make the import daemon load the trace
386 trace.inserted = false
389 # Remove the file as we have failed to update the database
390 FileUtils.rm_f(trace.trace_name)
392 # Pass the exception on
398 # Finally save the user's preferred privacy level
399 if pref = current_user.preferences.where(:k => "gps.trace.visibility").first
403 current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
410 flash.now[:warning] = t "traces.offline_warning.message" if STATUS == :gpx_offline
414 redirect_to :action => :offline if STATUS == :gpx_offline
417 def default_visibility
418 visibility = current_user.preferences.where(:k => "gps.trace.visibility").first
422 elsif current_user.preferences.where(:k => "gps.trace.public", :v => "default").first.nil?
430 params.require(:trace).permit(:description, :tagstring, :visibility)