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