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"
94 @title = t ".upload_trace"
95 @trace = Trace.new(:visibility => default_visibility)
99 @title = t ".upload_trace"
101 logger.info(params[:trace][:gpx_file].class.name)
103 if params[:trace][:gpx_file].respond_to?(:read)
105 @trace = do_create(params[:trace][:gpx_file], params[:trace][:tagstring],
106 params[:trace][:description], params[:trace][:visibility])
107 rescue StandardError => e
112 flash[:notice] = t ".trace_uploaded"
113 flash[:warning] = t ".traces_waiting", :count => current_user.traces.where(:inserted => false).count if current_user.traces.where(:inserted => false).count > 4
115 TraceImporterJob.perform_later(@trace) if Settings.trace_use_job_queue
116 redirect_to :action => :index, :display_name => current_user.display_name
118 flash[:error] = t("traces.create.upload_failed") if @trace.valid?
120 render :action => "new"
123 @trace = Trace.new(:name => "Dummy",
124 :tagstring => params[:trace][:tagstring],
125 :description => params[:trace][:description],
126 :visibility => params[:trace][:visibility],
127 :inserted => false, :user => current_user,
128 :timestamp => Time.now.getutc)
130 @trace.errors.add(:gpx_file, "can't be blank")
132 render :action => "new"
137 trace = Trace.find(params[:id])
139 if trace.visible? && (trace.public? || (current_user && current_user == trace.user))
140 if Acl.no_trace_download(request.remote_ip)
142 elsif request.format == Mime[:xml]
143 send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
144 elsif request.format == Mime[:gpx]
145 send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
147 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
152 rescue ActiveRecord::RecordNotFound
157 @trace = Trace.find(params[:id])
161 elsif current_user.nil? || @trace.user != current_user
164 @title = t ".title", :name => @trace.name
166 rescue ActiveRecord::RecordNotFound
171 @trace = Trace.find(params[:id])
175 elsif current_user.nil? || @trace.user != current_user
177 elsif @trace.update(trace_params)
178 flash[:notice] = t ".updated"
179 redirect_to :action => "show", :display_name => current_user.display_name
181 @title = t ".title", :name => @trace.name
182 render :action => "edit"
184 rescue ActiveRecord::RecordNotFound
189 trace = Trace.find(params[:id])
193 elsif current_user.nil? || (trace.user != current_user && !current_user.administrator? && !current_user.moderator?)
196 trace.visible = false
198 flash[:notice] = t ".scheduled_for_deletion"
199 TraceDestroyerJob.perform_later(trace) if Settings.trace_use_job_queue
200 redirect_to :action => :index, :display_name => trace.user.display_name
202 rescue ActiveRecord::RecordNotFound
207 @traces = Trace.visible_to_all.visible
209 @traces = @traces.joins(:user).where(:users => { :display_name => params[:display_name] }) if params[:display_name]
211 @traces = @traces.tagged(params[:tag]) if params[:tag]
212 @traces = @traces.order("timestamp DESC")
213 @traces = @traces.limit(20)
214 @traces = @traces.includes(:user)
218 trace = Trace.find(params[:id])
220 if trace.visible? && trace.inserted?
221 if trace.public? || (current_user && current_user == trace.user)
222 expires_in 7.days, :private => !trace.public?, :public => trace.public?
223 send_file(trace.large_picture_name, :filename => "#{trace.id}.gif", :type => "image/gif", :disposition => "inline")
230 rescue ActiveRecord::RecordNotFound
235 trace = Trace.find(params[:id])
237 if trace.visible? && trace.inserted?
238 if trace.public? || (current_user && current_user == trace.user)
239 expires_in 7.days, :private => !trace.public?, :public => trace.public?
240 send_file(trace.icon_picture_name, :filename => "#{trace.id}_icon.gif", :type => "image/gif", :disposition => "inline")
247 rescue ActiveRecord::RecordNotFound
253 def do_create(file, tags, description, visibility)
254 # Sanitise the user's filename
255 name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
257 # Get a temporary filename...
258 filename = "/tmp/#{rand}"
260 # ...and save the uploaded file to that location
261 File.open(filename, "wb") { |f| f.write(file.read) }
263 # Create the trace object, falsely marked as already
264 # inserted to stop the import daemon trying to load it
268 :description => description,
269 :visibility => visibility,
271 :user => current_user,
272 :timestamp => Time.now.getutc
278 # Save the trace object
281 # Rename the temporary file to the final name
282 FileUtils.mv(filename, trace.trace_name)
284 # Remove the file as we have failed to update the database
285 FileUtils.rm_f(filename)
287 # Pass the exception on
292 # Clear the inserted flag to make the import daemon load the trace
293 trace.inserted = false
296 # Remove the file as we have failed to update the database
297 FileUtils.rm_f(trace.trace_name)
299 # Pass the exception on
305 # Finally save the user's preferred privacy level
306 if pref = current_user.preferences.where(:k => "gps.trace.visibility").first
310 current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
317 flash.now[:warning] = t "traces.offline_warning.message" if Settings.status == "gpx_offline"
321 logger.info "offline_redirect status is #{Settings.status}"
322 render :action => :offline if Settings.status == "gpx_offline"
325 def default_visibility
326 visibility = current_user.preferences.where(:k => "gps.trace.visibility").first
330 elsif current_user.preferences.where(:k => "gps.trace.public", :v => "default").first.nil?
338 params.require(:trace).permit(:description, :tagstring, :visibility)