]> git.openstreetmap.org Git - rails.git/blob - app/controllers/trace_controller.rb
Fixed YAML syntax error in commit [16421]
[rails.git] / app / controllers / trace_controller.rb
1 class TraceController < ApplicationController
2   layout 'site'
3
4   before_filter :authorize_web
5   before_filter :set_locale
6   before_filter :require_user, :only => [:mine, :create, :edit, :delete, :make_public]
7   before_filter :authorize, :only => [:api_details, :api_data, :api_create]
8   before_filter :check_database_readable, :except => [:api_details, :api_data, :api_create]
9   before_filter :check_database_writable, :only => [:create, :edit, :delete, :make_public]
10   before_filter :check_api_readable, :only => [:api_details, :api_data]
11   before_filter :check_api_writable, :only => [:api_create]
12  
13   # Counts and selects pages of GPX traces for various criteria (by user, tags, public etc.).
14   #  target_user - if set, specifies the user to fetch traces for.  if not set will fetch all traces
15   def list(target_user = nil, action = "list")
16     # from display name, pick up user id if one user's traces only
17     display_name = params[:display_name]
18     if target_user.nil? and !display_name.blank?
19       target_user = User.find(:first, :conditions => [ "visible = ? and display_name = ?", true, display_name])
20       if target_user.nil?
21         @title = t'trace.no_such_user.title'
22         @not_found_user = display_name
23         render :action => 'no_such_user', :status => :not_found
24         return
25       end
26     end
27
28     # set title
29     if target_user.nil?
30       @title = t 'trace.list.public_traces'
31     elsif @user and @user == target_user
32       @title = t 'trace.list.your_traces'
33     else
34       @title = t 'trace.list.public_traces_from', :user => target_user.display_name
35     end
36
37     @title += t 'trace.list.tagged_with', :tags => params[:tag] if params[:tag]
38
39     # four main cases:
40     # 1 - all traces, logged in = all public traces + all user's (i.e + all mine)
41     # 2 - all traces, not logged in = all public traces
42     # 3 - user's traces, logged in as same user = all user's traces 
43     # 4 - user's traces, not logged in as that user = all user's public traces
44     if target_user.nil? # all traces
45       if @user
46         conditions = ["(gpx_files.public = ? OR gpx_files.user_id = ?)", true, @user.id] #1
47       else
48         conditions  = ["gpx_files.public = ?", true] #2
49       end
50     else
51       if @user and @user == target_user
52         conditions = ["gpx_files.user_id = ?", @user.id] #3 (check vs user id, so no join + can't pick up non-public traces by changing name)
53       else
54         conditions = ["gpx_files.public = ? AND gpx_files.user_id = ?", true, target_user.id] #4
55       end
56     end
57     
58     if params[:tag]
59       @tag = params[:tag]
60
61       files = Tracetag.find_all_by_tag(params[:tag]).collect { |tt| tt.gpx_id }
62
63       if files.length > 0
64         conditions[0] += " AND gpx_files.id IN (#{files.join(',')})"
65       else
66         conditions[0] += " AND 0 = 1"
67       end
68     end
69     
70     conditions[0] += " AND gpx_files.visible = ?"
71     conditions << true
72
73     @trace_pages, @traces = paginate(:traces,
74                                      :include => [:user, :tags],
75                                      :conditions => conditions,
76                                      :order => "gpx_files.timestamp DESC",
77                                      :per_page => 20)
78
79     # put together SET of tags across traces, for related links
80     tagset = Hash.new
81     if @traces
82       @traces.each do |trace|
83         trace.tags.reload if params[:tag] # if searched by tag, ActiveRecord won't bring back other tags, so do explicitly here
84         trace.tags.each do |tag|
85           tagset[tag.tag] = tag.tag
86         end
87       end
88     end
89     
90     # final helper vars for view
91     @action = action
92     @display_name = target_user.display_name if target_user
93     @all_tags = tagset.values
94   end
95
96   def mine
97     # Load the preference of whether the user set the trace public the last time
98     @trace = Trace.new
99     if @user.preferences.find(:first, :conditions => {:k => "gps.trace.public", :v => "default"}).nil?
100       @trace.public = false
101     else 
102       @trace.public = true
103     end
104     list(@user, "mine")
105   end
106
107   def view
108     @trace = Trace.find(params[:id])
109
110     if @trace and @trace.visible? and
111        (@trace.public? or @trace.user == @user)
112       @title = t 'trace.view.title', :name => @trace.name
113     else
114       flash[:notice] = t 'trace.view.trace_not_found'
115       redirect_to :controller => 'trace', :action => 'list'
116     end
117   rescue ActiveRecord::RecordNotFound
118     flash[:notice] = t 'trace.view.trace_not_found'
119     redirect_to :controller => 'trace', :action => 'list'
120   end
121
122   def create
123     if params[:trace]
124       logger.info(params[:trace][:gpx_file].class.name)
125       if params[:trace][:gpx_file].respond_to?(:read)
126         do_create(params[:trace][:gpx_file], params[:trace][:tagstring],
127                   params[:trace][:description], params[:trace][:public])
128
129         if @trace.id
130           logger.info("id is #{@trace.id}")
131           flash[:notice] = t 'trace.create.trace_uploaded'
132
133           redirect_to :action => 'mine'
134         end
135       else
136         @trace = Trace.new({:name => "Dummy",
137                             :tagstring => params[:trace][:tagstring],
138                             :description => params[:trace][:description],
139                             :public => params[:trace][:public],
140                             :inserted => false, :user => @user,
141                             :timestamp => Time.now.getutc})
142         @trace.valid?
143         @trace.errors.add(:gpx_file, "can't be blank")
144       end
145     end
146     @title = t 'trace.create.upload_trace'
147   end
148
149   def data
150     trace = Trace.find(params[:id])
151
152     if trace.visible? and (trace.public? or (@user and @user == trace.user))
153       if request.format == Mime::XML
154         send_file(trace.xml_file, :filename => "#{trace.id}.xml", :type => Mime::XML.to_s, :disposition => 'attachment')
155       else
156         send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => 'attachment')
157       end
158     else
159       render :nothing => true, :status => :not_found
160     end
161   rescue ActiveRecord::RecordNotFound
162     render :nothing => true, :status => :not_found
163   end
164
165   def edit
166     @trace = Trace.find(params[:id])
167
168     if @user and @trace.user == @user
169       @title = t 'trace.edit.title', :name => @trace.name
170       if params[:trace]
171         @trace.description = params[:trace][:description]
172         @trace.tagstring = params[:trace][:tagstring]
173         if @trace.save
174           redirect_to :action => 'view'
175         end        
176       end
177     else
178       render :nothing => true, :status => :forbidden
179     end
180   rescue ActiveRecord::RecordNotFound
181     render :nothing => true, :status => :not_found
182   end
183
184   def delete
185     trace = Trace.find(params[:id])
186
187     if @user and trace.user == @user
188       if request.post? and trace.visible?
189         trace.visible = false
190         trace.save
191         flash[:notice] = t 'trace.delete.scheduled_for_deletion'
192         redirect_to :controller => 'traces', :action => 'mine'
193       else
194         render :nothing => true, :status => :bad_request
195       end
196     else
197       render :nothing => true, :status => :forbidden
198     end
199   rescue ActiveRecord::RecordNotFound
200     render :nothing => true, :status => :not_found
201   end
202
203   def make_public
204     trace = Trace.find(params[:id])
205
206     if @user and trace.user == @user
207       if request.post? and !trace.public?
208         trace.public = true
209         trace.save
210         flash[:notice] = t 'trace.make_public.made_public'
211         redirect_to :controller => 'trace', :action => 'view', :id => params[:id]
212       else
213         render :nothing => true, :status => :bad_request
214       end
215     else
216       render :nothing => true, :status => :forbidden
217     end
218   rescue ActiveRecord::RecordNotFound
219     render :nothing => true, :status => :not_found
220   end
221
222   def georss
223     conditions = ["gpx_files.public = ?", true]
224
225     if params[:display_name]
226       conditions[0] += " AND users.display_name = ?"
227       conditions << params[:display_name]
228     end
229
230     if params[:tag]
231       conditions[0] += " AND EXISTS (SELECT * FROM gpx_file_tags AS gft WHERE gft.gpx_id = gpx_files.id AND gft.tag = ?)"
232       conditions << params[:tag]
233     end
234
235     traces = Trace.find(:all, :include => :user, :conditions => conditions, 
236                         :order => "timestamp DESC", :limit => 20)
237
238     rss = OSM::GeoRSS.new
239
240     traces.each do |trace|
241       rss.add(trace.latitude, trace.longitude, trace.name, trace.user.display_name, url_for({:controller => 'trace', :action => 'view', :id => trace.id, :display_name => trace.user.display_name}), "<img src='#{url_for({:controller => 'trace', :action => 'icon', :id => trace.id, :user_login => trace.user.display_name})}'> GPX file with #{trace.size} points from #{trace.user.display_name}", trace.timestamp)
242     end
243
244     render :text => rss.to_s, :content_type => "application/rss+xml"
245   end
246
247   def picture
248     trace = Trace.find(params[:id])
249
250     if trace.inserted?
251       if trace.public? or (@user and @user == trace.user)
252         expires_in 7.days, :private => !trace.public, :public => trace.public
253         send_file(trace.large_picture_name, :filename => "#{trace.id}.gif", :type => 'image/gif', :disposition => 'inline')
254       else
255         render :nothing => true, :status => :forbidden
256       end
257     else
258       render :nothing => true, :status => :not_found
259     end
260   rescue ActiveRecord::RecordNotFound
261     render :nothing => true, :status => :not_found
262   end
263
264   def icon
265     trace = Trace.find(params[:id])
266
267     if trace.inserted?
268       if trace.public? or (@user and @user == trace.user)
269         expires_in 7.days, :private => !trace.public, :public => trace.public
270         send_file(trace.icon_picture_name, :filename => "#{trace.id}_icon.gif", :type => 'image/gif', :disposition => 'inline')
271       else
272         render :nothing => true, :status => :forbidden
273       end
274     else
275       render :nothing => true, :status => :not_found
276     end
277   rescue ActiveRecord::RecordNotFound
278     render :nothing => true, :status => :not_found
279   end
280
281   def api_details
282     trace = Trace.find(params[:id])
283
284     if trace.public? or trace.user == @user
285       render :text => trace.to_xml.to_s, :content_type => "text/xml"
286     else
287       render :nothing => true, :status => :forbidden
288     end
289   rescue ActiveRecord::RecordNotFound
290     render :nothing => true, :status => :not_found
291   end
292
293   def api_data
294     trace = Trace.find(params[:id])
295
296     if trace.public? or trace.user == @user
297       send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => 'attachment')
298     else
299       render :nothing => true, :status => :forbidden
300     end
301   rescue ActiveRecord::RecordNotFound
302     render :nothing => true, :status => :not_found
303   end
304
305   def api_create
306     if request.post?
307       tags = params[:tags] || ""
308       description = params[:description] || ""
309       pub = params[:public] || false
310       
311       if params[:file].respond_to?(:read)
312         do_create(params[:file], tags, description, pub)
313
314         if @trace.id
315           render :text => @trace.id.to_s, :content_type => "text/plain"
316         elsif @trace.valid?
317           render :nothing => true, :status => :internal_server_error
318         else
319           render :nothing => true, :status => :bad_request
320         end
321       else
322         render :nothing => true, :status => :bad_request
323       end
324     else
325       render :nothing => true, :status => :method_not_allowed
326     end
327   end
328
329 private
330
331   def do_create(file, tags, description, public)
332     # Sanitise the user's filename
333     name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, '_')
334
335     # Get a temporary filename...
336     filename = "/tmp/#{rand}"
337
338     # ...and save the uploaded file to that location
339     File.open(filename, "w") { |f| f.write(file.read) }
340
341     # Create the trace object, falsely marked as already
342     # inserted to stop the import daemon trying to load it
343     @trace = Trace.new({
344       :name => name,
345       :tagstring => tags,
346       :description => description,
347       :public => public,
348       :inserted => true,
349       :user => @user,
350       :timestamp => Time.now.getutc
351     })
352
353     # Save the trace object
354     if @trace.save
355       # Rename the temporary file to the final name
356       FileUtils.mv(filename, @trace.trace_name)
357
358       # Clear the inserted flag to make the import daemon load the trace
359       @trace.inserted = false
360       @trace.save!
361     else
362       # Remove the file as we have failed to update the database
363       FileUtils.rm_f(filename)
364     end
365     
366     # Finally save whether the user marked the trace as being public
367     if @trace.public?
368       if @user.trace_public_default.nil?
369         @user.preferences.create(:k => "gps.trace.public", :v => "default")
370       end
371     else
372       pref = @user.trace_public_default
373       pref.destroy unless pref.nil?
374     end
375     
376   end
377
378 end