- File.open(filename, "w") { |f| f.write(@params['trace']['gpx_file'].read) }
- @params['trace']['name'] = @params['trace']['gpx_file'].original_filename.gsub(/[^a-zA-Z0-9.]/, '_') # This makes sure filenames are sane
- @params['trace'].delete('gpx_file') # let's remove the field from the hash, because there's no such field in the DB anyway.
- @trace = Trace.new(@params['trace'])
- @trace.inserted = false
- @trace.user_id = @user.id
- @trace.timestamp = Time.now
- if @trace.save
- logger.info("id is #{@trace.id}")
- `mv #{filename} /tmp/#{@trace.id}.gpx`
- flash[:notice] = "Your GPX file has been uploaded and is awaiting insertion in to the database. This will usually happen within half an hour, and an email will be sent to you on completion."
+ if @trace.id
+ logger.info("id is #{@trace.id}")
+ flash[:notice] = "Your GPX file has been uploaded and is awaiting insertion in to the database. This will usually happen within half an hour, and an email will be sent to you on completion."
+
+ redirect_to :action => 'mine'
+ end
+ else
+ @trace = Trace.new({:name => "Dummy",
+ :tagstring => params[:trace][:tagstring],
+ :description => params[:trace][:description],
+ :public => params[:trace][:public],
+ :inserted => false, :user => @user,
+ :timestamp => Time.now})
+ @trace.valid?
+ @trace.errors.add(:gpx_file, "can't be blank")
+ end
+ end
+
+ def data
+ trace = Trace.find(params[:id])
+
+ if trace.visible? and (trace.public? or (@user and @user == trace.user))
+ send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => 'attachment')
+ else
+ render :nothing, :status => :not_found
+ end
+ rescue ActiveRecord::RecordNotFound
+ render :nothing => true, :status => :not_found
+ end
+
+ def edit
+ @trace = Trace.find(params[:id])
+
+ if @user and @trace.user == @user
+ if params[:trace]
+ @trace.description = params[:trace][:description]
+ @trace.tagstring = params[:trace][:tagstring]
+ if @trace.save
+ redirect_to :action => 'view'
+ end
+ end
+ else
+ render :nothing, :status => :forbidden
+ end
+ rescue ActiveRecord::RecordNotFound
+ render :nothing => true, :status => :not_found
+ end
+
+ def delete
+ trace = Trace.find(params[:id])
+
+ if @user and trace.user == @user
+ if request.post? and trace.visible?
+ trace.visible = false
+ trace.save
+ flash[:notice] = 'Track scheduled for deletion'
+ redirect_to :controller => 'traces', :action => 'mine'
+ else
+ render :nothing, :status => :bad_request
+ end
+ else
+ render :nothing, :status => :forbidden
+ end
+ rescue ActiveRecord::RecordNotFound
+ render :nothing => true, :status => :not_found
+ end
+
+ def make_public
+ trace = Trace.find(params[:id])
+
+ if @user and trace.user == @user
+ if request.post? and !trace.public?
+ trace.public = true
+ trace.save
+ flash[:notice] = 'Track made public'
+ redirect_to :controller => 'trace', :action => 'view', :id => params[:id]
+ else
+ render :nothing, :status => :bad_request
+ end
+ else
+ render :nothing, :status => :forbidden
+ end
+ rescue ActiveRecord::RecordNotFound
+ render :nothing => true, :status => :not_found
+ end
+
+ def georss
+ conditions = ["gpx_files.public = 1"]
+
+ if params[:display_name]
+ conditions[0] += " AND users.display_name = ?"
+ conditions << params[:display_name]
+ end
+
+ if params[:tag]
+ conditions[0] += " AND EXISTS (SELECT * FROM gpx_file_tags AS gft WHERE gft.gpx_id = gpx_files.id AND gft.tag = ?)"
+ conditions << params[:tag]
+ end
+
+ traces = Trace.find(:all, :include => :user, :conditions => conditions,
+ :order => "timestamp DESC", :limit => 20)
+
+ rss = OSM::GeoRSS.new
+
+ traces.each do |trace|
+ 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)