1 class TracesController < ApplicationController
2 layout "site", :except => :georss
4 before_action :authorize_web
5 before_action :set_locale
6 before_action :check_database_readable
10 before_action :check_database_writable, :only => [:new, :create, :edit, :destroy]
11 before_action :offline_warning, :only => [:mine, :show]
12 before_action :offline_redirect, :only => [:new, :create, :edit, :destroy, :data]
14 # Counts and selects pages of GPX traces for various criteria (by user, tags, public etc.).
15 # target_user - if set, specifies the user to fetch traces for. if not set will fetch all traces
17 # from display name, pick up user id if one user's traces only
18 display_name = params[:display_name]
19 if display_name.present?
20 target_user = User.active.where(:display_name => display_name).first
22 render_unknown_user display_name
28 @title = if target_user.nil?
30 elsif current_user && current_user == target_user
33 t ".public_traces_from", :user => target_user.display_name
36 @title += t ".tagged_with", :tags => params[:tag] if params[:tag]
39 # 1 - all traces, logged in = all public traces + all user's (i.e + all mine)
40 # 2 - all traces, not logged in = all public traces
41 # 3 - user's traces, logged in as same user = all user's traces
42 # 4 - user's traces, not logged in as that user = all user's public traces
43 @traces = if target_user.nil? # all traces
45 Trace.visible_to(current_user) # 1
47 Trace.visible_to_all # 2
49 elsif current_user && current_user == target_user
50 current_user.traces # 3 (check vs user id, so no join + can't pick up non-public traces by changing name)
52 target_user.traces.visible_to_all # 4
55 @traces = @traces.tagged(params[:tag]) if params[:tag]
57 @params = params.permit(:display_name, :tag)
59 @page = (params[:page] || 1).to_i
62 @traces = @traces.visible
63 @traces = @traces.order(:id => :desc)
64 @traces = @traces.offset((@page - 1) * @page_size)
65 @traces = @traces.limit(@page_size)
66 @traces = @traces.includes(:user, :tags)
68 # final helper vars for view
69 @target_user = target_user
70 @display_name = target_user.display_name if target_user
74 redirect_to :action => :index, :display_name => current_user.display_name
78 @trace = Trace.find(params[:id])
80 if @trace&.visible? &&
81 (@trace&.public? || @trace&.user == current_user)
82 @title = t ".title", :name => @trace.name
84 flash[:error] = t ".trace_not_found"
85 redirect_to :action => "index"
87 rescue ActiveRecord::RecordNotFound
88 flash[:error] = t ".trace_not_found"
89 redirect_to :action => "index"
93 @title = t ".upload_trace"
94 @trace = Trace.new(:visibility => default_visibility)
98 @title = t ".upload_trace"
100 logger.info(params[:trace][:gpx_file].class.name)
102 if params[:trace][:gpx_file].respond_to?(:read)
104 @trace = do_create(params[:trace][:gpx_file], params[:trace][:tagstring],
105 params[:trace][:description], params[:trace][:visibility])
106 rescue StandardError => e
111 flash[:notice] = t ".trace_uploaded"
112 flash[:warning] = t ".traces_waiting", :count => current_user.traces.where(:inserted => false).count if current_user.traces.where(:inserted => false).count > 4
114 TraceImporterJob.perform_later(@trace) if Settings.trace_use_job_queue
115 redirect_to :action => :index, :display_name => current_user.display_name
117 flash[:error] = t("traces.create.upload_failed") if @trace.valid?
119 render :action => "new"
122 @trace = Trace.new(:name => "Dummy",
123 :tagstring => params[:trace][:tagstring],
124 :description => params[:trace][:description],
125 :visibility => params[:trace][:visibility],
126 :inserted => false, :user => current_user,
127 :timestamp => Time.now.getutc)
129 @trace.errors.add(:gpx_file, "can't be blank")
131 render :action => "new"
136 trace = Trace.find(params[:id])
138 if trace.visible? && (trace.public? || (current_user && current_user == trace.user))
139 if Acl.no_trace_download(request.remote_ip)
141 elsif request.format == Mime[:xml]
142 send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
143 elsif request.format == Mime[:gpx]
144 send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
146 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
151 rescue ActiveRecord::RecordNotFound
156 @trace = Trace.find(params[:id])
160 elsif current_user.nil? || @trace.user != current_user
163 @title = t ".title", :name => @trace.name
165 rescue ActiveRecord::RecordNotFound
170 @trace = Trace.find(params[:id])
174 elsif current_user.nil? || @trace.user != current_user
176 elsif @trace.update(trace_params)
177 flash[:notice] = t ".updated"
178 redirect_to :action => "show", :display_name => current_user.display_name
180 @title = t ".title", :name => @trace.name
181 render :action => "edit"
183 rescue ActiveRecord::RecordNotFound
188 trace = Trace.find(params[:id])
192 elsif current_user.nil? || (trace.user != current_user && !current_user.administrator? && !current_user.moderator?)
195 trace.visible = false
197 flash[:notice] = t ".scheduled_for_deletion"
198 TraceDestroyerJob.perform_later(trace) if Settings.trace_use_job_queue
199 redirect_to :action => :index, :display_name => trace.user.display_name
201 rescue ActiveRecord::RecordNotFound
206 @traces = Trace.visible_to_all.visible
208 @traces = @traces.joins(:user).where(:users => { :display_name => params[:display_name] }) if params[:display_name]
210 @traces = @traces.tagged(params[:tag]) if params[:tag]
211 @traces = @traces.order("timestamp DESC")
212 @traces = @traces.limit(20)
213 @traces = @traces.includes(:user)
217 trace = Trace.find(params[:id])
219 if trace.visible? && trace.inserted?
220 if trace.public? || (current_user && current_user == trace.user)
221 expires_in 7.days, :private => !trace.public?, :public => trace.public?
222 send_file(trace.large_picture_name, :filename => "#{trace.id}.gif", :type => "image/gif", :disposition => "inline")
229 rescue ActiveRecord::RecordNotFound
234 trace = Trace.find(params[:id])
236 if trace.visible? && trace.inserted?
237 if trace.public? || (current_user && current_user == trace.user)
238 expires_in 7.days, :private => !trace.public?, :public => trace.public?
239 send_file(trace.icon_picture_name, :filename => "#{trace.id}_icon.gif", :type => "image/gif", :disposition => "inline")
246 rescue ActiveRecord::RecordNotFound
252 def do_create(file, tags, description, visibility)
253 # Sanitise the user's filename
254 name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
256 # Get a temporary filename...
257 filename = "/tmp/#{rand}"
259 # ...and save the uploaded file to that location
260 File.open(filename, "wb") { |f| f.write(file.read) }
262 # Create the trace object, falsely marked as already
263 # inserted to stop the import daemon trying to load it
267 :description => description,
268 :visibility => visibility,
270 :user => current_user,
271 :timestamp => Time.now.getutc
277 # Save the trace object
280 # Rename the temporary file to the final name
281 FileUtils.mv(filename, trace.trace_name)
283 # Remove the file as we have failed to update the database
284 FileUtils.rm_f(filename)
286 # Pass the exception on
291 # Clear the inserted flag to make the import daemon load the trace
292 trace.inserted = false
295 # Remove the file as we have failed to update the database
296 FileUtils.rm_f(trace.trace_name)
298 # Pass the exception on
304 # Finally save the user's preferred privacy level
305 if pref = current_user.preferences.where(:k => "gps.trace.visibility").first
309 current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
316 flash.now[:warning] = t "traces.offline_warning.message" if Settings.status == "gpx_offline"
320 render :action => :offline if Settings.status == "gpx_offline"
323 def default_visibility
324 visibility = current_user.preferences.where(:k => "gps.trace.visibility").first
328 elsif current_user.preferences.where(:k => "gps.trace.public", :v => "default").first.nil?
336 params.require(:trace).permit(:description, :tagstring, :visibility)