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