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 @trace = Trace.find(params[:id])
75 if @trace&.visible? &&
76 (@trace&.public? || @trace&.user == current_user)
77 @title = t ".title", :name => @trace.name
79 flash[:error] = t ".trace_not_found"
80 redirect_to :action => "index"
82 rescue ActiveRecord::RecordNotFound
83 flash[:error] = t ".trace_not_found"
84 redirect_to :action => "index"
88 @title = t ".upload_trace"
89 @trace = Trace.new(:visibility => default_visibility)
93 @trace = Trace.find(params[:id])
97 elsif current_user.nil? || @trace.user != current_user
100 @title = t ".title", :name => @trace.name
102 rescue ActiveRecord::RecordNotFound
107 @title = t ".upload_trace"
109 logger.info(params[:trace][:gpx_file].class.name)
111 if params[:trace][:gpx_file].respond_to?(:read)
112 @trace = do_create(params[:trace][:gpx_file], params[:trace][:tagstring],
113 params[:trace][:description], params[:trace][:visibility])
116 flash[:notice] = t ".trace_uploaded"
117 flash[:warning] = t ".traces_waiting", :count => current_user.traces.where(:inserted => false).count if current_user.traces.where(:inserted => false).count > 4
119 TraceImporterJob.perform_later(@trace)
120 redirect_to :action => :index, :display_name => current_user.display_name
122 flash[:error] = t(".upload_failed") if @trace.valid?
124 render :action => "new"
127 @trace = Trace.new(:name => "Dummy",
128 :tagstring => params[:trace][:tagstring],
129 :description => params[:trace][:description],
130 :visibility => params[:trace][:visibility],
131 :inserted => false, :user => current_user,
132 :timestamp => Time.now.utc)
134 @trace.errors.add(:gpx_file, "can't be blank")
136 render :action => "new"
141 @trace = Trace.find(params[:id])
145 elsif current_user.nil? || @trace.user != current_user
147 elsif @trace.update(trace_params)
148 flash[:notice] = t ".updated"
149 redirect_to :action => "show", :display_name => current_user.display_name
151 @title = t ".title", :name => @trace.name
152 render :action => "edit"
154 rescue ActiveRecord::RecordNotFound
159 trace = Trace.find(params[:id])
163 elsif current_user.nil? || (trace.user != current_user && !current_user.administrator? && !current_user.moderator?)
166 trace.visible = false
168 flash[:notice] = t ".scheduled_for_deletion"
169 TraceDestroyerJob.perform_later(trace)
170 redirect_to :action => :index, :display_name => trace.user.display_name
172 rescue ActiveRecord::RecordNotFound
177 redirect_to :action => :index, :display_name => current_user.display_name
181 trace = Trace.find(params[:id])
183 if trace.visible? && (trace.public? || (current_user && current_user == trace.user))
184 if Acl.no_trace_download(request.remote_ip)
186 elsif request.format == Mime[:xml]
187 send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
188 elsif request.format == Mime[:gpx]
189 send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
190 elsif trace.file.attached?
191 redirect_to rails_blob_path(trace.file, :disposition => "attachment")
193 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
198 rescue ActiveRecord::RecordNotFound
203 @traces = Trace.visible_to_all.visible
205 @traces = @traces.joins(:user).where(:users => { :display_name => params[:display_name] }) if params[:display_name]
207 @traces = @traces.tagged(params[:tag]) if params[:tag]
208 @traces = @traces.order("timestamp DESC")
209 @traces = @traces.limit(20)
210 @traces = @traces.includes(:user)
214 trace = Trace.find(params[:id])
216 if trace.visible? && trace.inserted?
217 if trace.public? || (current_user && current_user == trace.user)
218 if trace.icon.attached?
219 redirect_to rails_blob_path(trace.image, :disposition => "inline")
221 expires_in 7.days, :private => !trace.public?, :public => trace.public?
222 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 if trace.icon.attached?
240 redirect_to rails_blob_path(trace.icon, :disposition => "inline")
242 expires_in 7.days, :private => !trace.public?, :public => trace.public?
243 send_file(trace.icon_picture_name, :filename => "#{trace.id}_icon.gif", :type => "image/gif", :disposition => "inline")
251 rescue ActiveRecord::RecordNotFound
257 def do_create(file, tags, description, visibility)
258 # Sanitise the user's filename
259 name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
261 # Create the trace object
265 :description => description,
266 :visibility => visibility,
268 :user => current_user,
269 :timestamp => Time.now.utc,
273 # Save the trace object
275 # Finally save the user's preferred privacy level
276 if pref = current_user.preferences.where(:k => "gps.trace.visibility").first
280 current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
288 flash.now[:warning] = t "traces.offline_warning.message" if Settings.status == "gpx_offline"
292 render :action => :offline if Settings.status == "gpx_offline"
295 def default_visibility
296 visibility = current_user.preferences.where(:k => "gps.trace.visibility").first
300 elsif current_user.preferences.where(:k => "gps.trace.public", :v => "default").first.nil?
308 params.require(:trace).permit(:description, :tagstring, :visibility)