1 class TracesController < ApplicationController
3 include PaginationMethods
5 layout "site", :except => :georss
7 before_action :authorize_web
8 before_action :set_locale
9 before_action :check_database_readable
13 before_action :check_database_writable, :only => [:new, :create, :edit, :destroy]
14 before_action :offline_warning, :only => [:mine, :show]
15 before_action :offline_redirect, :only => [:new, :create, :edit, :destroy, :data]
17 # Counts and selects pages of GPX traces for various criteria (by user, tags, public etc.).
18 # target_user - if set, specifies the user to fetch traces for. if not set will fetch all traces
20 # from display name, pick up user id if one user's traces only
21 display_name = params[:display_name]
22 if display_name.present?
23 target_user = User.active.find_by(:display_name => display_name)
25 render_unknown_user display_name
31 @title = if target_user.nil?
33 elsif current_user && current_user == target_user
36 t ".public_traces_from", :user => target_user.display_name
39 @title += t ".tagged_with", :tags => params[:tag] if params[:tag]
42 # 1 - all traces, logged in = all public traces + all user's (i.e + all mine)
43 # 2 - all traces, not logged in = all public traces
44 # 3 - user's traces, logged in as same user = all user's traces
45 # 4 - user's traces, not logged in as that user = all user's public traces
46 traces = if target_user.nil? # all traces
48 Trace.visible_to(current_user) # 1
50 Trace.visible_to_all # 2
52 elsif current_user && current_user == target_user
53 current_user.traces # 3 (check vs user id, so no join + can't pick up non-public traces by changing name)
55 target_user.traces.visible_to_all # 4
58 traces = traces.tagged(params[:tag]) if params[:tag]
60 traces = traces.visible
62 @params = params.permit(:display_name, :tag, :before, :after)
64 @traces, @newer_traces_id, @older_traces_id = get_page_items(traces, [:user, :tags])
66 # final helper vars for view
67 @target_user = target_user
71 @trace = Trace.find(params[:id])
73 if @trace&.visible? &&
74 (@trace&.public? || @trace&.user == current_user)
75 @title = t ".title", :name => @trace.name
77 flash[:error] = t ".trace_not_found"
78 redirect_to :action => "index"
80 rescue ActiveRecord::RecordNotFound
81 flash[:error] = t ".trace_not_found"
82 redirect_to :action => "index"
86 @title = t ".upload_trace"
87 @trace = Trace.new(:visibility => default_visibility)
91 @trace = Trace.find(params[:id])
95 elsif current_user.nil? || @trace.user != current_user
98 @title = t ".title", :name => @trace.name
100 rescue ActiveRecord::RecordNotFound
105 @title = t ".upload_trace"
107 logger.info(params[:trace][:gpx_file].class.name)
109 if params[:trace][:gpx_file].respond_to?(:read)
110 @trace = do_create(params[:trace][:gpx_file], params[:trace][:tagstring],
111 params[:trace][:description], params[:trace][:visibility])
114 flash[:notice] = t ".trace_uploaded"
115 flash[:warning] = t ".traces_waiting", :count => current_user.traces.where(:inserted => false).count if current_user.traces.where(:inserted => false).count > 4
117 @trace.schedule_import
118 redirect_to :action => :index, :display_name => current_user.display_name
120 flash[:error] = t(".upload_failed") if @trace.valid?
122 render :action => "new"
125 @trace = Trace.new(:name => "Dummy",
126 :tagstring => params[:trace][:tagstring],
127 :description => params[:trace][:description],
128 :visibility => params[:trace][:visibility],
129 :inserted => false, :user => current_user,
130 :timestamp => Time.now.utc)
132 @trace.errors.add(:gpx_file, "can't be blank")
134 render :action => "new"
139 @trace = Trace.find(params[:id])
143 elsif current_user.nil? || @trace.user != current_user
145 elsif @trace.update(trace_params)
146 flash[:notice] = t ".updated"
147 redirect_to :action => "show", :display_name => current_user.display_name
149 @title = t ".title", :name => @trace.name
150 render :action => "edit"
152 rescue ActiveRecord::RecordNotFound
157 trace = Trace.find(params[:id])
161 elsif current_user.nil? || (trace.user != current_user && !current_user.administrator? && !current_user.moderator?)
164 trace.visible = false
166 flash[:notice] = t ".scheduled_for_deletion"
167 trace.schedule_destruction
168 redirect_to :action => :index, :display_name => trace.user.display_name
170 rescue ActiveRecord::RecordNotFound
175 redirect_to :action => :index, :display_name => current_user.display_name
179 trace = Trace.find(params[:id])
181 if trace.visible? && (trace.public? || (current_user && current_user == trace.user))
182 if Acl.no_trace_download(request.remote_ip)
184 elsif request.format == Mime[:xml]
185 send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
186 elsif request.format == Mime[:gpx]
187 send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
188 elsif trace.file.attached?
189 redirect_to rails_blob_path(trace.file, :disposition => "attachment")
191 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
196 rescue ActiveRecord::RecordNotFound
201 @traces = Trace.visible_to_all.visible
203 @traces = @traces.joins(:user).where(:users => { :display_name => params[:display_name] }) if params[:display_name]
205 @traces = @traces.tagged(params[:tag]) if params[:tag]
206 @traces = @traces.order("timestamp DESC")
207 @traces = @traces.limit(20)
208 @traces = @traces.includes(:user)
212 trace = Trace.find(params[:id])
214 if trace.visible? && trace.inserted?
215 if trace.public? || (current_user && current_user == trace.user)
216 if trace.icon.attached?
217 redirect_to rails_blob_path(trace.image, :disposition => "inline")
219 expires_in 7.days, :private => !trace.public?, :public => trace.public?
220 send_file(trace.large_picture_name, :filename => "#{trace.id}.gif", :type => "image/gif", :disposition => "inline")
228 rescue ActiveRecord::RecordNotFound
233 trace = Trace.find(params[:id])
235 if trace.visible? && trace.inserted?
236 if trace.public? || (current_user && current_user == trace.user)
237 if trace.icon.attached?
238 redirect_to rails_blob_path(trace.icon, :disposition => "inline")
240 expires_in 7.days, :private => !trace.public?, :public => trace.public?
241 send_file(trace.icon_picture_name, :filename => "#{trace.id}_icon.gif", :type => "image/gif", :disposition => "inline")
249 rescue ActiveRecord::RecordNotFound
255 def do_create(file, tags, description, visibility)
256 # Sanitise the user's filename
257 name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
259 # Create the trace object
263 :description => description,
264 :visibility => visibility,
266 :user => current_user,
267 :timestamp => Time.now.utc,
271 # Save the trace object
273 # Finally save the user's preferred privacy level
274 if pref = current_user.preferences.find_by(:k => "gps.trace.visibility")
278 current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
286 flash.now[:warning] = t "traces.offline_warning.message" if Settings.status == "gpx_offline"
290 render :action => :offline if Settings.status == "gpx_offline"
293 def default_visibility
294 visibility = current_user.preferences.find_by(:k => "gps.trace.visibility")
298 elsif current_user.preferences.find_by(:k => "gps.trace.public", :v => "default").nil?
306 params.require(:trace).permit(:description, :tagstring, :visibility)