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 redirect_to :action => :index, :display_name => current_user.display_name
77 @trace = Trace.find(params[:id])
79 if @trace&.visible? &&
80 (@trace&.public? || @trace&.user == current_user)
81 @title = t ".title", :name => @trace.name
83 flash[:error] = t ".trace_not_found"
84 redirect_to :action => "index"
86 rescue ActiveRecord::RecordNotFound
87 flash[:error] = t ".trace_not_found"
88 redirect_to :action => "index"
92 @title = t ".upload_trace"
93 @trace = Trace.new(:visibility => default_visibility)
97 @title = t ".upload_trace"
99 logger.info(params[:trace][:gpx_file].class.name)
101 if params[:trace][:gpx_file].respond_to?(:read)
102 @trace = do_create(params[:trace][:gpx_file], params[:trace][:tagstring],
103 params[:trace][:description], params[:trace][:visibility])
106 flash[:notice] = t ".trace_uploaded"
107 flash[:warning] = t ".traces_waiting", :count => current_user.traces.where(:inserted => false).count if current_user.traces.where(:inserted => false).count > 4
109 TraceImporterJob.perform_later(@trace)
110 redirect_to :action => :index, :display_name => current_user.display_name
112 flash[:error] = t("traces.create.upload_failed") if @trace.valid?
114 render :action => "new"
117 @trace = Trace.new(:name => "Dummy",
118 :tagstring => params[:trace][:tagstring],
119 :description => params[:trace][:description],
120 :visibility => params[:trace][:visibility],
121 :inserted => false, :user => current_user,
122 :timestamp => Time.now.getutc)
124 @trace.errors.add(:gpx_file, "can't be blank")
126 render :action => "new"
131 trace = Trace.find(params[:id])
133 if trace.visible? && (trace.public? || (current_user && current_user == trace.user))
134 if Acl.no_trace_download(request.remote_ip)
136 elsif request.format == Mime[:xml]
137 send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
138 elsif request.format == Mime[:gpx]
139 send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
140 elsif trace.file.attached?
141 redirect_to rails_blob_path(trace.file, :disposition => "attachment")
143 send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
148 rescue ActiveRecord::RecordNotFound
153 @trace = Trace.find(params[:id])
157 elsif current_user.nil? || @trace.user != current_user
160 @title = t ".title", :name => @trace.name
162 rescue ActiveRecord::RecordNotFound
167 @trace = Trace.find(params[:id])
171 elsif current_user.nil? || @trace.user != current_user
173 elsif @trace.update(trace_params)
174 flash[:notice] = t ".updated"
175 redirect_to :action => "show", :display_name => current_user.display_name
177 @title = t ".title", :name => @trace.name
178 render :action => "edit"
180 rescue ActiveRecord::RecordNotFound
185 trace = Trace.find(params[:id])
189 elsif current_user.nil? || (trace.user != current_user && !current_user.administrator? && !current_user.moderator?)
192 trace.visible = false
194 flash[:notice] = t ".scheduled_for_deletion"
195 TraceDestroyerJob.perform_later(trace)
196 redirect_to :action => :index, :display_name => trace.user.display_name
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.getutc,
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)