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, :delete]
11 before_action :offline_warning, :only => [:mine, :show]
12 before_action :offline_redirect, :only => [:new, :create, :edit, :delete, :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 # put together SET of tags across traces, for related links
70 @traces.each do |trace|
71 trace.tags.reload if params[:tag] # if searched by tag, ActiveRecord won't bring back other tags, so do explicitly here
72 trace.tags.each do |tag|
73 tagset[tag.tag] = tag.tag
77 # final helper vars for view
78 @target_user = target_user
79 @display_name = target_user.display_name if target_user
80 @all_tags = tagset.values
84 redirect_to :action => :index, :display_name => current_user.display_name
88 @trace = Trace.find(params[:id])
90 if @trace&.visible? &&
91 (@trace&.public? || @trace&.user == current_user)
92 @title = t ".title", :name => @trace.name
94 flash[:error] = t ".trace_not_found"
95 redirect_to :action => "index"
97 rescue ActiveRecord::RecordNotFound
98 flash[:error] = t ".trace_not_found"
99 redirect_to :action => "index"
103 @title = t ".upload_trace"
104 @trace = Trace.new(:visibility => default_visibility)
108 @title = t ".upload_trace"
110 logger.info(params[:trace][:gpx_file].class.name)
112 if params[:trace][:gpx_file].respond_to?(:read)
114 @trace = do_create(params[:trace][:gpx_file], params[:trace][:tagstring],
115 params[:trace][:description], params[:trace][:visibility])
116 rescue StandardError => e
121 flash[:notice] = t ".trace_uploaded"
122 flash[:warning] = t ".traces_waiting", :count => current_user.traces.where(:inserted => false).count if current_user.traces.where(:inserted => false).count > 4
124 TraceImporterJob.perform_later(@trace) if Settings.trace_use_job_queue
125 redirect_to :action => :index, :display_name => current_user.display_name
127 flash[:error] = t("traces.create.upload_failed") if @trace.valid?
129 render :action => "new"
132 @trace = Trace.new(:name => "Dummy",
133 :tagstring => params[:trace][:tagstring],
134 :description => params[:trace][:description],
135 :visibility => params[:trace][:visibility],
136 :inserted => false, :user => current_user,
137 :timestamp => Time.now.getutc)
139 @trace.errors.add(:gpx_file, "can't be blank")
141 render :action => "new"
146 trace = Trace.find(params[:id])
148 if trace.visible? && (trace.public? || (current_user && current_user == trace.user))
149 if Acl.no_trace_download(request.remote_ip)
151 elsif request.format == Mime[:xml]
152 send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
153 elsif request.format == Mime[:gpx]
154 send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
156 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
161 rescue ActiveRecord::RecordNotFound
166 @trace = Trace.find(params[:id])
170 elsif current_user.nil? || @trace.user != current_user
173 @title = t ".title", :name => @trace.name
175 rescue ActiveRecord::RecordNotFound
180 @trace = Trace.find(params[:id])
184 elsif current_user.nil? || @trace.user != current_user
186 elsif @trace.update(trace_params)
187 flash[:notice] = t ".updated"
188 redirect_to :action => "show", :display_name => current_user.display_name
190 @title = t ".title", :name => @trace.name
191 render :action => "edit"
193 rescue ActiveRecord::RecordNotFound
198 trace = Trace.find(params[:id])
202 elsif current_user.nil? || (trace.user != current_user && !current_user.administrator? && !current_user.moderator?)
205 trace.visible = false
207 flash[:notice] = t ".scheduled_for_deletion"
208 TraceDestroyerJob.perform_later(trace) if Settings.trace_use_job_queue
209 redirect_to :action => :index, :display_name => trace.user.display_name
211 rescue ActiveRecord::RecordNotFound
216 @traces = Trace.visible_to_all.visible
218 @traces = @traces.joins(:user).where(:users => { :display_name => params[:display_name] }) if params[:display_name]
220 @traces = @traces.tagged(params[:tag]) if params[:tag]
221 @traces = @traces.order("timestamp DESC")
222 @traces = @traces.limit(20)
223 @traces = @traces.includes(:user)
227 trace = Trace.find(params[:id])
229 if trace.visible? && trace.inserted?
230 if trace.public? || (current_user && current_user == trace.user)
231 expires_in 7.days, :private => !trace.public?, :public => trace.public?
232 send_file(trace.large_picture_name, :filename => "#{trace.id}.gif", :type => "image/gif", :disposition => "inline")
239 rescue ActiveRecord::RecordNotFound
244 trace = Trace.find(params[:id])
246 if trace.visible? && trace.inserted?
247 if trace.public? || (current_user && current_user == trace.user)
248 expires_in 7.days, :private => !trace.public?, :public => trace.public?
249 send_file(trace.icon_picture_name, :filename => "#{trace.id}_icon.gif", :type => "image/gif", :disposition => "inline")
256 rescue ActiveRecord::RecordNotFound
262 def do_create(file, tags, description, visibility)
263 # Sanitise the user's filename
264 name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
266 # Get a temporary filename...
267 filename = "/tmp/#{rand}"
269 # ...and save the uploaded file to that location
270 File.open(filename, "wb") { |f| f.write(file.read) }
272 # Create the trace object, falsely marked as already
273 # inserted to stop the import daemon trying to load it
277 :description => description,
278 :visibility => visibility,
280 :user => current_user,
281 :timestamp => Time.now.getutc
287 # Save the trace object
290 # Rename the temporary file to the final name
291 FileUtils.mv(filename, trace.trace_name)
293 # Remove the file as we have failed to update the database
294 FileUtils.rm_f(filename)
296 # Pass the exception on
301 # Clear the inserted flag to make the import daemon load the trace
302 trace.inserted = false
305 # Remove the file as we have failed to update the database
306 FileUtils.rm_f(trace.trace_name)
308 # Pass the exception on
314 # Finally save the user's preferred privacy level
315 if pref = current_user.preferences.where(:k => "gps.trace.visibility").first
319 current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
326 flash.now[:warning] = t "traces.offline_warning.message" if Settings.status == "gpx_offline"
330 redirect_to :action => :offline if Settings.status == "gpx_offline"
333 def default_visibility
334 visibility = current_user.preferences.where(:k => "gps.trace.visibility").first
338 elsif current_user.preferences.where(:k => "gps.trace.public", :v => "default").first.nil?
346 params.require(:trace).permit(:description, :tagstring, :visibility)