]> git.openstreetmap.org Git - rails.git/blob - app/controllers/traces_controller.rb
Show sidebar in print media
[rails.git] / app / controllers / traces_controller.rb
1 class TracesController < ApplicationController
2   layout "site", :except => :georss
3
4   before_action :authorize_web
5   before_action :set_locale
6   before_action :check_database_readable
7
8   authorize_resource
9
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]
13
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
16   def index
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
21       if target_user.nil?
22         render_unknown_user display_name
23         return
24       end
25     end
26
27     # set title
28     @title = if target_user.nil?
29                t ".public_traces"
30              elsif current_user && current_user == target_user
31                t ".my_gps_traces"
32              else
33                t ".public_traces_from", :user => target_user.display_name
34              end
35
36     @title += t ".tagged_with", :tags => params[:tag] if params[:tag]
37
38     # four main cases:
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
44                if current_user
45                  Trace.visible_to(current_user) # 1
46                else
47                  Trace.visible_to_all # 2
48                end
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)
51              else
52                target_user.traces.visible_to_all # 4
53              end
54
55     traces = traces.tagged(params[:tag]) if params[:tag]
56
57     traces = traces.visible
58
59     @params = params.permit(:display_name, :tag, :before, :after)
60
61     @traces = if params[:before]
62                 traces.where("gpx_files.id < ?", params[:before]).order(:id => :desc)
63               elsif params[:after]
64                 traces.where("gpx_files.id > ?", params[:after]).order(:id => :asc)
65               else
66                 traces.order(:id => :desc)
67               end
68
69     @traces = @traces.limit(20)
70     @traces = @traces.includes(:user, :tags)
71     @traces = @traces.sort.reverse
72
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])
75
76     # final helper vars for view
77     @target_user = target_user
78   end
79
80   def show
81     @trace = Trace.find(params[:id])
82
83     if @trace&.visible? &&
84        (@trace&.public? || @trace&.user == current_user)
85       @title = t ".title", :name => @trace.name
86     else
87       flash[:error] = t ".trace_not_found"
88       redirect_to :action => "index"
89     end
90   rescue ActiveRecord::RecordNotFound
91     flash[:error] = t ".trace_not_found"
92     redirect_to :action => "index"
93   end
94
95   def new
96     @title = t ".upload_trace"
97     @trace = Trace.new(:visibility => default_visibility)
98   end
99
100   def edit
101     @trace = Trace.find(params[:id])
102
103     if !@trace.visible?
104       head :not_found
105     elsif current_user.nil? || @trace.user != current_user
106       head :forbidden
107     else
108       @title = t ".title", :name => @trace.name
109     end
110   rescue ActiveRecord::RecordNotFound
111     head :not_found
112   end
113
114   def create
115     @title = t ".upload_trace"
116
117     logger.info(params[:trace][:gpx_file].class.name)
118
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])
122
123       if @trace.id
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
126
127         TraceImporterJob.perform_later(@trace)
128         redirect_to :action => :index, :display_name => current_user.display_name
129       else
130         flash[:error] = t(".upload_failed") if @trace.valid?
131
132         render :action => "new"
133       end
134     else
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)
141       @trace.valid?
142       @trace.errors.add(:gpx_file, "can't be blank")
143
144       render :action => "new"
145     end
146   end
147
148   def update
149     @trace = Trace.find(params[:id])
150
151     if !@trace.visible?
152       head :not_found
153     elsif current_user.nil? || @trace.user != current_user
154       head :forbidden
155     elsif @trace.update(trace_params)
156       flash[:notice] = t ".updated"
157       redirect_to :action => "show", :display_name => current_user.display_name
158     else
159       @title = t ".title", :name => @trace.name
160       render :action => "edit"
161     end
162   rescue ActiveRecord::RecordNotFound
163     head :not_found
164   end
165
166   def destroy
167     trace = Trace.find(params[:id])
168
169     if !trace.visible?
170       head :not_found
171     elsif current_user.nil? || (trace.user != current_user && !current_user.administrator? && !current_user.moderator?)
172       head :forbidden
173     else
174       trace.visible = false
175       trace.save
176       flash[:notice] = t ".scheduled_for_deletion"
177       TraceDestroyerJob.perform_later(trace)
178       redirect_to :action => :index, :display_name => trace.user.display_name
179     end
180   rescue ActiveRecord::RecordNotFound
181     head :not_found
182   end
183
184   def mine
185     redirect_to :action => :index, :display_name => current_user.display_name
186   end
187
188   def data
189     trace = Trace.find(params[:id])
190
191     if trace.visible? && (trace.public? || (current_user && current_user == trace.user))
192       if Acl.no_trace_download(request.remote_ip)
193         head :forbidden
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")
200       else
201         send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
202       end
203     else
204       head :not_found
205     end
206   rescue ActiveRecord::RecordNotFound
207     head :not_found
208   end
209
210   def georss
211     @traces = Trace.visible_to_all.visible
212
213     @traces = @traces.joins(:user).where(:users => { :display_name => params[:display_name] }) if params[:display_name]
214
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)
219   end
220
221   def picture
222     trace = Trace.find(params[:id])
223
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")
228         else
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")
231         end
232       else
233         head :forbidden
234       end
235     else
236       head :not_found
237     end
238   rescue ActiveRecord::RecordNotFound
239     head :not_found
240   end
241
242   def icon
243     trace = Trace.find(params[:id])
244
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")
249         else
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")
252         end
253       else
254         head :forbidden
255       end
256     else
257       head :not_found
258     end
259   rescue ActiveRecord::RecordNotFound
260     head :not_found
261   end
262
263   private
264
265   def do_create(file, tags, description, visibility)
266     # Sanitise the user's filename
267     name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
268
269     # Create the trace object
270     trace = Trace.new(
271       :name => name,
272       :tagstring => tags,
273       :description => description,
274       :visibility => visibility,
275       :inserted => false,
276       :user => current_user,
277       :timestamp => Time.now.utc,
278       :file => file
279     )
280
281     # Save the trace object
282     if trace.save
283       # Finally save the user's preferred privacy level
284       if pref = current_user.preferences.where(:k => "gps.trace.visibility").first
285         pref.v = visibility
286         pref.save
287       else
288         current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
289       end
290     end
291
292     trace
293   end
294
295   def offline_warning
296     flash.now[:warning] = t "traces.offline_warning.message" if Settings.status == "gpx_offline"
297   end
298
299   def offline_redirect
300     render :action => :offline if Settings.status == "gpx_offline"
301   end
302
303   def default_visibility
304     visibility = current_user.preferences.where(:k => "gps.trace.visibility").first
305
306     if visibility
307       visibility.v
308     elsif current_user.preferences.where(:k => "gps.trace.public", :v => "default").first.nil?
309       "private"
310     else
311       "public"
312     end
313   end
314
315   def trace_params
316     params.require(:trace).permit(:description, :tagstring, :visibility)
317   end
318 end