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 traces = traces.visible
59 @params = params.permit(:display_name, :tag, :before, :after)
61 @traces = if params[:before]
62 traces.where("gpx_files.id < ?", params[:before]).order(:id => :desc)
64 traces.where("gpx_files.id > ?", params[:after]).order(:id => :asc)
66 traces.order(:id => :desc)
69 @traces = @traces.limit(20)
70 @traces = @traces.includes(:user, :tags)
71 @traces = @traces.sort.reverse
73 @newer_traces = @traces.count.positive? && traces.exists?(["gpx_files.id > ?", @traces.first.id])
74 @older_traces = @traces.count.positive? && traces.exists?(["gpx_files.id < ?", @traces.last.id])
76 # final helper vars for view
77 @target_user = target_user
81 @trace = Trace.find(params[:id])
83 if @trace&.visible? &&
84 (@trace&.public? || @trace&.user == current_user)
85 @title = t ".title", :name => @trace.name
87 flash[:error] = t ".trace_not_found"
88 redirect_to :action => "index"
90 rescue ActiveRecord::RecordNotFound
91 flash[:error] = t ".trace_not_found"
92 redirect_to :action => "index"
96 @title = t ".upload_trace"
97 @trace = Trace.new(:visibility => default_visibility)
101 @trace = Trace.find(params[:id])
105 elsif current_user.nil? || @trace.user != current_user
108 @title = t ".title", :name => @trace.name
110 rescue ActiveRecord::RecordNotFound
115 @title = t ".upload_trace"
117 logger.info(params[:trace][:gpx_file].class.name)
119 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])
124 flash[:notice] = t ".trace_uploaded"
125 flash[:warning] = t ".traces_waiting", :count => current_user.traces.where(:inserted => false).count if current_user.traces.where(:inserted => false).count > 4
127 TraceImporterJob.perform_later(@trace)
128 redirect_to :action => :index, :display_name => current_user.display_name
130 flash[:error] = t(".upload_failed") if @trace.valid?
132 render :action => "new"
135 @trace = Trace.new(:name => "Dummy",
136 :tagstring => params[:trace][:tagstring],
137 :description => params[:trace][:description],
138 :visibility => params[:trace][:visibility],
139 :inserted => false, :user => current_user,
140 :timestamp => Time.now.utc)
142 @trace.errors.add(:gpx_file, "can't be blank")
144 render :action => "new"
149 @trace = Trace.find(params[:id])
153 elsif current_user.nil? || @trace.user != current_user
155 elsif @trace.update(trace_params)
156 flash[:notice] = t ".updated"
157 redirect_to :action => "show", :display_name => current_user.display_name
159 @title = t ".title", :name => @trace.name
160 render :action => "edit"
162 rescue ActiveRecord::RecordNotFound
167 trace = Trace.find(params[:id])
171 elsif current_user.nil? || (trace.user != current_user && !current_user.administrator? && !current_user.moderator?)
174 trace.visible = false
176 flash[:notice] = t ".scheduled_for_deletion"
177 TraceDestroyerJob.perform_later(trace)
178 redirect_to :action => :index, :display_name => trace.user.display_name
180 rescue ActiveRecord::RecordNotFound
185 redirect_to :action => :index, :display_name => current_user.display_name
189 trace = Trace.find(params[:id])
191 if trace.visible? && (trace.public? || (current_user && current_user == trace.user))
192 if Acl.no_trace_download(request.remote_ip)
194 elsif request.format == Mime[:xml]
195 send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
196 elsif request.format == Mime[:gpx]
197 send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
198 elsif trace.file.attached?
199 redirect_to rails_blob_path(trace.file, :disposition => "attachment")
201 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
206 rescue ActiveRecord::RecordNotFound
211 @traces = Trace.visible_to_all.visible
213 @traces = @traces.joins(:user).where(:users => { :display_name => params[:display_name] }) if params[:display_name]
215 @traces = @traces.tagged(params[:tag]) if params[:tag]
216 @traces = @traces.order("timestamp DESC")
217 @traces = @traces.limit(20)
218 @traces = @traces.includes(:user)
222 trace = Trace.find(params[:id])
224 if trace.visible? && trace.inserted?
225 if trace.public? || (current_user && current_user == trace.user)
226 if trace.icon.attached?
227 redirect_to rails_blob_path(trace.image, :disposition => "inline")
229 expires_in 7.days, :private => !trace.public?, :public => trace.public?
230 send_file(trace.large_picture_name, :filename => "#{trace.id}.gif", :type => "image/gif", :disposition => "inline")
238 rescue ActiveRecord::RecordNotFound
243 trace = Trace.find(params[:id])
245 if trace.visible? && trace.inserted?
246 if trace.public? || (current_user && current_user == trace.user)
247 if trace.icon.attached?
248 redirect_to rails_blob_path(trace.icon, :disposition => "inline")
250 expires_in 7.days, :private => !trace.public?, :public => trace.public?
251 send_file(trace.icon_picture_name, :filename => "#{trace.id}_icon.gif", :type => "image/gif", :disposition => "inline")
259 rescue ActiveRecord::RecordNotFound
265 def do_create(file, tags, description, visibility)
266 # Sanitise the user's filename
267 name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
269 # Create the trace object
273 :description => description,
274 :visibility => visibility,
276 :user => current_user,
277 :timestamp => Time.now.utc,
281 # Save the trace object
283 # Finally save the user's preferred privacy level
284 if pref = current_user.preferences.where(:k => "gps.trace.visibility").first
288 current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
296 flash.now[:warning] = t "traces.offline_warning.message" if Settings.status == "gpx_offline"
300 render :action => :offline if Settings.status == "gpx_offline"
303 def default_visibility
304 visibility = current_user.preferences.where(:k => "gps.trace.visibility").first
308 elsif current_user.preferences.where(:k => "gps.trace.public", :v => "default").first.nil?
316 params.require(:trace).permit(:description, :tagstring, :visibility)