From: Steve Coast Date: Sat, 7 Apr 2007 17:48:34 +0000 (+0000) Subject: GPX api done X-Git-Tag: live~8532 X-Git-Url: https://git.openstreetmap.org/rails.git/commitdiff_plain/01cfcbd845c5d8d33f83bb22a24cf35932da2e29?ds=sidebyside GPX api done --- diff --git a/app/controllers/trace_controller.rb b/app/controllers/trace_controller.rb index 3614dadde..fcf66dac1 100644 --- a/app/controllers/trace_controller.rb +++ b/app/controllers/trace_controller.rb @@ -1,5 +1,6 @@ class TraceController < ApplicationController before_filter :authorize_web + before_filter :authorize, :only => [:api_details, :api_data, :api_create] layout 'site' # Counts and selects pages of GPX traces for various criteria (by user, tags, public etc.). @@ -159,4 +160,41 @@ class TraceController < ApplicationController trace = Trace.find(params[:id]) send_data(trace.icon_picture, :filename => "#{trace.id}_icon.gif", :type => 'image/gif', :disposition => 'inline') if trace.public? or (@user and @user == trace.user) end + + def api_details + trace = Trace.find(params[:id]) + doc = OSM::API.new.get_xml_doc + doc.root << trace.to_xml_node() if trace.public? or trace.user == @user + render :text => doc.to_s + end + + def api_data + render :action => 'data' + end + + def api_create + #FIXME merge this code with create as they're pretty similar? + + filename = "/tmp/#{rand}" + File.open(filename, "w") { |f| f.write(request.raw_post) } + @params['trace'] = {} + @params['trace']['name'] = params[:filename] + @params['trace']['tagstring'] = params[:tags] + @params['trace']['description'] = params[:description] + @trace = Trace.new(@params['trace']) + @trace.inserted = false + @trace.user = @user + @trace.timestamp = Time.now + + if @trace.save + saved_filename = "/tmp/#{@trace.id}.gpx" + File.rename(filename, saved_filename) + 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." + render :nothing => true + else + render :nothing => true, :status => 400 # er FIXME what fricking code to return? + end + + end end diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 761468b98..f3ec2e243 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -1,7 +1,7 @@ class UserController < ApplicationController layout 'site' - before_filter :authorize, :only => [:preferences, :api_details] + before_filter :authorize, :only => [:preferences, :api_details, :api_gpx_files] before_filter :authorize_web, :only => [:rename, :account, :go_public] before_filter :require_user, :only => [:rename, :account, :go_public] @@ -129,5 +129,13 @@ class UserController < ApplicationController def api_details render :text => @user.to_xml.to_s end + + def api_gpx_files + doc = OSM::API.new.get_xml_doc + @user.traces.each do |trace| + doc.root << trace.to_xml_node() if trace.public? or trace.user == @user + end + render :text => doc.to_s + end end diff --git a/app/models/trace.rb b/app/models/trace.rb index aa68d9b94..e98b0c67c 100644 --- a/app/models/trace.rb +++ b/app/models/trace.rb @@ -54,4 +54,16 @@ class Trace < ActiveRecord::Base def icon_picture_name "/tmp/#{id}_icon.gif" end + + def to_xml_node + el1 = XML::Node.new 'gpx_file' + el1['id'] = self.id.to_s + el1['name'] = self.name.to_s + el1['lat'] = self.latitude.to_s + el1['lon'] = self.longitude.to_s + el1['user'] = self.user.display_name + el1['public'] = self.user.public + el1['timestamp'] = self.timestamp.xmlschema + return el1 + end end diff --git a/config/routes.rb b/config/routes.rb index 20bf02ce7..1a3d2f85d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -19,6 +19,11 @@ ActionController::Routing::Routes.draw do |map| map.connect "api/#{API_VERSION}/map", :controller => 'api', :action => 'map' map.connect "api/#{API_VERSION}/user/details", :controller => 'user', :action => 'api_details' + map.connect "api/#{API_VERSION}/user/gpx_files", :controller => 'user', :action => 'api_gpx_files' + + map.connect "api/#{API_VERSION}/gpx/create/:filename/:description/:tags", :controller => 'trace', :action => 'api_create' + map.connect "api/#{API_VERSION}/gpx/:id/details", :controller => 'trace', :action => 'api_details' + map.connect "api/#{API_VERSION}/gpx/:id/data", :controller => 'trace', :action => 'api_data' # web site