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
73 redirect_to :action => :index, :display_name => current_user.display_name
77 @trace = Trace.find(params[:id])
79 if @trace&.visible? &&
80 (@trace&.public? || @trace&.user == current_user)
81 @title = t ".title", :name => @trace.name
83 flash[:error] = t ".trace_not_found"
84 redirect_to :action => "index"
86 rescue ActiveRecord::RecordNotFound
87 flash[:error] = t ".trace_not_found"
88 redirect_to :action => "index"
92 @title = t ".upload_trace"
93 @trace = Trace.new(:visibility => default_visibility)
97 @title = t ".upload_trace"
99 logger.info(params[:trace][:gpx_file].class.name)
101 if params[:trace][:gpx_file].respond_to?(:read)
103 @trace = do_create(params[:trace][:gpx_file], params[:trace][:tagstring],
104 params[:trace][:description], params[:trace][:visibility])
105 rescue StandardError => e
110 flash[:notice] = t ".trace_uploaded"
111 flash[:warning] = t ".traces_waiting", :count => current_user.traces.where(:inserted => false).count if current_user.traces.where(:inserted => false).count > 4
113 TraceImporterJob.perform_later(@trace)
114 redirect_to :action => :index, :display_name => current_user.display_name
116 flash[:error] = t("traces.create.upload_failed") if @trace.valid?
118 render :action => "new"
121 @trace = Trace.new(:name => "Dummy",
122 :tagstring => params[:trace][:tagstring],
123 :description => params[:trace][:description],
124 :visibility => params[:trace][:visibility],
125 :inserted => false, :user => current_user,
126 :timestamp => Time.now.getutc)
128 @trace.errors.add(:gpx_file, "can't be blank")
130 render :action => "new"
135 trace = Trace.find(params[:id])
137 if trace.visible? && (trace.public? || (current_user && current_user == trace.user))
138 if Acl.no_trace_download(request.remote_ip)
140 elsif request.format == Mime[:xml]
141 send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
142 elsif request.format == Mime[:gpx]
143 send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
145 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
150 rescue ActiveRecord::RecordNotFound
155 @trace = Trace.find(params[:id])
159 elsif current_user.nil? || @trace.user != current_user
162 @title = t ".title", :name => @trace.name
164 rescue ActiveRecord::RecordNotFound
169 @trace = Trace.find(params[:id])
173 elsif current_user.nil? || @trace.user != current_user
175 elsif @trace.update(trace_params)
176 flash[:notice] = t ".updated"
177 redirect_to :action => "show", :display_name => current_user.display_name
179 @title = t ".title", :name => @trace.name
180 render :action => "edit"
182 rescue ActiveRecord::RecordNotFound
187 trace = Trace.find(params[:id])
191 elsif current_user.nil? || (trace.user != current_user && !current_user.administrator? && !current_user.moderator?)
194 trace.visible = false
196 flash[:notice] = t ".scheduled_for_deletion"
197 TraceDestroyerJob.perform_later(trace)
198 redirect_to :action => :index, :display_name => trace.user.display_name
200 rescue ActiveRecord::RecordNotFound
205 @traces = Trace.visible_to_all.visible
207 @traces = @traces.joins(:user).where(:users => { :display_name => params[:display_name] }) if params[:display_name]
209 @traces = @traces.tagged(params[:tag]) if params[:tag]
210 @traces = @traces.order("timestamp DESC")
211 @traces = @traces.limit(20)
212 @traces = @traces.includes(:user)
216 trace = Trace.find(params[:id])
218 if trace.visible? && trace.inserted?
219 if trace.public? || (current_user && current_user == trace.user)
220 expires_in 7.days, :private => !trace.public?, :public => trace.public?
221 send_file(trace.large_picture_name, :filename => "#{trace.id}.gif", :type => "image/gif", :disposition => "inline")
228 rescue ActiveRecord::RecordNotFound
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.icon_picture_name, :filename => "#{trace.id}_icon.gif", :type => "image/gif", :disposition => "inline")
245 rescue ActiveRecord::RecordNotFound
251 def do_create(file, tags, description, visibility)
252 # Sanitise the user's filename
253 name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
255 # Get a temporary filename...
256 filename = "/tmp/#{rand}"
258 # ...and save the uploaded file to that location
259 File.open(filename, "wb") { |f| f.write(file.read) }
261 # Create the trace object, falsely marked as already
262 # inserted to stop the import daemon trying to load it
266 :description => description,
267 :visibility => visibility,
269 :user => current_user,
270 :timestamp => Time.now.getutc
276 # Save the trace object
279 # Rename the temporary file to the final name
280 FileUtils.mv(filename, trace.trace_name)
282 # Remove the file as we have failed to update the database
283 FileUtils.rm_f(filename)
285 # Pass the exception on
290 # Clear the inserted flag to make the import daemon load the trace
291 trace.inserted = false
294 # Remove the file as we have failed to update the database
295 FileUtils.rm_f(trace.trace_name)
297 # Pass the exception on
303 # Finally save the user's preferred privacy level
304 if pref = current_user.preferences.where(:k => "gps.trace.visibility").first
308 current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
315 flash.now[:warning] = t "traces.offline_warning.message" if Settings.status == "gpx_offline"
319 render :action => :offline if Settings.status == "gpx_offline"
322 def default_visibility
323 visibility = current_user.preferences.where(:k => "gps.trace.visibility").first
327 elsif current_user.preferences.where(:k => "gps.trace.public", :v => "default").first.nil?
335 params.require(:trace).permit(:description, :tagstring, :visibility)