]> git.openstreetmap.org Git - rails.git/blob - app/controllers/traces_controller.rb
86b09215ef94eba30405089e4918cc37020459fe
[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     @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 show
73     @trace = Trace.find(params[:id])
74
75     if @trace&.visible? &&
76        (@trace&.public? || @trace&.user == current_user)
77       @title = t ".title", :name => @trace.name
78     else
79       flash[:error] = t ".trace_not_found"
80       redirect_to :action => "index"
81     end
82   rescue ActiveRecord::RecordNotFound
83     flash[:error] = t ".trace_not_found"
84     redirect_to :action => "index"
85   end
86
87   def new
88     @title = t ".upload_trace"
89     @trace = Trace.new(:visibility => default_visibility)
90   end
91
92   def edit
93     @trace = Trace.find(params[:id])
94
95     if !@trace.visible?
96       head :not_found
97     elsif current_user.nil? || @trace.user != current_user
98       head :forbidden
99     else
100       @title = t ".title", :name => @trace.name
101     end
102   rescue ActiveRecord::RecordNotFound
103     head :not_found
104   end
105
106   def create
107     @title = t ".upload_trace"
108
109     logger.info(params[:trace][:gpx_file].class.name)
110
111     if params[:trace][:gpx_file].respond_to?(:read)
112       @trace = do_create(params[:trace][:gpx_file], params[:trace][:tagstring],
113                          params[:trace][:description], params[:trace][:visibility])
114
115       if @trace.id
116         flash[:notice] = t ".trace_uploaded"
117         flash[:warning] = t ".traces_waiting", :count => current_user.traces.where(:inserted => false).count if current_user.traces.where(:inserted => false).count > 4
118
119         TraceImporterJob.perform_later(@trace)
120         redirect_to :action => :index, :display_name => current_user.display_name
121       else
122         flash[:error] = t("traces.create.upload_failed") if @trace.valid?
123
124         render :action => "new"
125       end
126     else
127       @trace = Trace.new(:name => "Dummy",
128                          :tagstring => params[:trace][:tagstring],
129                          :description => params[:trace][:description],
130                          :visibility => params[:trace][:visibility],
131                          :inserted => false, :user => current_user,
132                          :timestamp => Time.now.utc)
133       @trace.valid?
134       @trace.errors.add(:gpx_file, "can't be blank")
135
136       render :action => "new"
137     end
138   end
139
140   def update
141     @trace = Trace.find(params[:id])
142
143     if !@trace.visible?
144       head :not_found
145     elsif current_user.nil? || @trace.user != current_user
146       head :forbidden
147     elsif @trace.update(trace_params)
148       flash[:notice] = t ".updated"
149       redirect_to :action => "show", :display_name => current_user.display_name
150     else
151       @title = t ".title", :name => @trace.name
152       render :action => "edit"
153     end
154   rescue ActiveRecord::RecordNotFound
155     head :not_found
156   end
157
158   def destroy
159     trace = Trace.find(params[:id])
160
161     if !trace.visible?
162       head :not_found
163     elsif current_user.nil? || (trace.user != current_user && !current_user.administrator? && !current_user.moderator?)
164       head :forbidden
165     else
166       trace.visible = false
167       trace.save
168       flash[:notice] = t ".scheduled_for_deletion"
169       TraceDestroyerJob.perform_later(trace)
170       redirect_to :action => :index, :display_name => trace.user.display_name
171     end
172   rescue ActiveRecord::RecordNotFound
173     head :not_found
174   end
175
176   def mine
177     redirect_to :action => :index, :display_name => current_user.display_name
178   end
179
180   def data
181     trace = Trace.find(params[:id])
182
183     if trace.visible? && (trace.public? || (current_user && current_user == trace.user))
184       if Acl.no_trace_download(request.remote_ip)
185         head :forbidden
186       elsif request.format == Mime[:xml]
187         send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
188       elsif request.format == Mime[:gpx]
189         send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
190       elsif trace.file.attached?
191         redirect_to rails_blob_path(trace.file, :disposition => "attachment")
192       else
193         send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
194       end
195     else
196       head :not_found
197     end
198   rescue ActiveRecord::RecordNotFound
199     head :not_found
200   end
201
202   def georss
203     @traces = Trace.visible_to_all.visible
204
205     @traces = @traces.joins(:user).where(:users => { :display_name => params[:display_name] }) if params[:display_name]
206
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)
211   end
212
213   def picture
214     trace = Trace.find(params[:id])
215
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")
220         else
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         end
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         if trace.icon.attached?
240           redirect_to rails_blob_path(trace.icon, :disposition => "inline")
241         else
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")
244         end
245       else
246         head :forbidden
247       end
248     else
249       head :not_found
250     end
251   rescue ActiveRecord::RecordNotFound
252     head :not_found
253   end
254
255   private
256
257   def do_create(file, tags, description, visibility)
258     # Sanitise the user's filename
259     name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
260
261     # Create the trace object
262     trace = Trace.new(
263       :name => name,
264       :tagstring => tags,
265       :description => description,
266       :visibility => visibility,
267       :inserted => false,
268       :user => current_user,
269       :timestamp => Time.now.utc,
270       :file => file
271     )
272
273     # Save the trace object
274     if trace.save
275       # Finally save the user's preferred privacy level
276       if pref = current_user.preferences.where(:k => "gps.trace.visibility").first
277         pref.v = visibility
278         pref.save
279       else
280         current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
281       end
282     end
283
284     trace
285   end
286
287   def offline_warning
288     flash.now[:warning] = t "traces.offline_warning.message" if Settings.status == "gpx_offline"
289   end
290
291   def offline_redirect
292     render :action => :offline if Settings.status == "gpx_offline"
293   end
294
295   def default_visibility
296     visibility = current_user.preferences.where(:k => "gps.trace.visibility").first
297
298     if visibility
299       visibility.v
300     elsif current_user.preferences.where(:k => "gps.trace.public", :v => "default").first.nil?
301       "private"
302     else
303       "public"
304     end
305   end
306
307   def trace_params
308     params.require(:trace).permit(:description, :tagstring, :visibility)
309   end
310 end