]> git.openstreetmap.org Git - rails.git/blob - app/controllers/traces_controller.rb
Fix new rubocop warnings
[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_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     @params = params.permit(:display_name, :tag)
58
59     @page = (params[:page] || 1).to_i
60     @page_size = 20
61
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)
67
68     # final helper vars for view
69     @target_user = target_user
70   end
71
72   def mine
73     redirect_to :action => :index, :display_name => current_user.display_name
74   end
75
76   def show
77     @trace = Trace.find(params[:id])
78
79     if @trace&.visible? &&
80        (@trace&.public? || @trace&.user == current_user)
81       @title = t ".title", :name => @trace.name
82     else
83       flash[:error] = t ".trace_not_found"
84       redirect_to :action => "index"
85     end
86   rescue ActiveRecord::RecordNotFound
87     flash[:error] = t ".trace_not_found"
88     redirect_to :action => "index"
89   end
90
91   def new
92     @title = t ".upload_trace"
93     @trace = Trace.new(:visibility => default_visibility)
94   end
95
96   def create
97     @title = t ".upload_trace"
98
99     logger.info(params[:trace][:gpx_file].class.name)
100
101     if params[:trace][:gpx_file].respond_to?(:read)
102       begin
103         @trace = do_create(params[:trace][:gpx_file], params[:trace][:tagstring],
104                            params[:trace][:description], params[:trace][:visibility])
105       rescue StandardError => e
106         logger.debug e
107       end
108
109       if @trace.id
110         flash[:notice] = t ".trace_uploaded"
111         flash[:warning] = t ".traces_waiting", :count => current_user.traces.where(:inserted => false).count if current_user.traces.where(:inserted => false).count > 4
112
113         TraceImporterJob.perform_later(@trace) if Settings.trace_use_job_queue
114         redirect_to :action => :index, :display_name => current_user.display_name
115       else
116         flash[:error] = t("traces.create.upload_failed") if @trace.valid?
117
118         render :action => "new"
119       end
120     else
121       @trace = Trace.new(:name => "Dummy",
122                          :tagstring => params[:trace][:tagstring],
123                          :description => params[:trace][:description],
124                          :visibility => params[:trace][:visibility],
125                          :inserted => false, :user => current_user,
126                          :timestamp => Time.now.getutc)
127       @trace.valid?
128       @trace.errors.add(:gpx_file, "can't be blank")
129
130       render :action => "new"
131     end
132   end
133
134   def data
135     trace = Trace.find(params[:id])
136
137     if trace.visible? && (trace.public? || (current_user && current_user == trace.user))
138       if Acl.no_trace_download(request.remote_ip)
139         head :forbidden
140       elsif request.format == Mime[:xml]
141         send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
142       elsif request.format == Mime[:gpx]
143         send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
144       else
145         send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
146       end
147     else
148       head :not_found
149     end
150   rescue ActiveRecord::RecordNotFound
151     head :not_found
152   end
153
154   def edit
155     @trace = Trace.find(params[:id])
156
157     if !@trace.visible?
158       head :not_found
159     elsif current_user.nil? || @trace.user != current_user
160       head :forbidden
161     else
162       @title = t ".title", :name => @trace.name
163     end
164   rescue ActiveRecord::RecordNotFound
165     head :not_found
166   end
167
168   def update
169     @trace = Trace.find(params[:id])
170
171     if !@trace.visible?
172       head :not_found
173     elsif current_user.nil? || @trace.user != current_user
174       head :forbidden
175     elsif @trace.update(trace_params)
176       flash[:notice] = t ".updated"
177       redirect_to :action => "show", :display_name => current_user.display_name
178     else
179       @title = t ".title", :name => @trace.name
180       render :action => "edit"
181     end
182   rescue ActiveRecord::RecordNotFound
183     head :not_found
184   end
185
186   def destroy
187     trace = Trace.find(params[:id])
188
189     if !trace.visible?
190       head :not_found
191     elsif current_user.nil? || (trace.user != current_user && !current_user.administrator? && !current_user.moderator?)
192       head :forbidden
193     else
194       trace.visible = false
195       trace.save
196       flash[:notice] = t ".scheduled_for_deletion"
197       TraceDestroyerJob.perform_later(trace) if Settings.trace_use_job_queue
198       redirect_to :action => :index, :display_name => trace.user.display_name
199     end
200   rescue ActiveRecord::RecordNotFound
201     head :not_found
202   end
203
204   def georss
205     @traces = Trace.visible_to_all.visible
206
207     @traces = @traces.joins(:user).where(:users => { :display_name => params[:display_name] }) if params[:display_name]
208
209     @traces = @traces.tagged(params[:tag]) if params[:tag]
210     @traces = @traces.order("timestamp DESC")
211     @traces = @traces.limit(20)
212     @traces = @traces.includes(:user)
213   end
214
215   def picture
216     trace = Trace.find(params[:id])
217
218     if trace.visible? && trace.inserted?
219       if trace.public? || (current_user && current_user == trace.user)
220         expires_in 7.days, :private => !trace.public?, :public => trace.public?
221         send_file(trace.large_picture_name, :filename => "#{trace.id}.gif", :type => "image/gif", :disposition => "inline")
222       else
223         head :forbidden
224       end
225     else
226       head :not_found
227     end
228   rescue ActiveRecord::RecordNotFound
229     head :not_found
230   end
231
232   def icon
233     trace = Trace.find(params[:id])
234
235     if trace.visible? && trace.inserted?
236       if trace.public? || (current_user && current_user == trace.user)
237         expires_in 7.days, :private => !trace.public?, :public => trace.public?
238         send_file(trace.icon_picture_name, :filename => "#{trace.id}_icon.gif", :type => "image/gif", :disposition => "inline")
239       else
240         head :forbidden
241       end
242     else
243       head :not_found
244     end
245   rescue ActiveRecord::RecordNotFound
246     head :not_found
247   end
248
249   private
250
251   def do_create(file, tags, description, visibility)
252     # Sanitise the user's filename
253     name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
254
255     # Get a temporary filename...
256     filename = "/tmp/#{rand}"
257
258     # ...and save the uploaded file to that location
259     File.open(filename, "wb") { |f| f.write(file.read) }
260
261     # Create the trace object, falsely marked as already
262     # inserted to stop the import daemon trying to load it
263     trace = Trace.new(
264       :name => name,
265       :tagstring => tags,
266       :description => description,
267       :visibility => visibility,
268       :inserted => true,
269       :user => current_user,
270       :timestamp => Time.now.getutc
271     )
272
273     if trace.valid?
274       Trace.transaction do
275         begin
276           # Save the trace object
277           trace.save!
278
279           # Rename the temporary file to the final name
280           FileUtils.mv(filename, trace.trace_name)
281         rescue StandardError
282           # Remove the file as we have failed to update the database
283           FileUtils.rm_f(filename)
284
285           # Pass the exception on
286           raise
287         end
288
289         begin
290           # Clear the inserted flag to make the import daemon load the trace
291           trace.inserted = false
292           trace.save!
293         rescue StandardError
294           # Remove the file as we have failed to update the database
295           FileUtils.rm_f(trace.trace_name)
296
297           # Pass the exception on
298           raise
299         end
300       end
301     end
302
303     # Finally save the user's preferred privacy level
304     if pref = current_user.preferences.where(:k => "gps.trace.visibility").first
305       pref.v = visibility
306       pref.save
307     else
308       current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
309     end
310
311     trace
312   end
313
314   def offline_warning
315     flash.now[:warning] = t "traces.offline_warning.message" if Settings.status == "gpx_offline"
316   end
317
318   def offline_redirect
319     render :action => :offline if Settings.status == "gpx_offline"
320   end
321
322   def default_visibility
323     visibility = current_user.preferences.where(:k => "gps.trace.visibility").first
324
325     if visibility
326       visibility.v
327     elsif current_user.preferences.where(:k => "gps.trace.public", :v => "default").first.nil?
328       "private"
329     else
330       "public"
331     end
332   end
333
334   def trace_params
335     params.require(:trace).permit(:description, :tagstring, :visibility)
336   end
337 end