1 class TracesController < ApplicationController
4 layout "site", :except => :georss
6 before_action :authorize_web
7 before_action :set_locale
8 before_action :check_database_readable
12 before_action :check_database_writable, :only => [:new, :create, :edit, :destroy]
13 before_action :offline_warning, :only => [:mine, :show]
14 before_action :offline_redirect, :only => [:new, :create, :edit, :destroy, :data]
16 # Counts and selects pages of GPX traces for various criteria (by user, tags, public etc.).
17 # target_user - if set, specifies the user to fetch traces for. if not set will fetch all traces
19 # from display name, pick up user id if one user's traces only
20 display_name = params[:display_name]
21 if display_name.present?
22 target_user = User.active.where(:display_name => display_name).first
24 render_unknown_user display_name
30 @title = if target_user.nil?
32 elsif current_user && current_user == target_user
35 t ".public_traces_from", :user => target_user.display_name
38 @title += t ".tagged_with", :tags => params[:tag] if params[:tag]
41 # 1 - all traces, logged in = all public traces + all user's (i.e + all mine)
42 # 2 - all traces, not logged in = all public traces
43 # 3 - user's traces, logged in as same user = all user's traces
44 # 4 - user's traces, not logged in as that user = all user's public traces
45 traces = if target_user.nil? # all traces
47 Trace.visible_to(current_user) # 1
49 Trace.visible_to_all # 2
51 elsif current_user && current_user == target_user
52 current_user.traces # 3 (check vs user id, so no join + can't pick up non-public traces by changing name)
54 target_user.traces.visible_to_all # 4
57 traces = traces.tagged(params[:tag]) if params[:tag]
59 traces = traces.visible
61 @params = params.permit(:display_name, :tag, :before, :after)
63 @traces = if params[:before]
64 traces.where("gpx_files.id < ?", params[:before]).order(:id => :desc)
66 traces.where("gpx_files.id > ?", params[:after]).order(:id => :asc)
68 traces.order(:id => :desc)
71 @traces = @traces.limit(20)
72 @traces = @traces.includes(:user, :tags)
73 @traces = @traces.sort.reverse
75 @newer_traces = @traces.count.positive? && traces.exists?(["gpx_files.id > ?", @traces.first.id])
76 @older_traces = @traces.count.positive? && traces.exists?(["gpx_files.id < ?", @traces.last.id])
78 # final helper vars for view
79 @target_user = target_user
83 @trace = Trace.find(params[:id])
85 if @trace&.visible? &&
86 (@trace&.public? || @trace&.user == current_user)
87 @title = t ".title", :name => @trace.name
89 flash[:error] = t ".trace_not_found"
90 redirect_to :action => "index"
92 rescue ActiveRecord::RecordNotFound
93 flash[:error] = t ".trace_not_found"
94 redirect_to :action => "index"
98 @title = t ".upload_trace"
99 @trace = Trace.new(:visibility => default_visibility)
103 @trace = Trace.find(params[:id])
107 elsif current_user.nil? || @trace.user != current_user
110 @title = t ".title", :name => @trace.name
112 rescue ActiveRecord::RecordNotFound
117 @title = t ".upload_trace"
119 logger.info(params[:trace][:gpx_file].class.name)
121 if params[:trace][:gpx_file].respond_to?(:read)
122 @trace = do_create(params[:trace][:gpx_file], params[:trace][:tagstring],
123 params[:trace][:description], params[:trace][:visibility])
126 flash[:notice] = t ".trace_uploaded"
127 flash[:warning] = t ".traces_waiting", :count => current_user.traces.where(:inserted => false).count if current_user.traces.where(:inserted => false).count > 4
129 TraceImporterJob.perform_later(@trace)
130 redirect_to :action => :index, :display_name => current_user.display_name
132 flash[:error] = t(".upload_failed") if @trace.valid?
134 render :action => "new"
137 @trace = Trace.new(:name => "Dummy",
138 :tagstring => params[:trace][:tagstring],
139 :description => params[:trace][:description],
140 :visibility => params[:trace][:visibility],
141 :inserted => false, :user => current_user,
142 :timestamp => Time.now.utc)
144 @trace.errors.add(:gpx_file, "can't be blank")
146 render :action => "new"
151 @trace = Trace.find(params[:id])
155 elsif current_user.nil? || @trace.user != current_user
157 elsif @trace.update(trace_params)
158 flash[:notice] = t ".updated"
159 redirect_to :action => "show", :display_name => current_user.display_name
161 @title = t ".title", :name => @trace.name
162 render :action => "edit"
164 rescue ActiveRecord::RecordNotFound
169 trace = Trace.find(params[:id])
173 elsif current_user.nil? || (trace.user != current_user && !current_user.administrator? && !current_user.moderator?)
176 trace.visible = false
178 flash[:notice] = t ".scheduled_for_deletion"
179 TraceDestroyerJob.perform_later(trace)
180 redirect_to :action => :index, :display_name => trace.user.display_name
182 rescue ActiveRecord::RecordNotFound
187 redirect_to :action => :index, :display_name => current_user.display_name
191 trace = Trace.find(params[:id])
193 if trace.visible? && (trace.public? || (current_user && current_user == trace.user))
194 if Acl.no_trace_download(request.remote_ip)
196 elsif request.format == Mime[:xml]
197 send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
198 elsif request.format == Mime[:gpx]
199 send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
200 elsif trace.file.attached?
201 redirect_to rails_blob_path(trace.file, :disposition => "attachment")
203 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
208 rescue ActiveRecord::RecordNotFound
213 @traces = Trace.visible_to_all.visible
215 @traces = @traces.joins(:user).where(:users => { :display_name => params[:display_name] }) if params[:display_name]
217 @traces = @traces.tagged(params[:tag]) if params[:tag]
218 @traces = @traces.order("timestamp DESC")
219 @traces = @traces.limit(20)
220 @traces = @traces.includes(:user)
224 trace = Trace.find(params[:id])
226 if trace.visible? && trace.inserted?
227 if trace.public? || (current_user && current_user == trace.user)
228 if trace.icon.attached?
229 redirect_to rails_blob_path(trace.image, :disposition => "inline")
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")
240 rescue ActiveRecord::RecordNotFound
245 trace = Trace.find(params[:id])
247 if trace.visible? && trace.inserted?
248 if trace.public? || (current_user && current_user == trace.user)
249 if trace.icon.attached?
250 redirect_to rails_blob_path(trace.icon, :disposition => "inline")
252 expires_in 7.days, :private => !trace.public?, :public => trace.public?
253 send_file(trace.icon_picture_name, :filename => "#{trace.id}_icon.gif", :type => "image/gif", :disposition => "inline")
261 rescue ActiveRecord::RecordNotFound
267 def do_create(file, tags, description, visibility)
268 # Sanitise the user's filename
269 name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
271 # Create the trace object
275 :description => description,
276 :visibility => visibility,
278 :user => current_user,
279 :timestamp => Time.now.utc,
283 # Save the trace object
285 # Finally save the user's preferred privacy level
286 if pref = current_user.preferences.where(:k => "gps.trace.visibility").first
290 current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
298 flash.now[:warning] = t "traces.offline_warning.message" if Settings.status == "gpx_offline"
302 render :action => :offline if Settings.status == "gpx_offline"
305 def default_visibility
306 visibility = current_user.preferences.where(:k => "gps.trace.visibility").first
310 elsif current_user.preferences.where(:k => "gps.trace.public", :v => "default").first.nil?
318 params.require(:trace).permit(:description, :tagstring, :visibility)