apply patch from TomH for #477 and dont require http auth on GET to the API.
authorSteve Coast <steve@asklater.com>
Thu, 24 May 2007 11:20:16 +0000 (11:20 +0000)
committerSteve Coast <steve@asklater.com>
Thu, 24 May 2007 11:20:16 +0000 (11:20 +0000)
app/controllers/application.rb
lib/daemons/gpx_import.rb

index fd6ea786675a3f9e0d7472298eb3fb5948bcae63..52aeff3cbe4c79db9e1539d0772ba72a8f24ed0c 100644 (file)
@@ -11,31 +11,33 @@ class ApplicationController < ActionController::Base
   end
 
   def authorize(realm='Web Password', errormessage="Couldn't authenticate you") 
-    username, passwd = get_auth_data # parse from headers
-    # authenticate per-scheme
-    if username.nil?
-      @user = nil # no authentication provided - perhaps first connect (client should retry after 401)
-    elsif username == 'token' 
-      @user = User.authenticate_token(passwd) # preferred - random token for user from db, passed in basic auth
-    else
-      @user = User.authenticate(username, passwd) # basic auth
+    unless request.get?
+      username, passwd = get_auth_data # parse from headers
+      # authenticate per-scheme
+      if username.nil?
+        @user = nil # no authentication provided - perhaps first connect (client should retry after 401)
+      elsif username == 'token' 
+        @user = User.authenticate_token(passwd) # preferred - random token for user from db, passed in basic auth
+      else
+        @user = User.authenticate(username, passwd) # basic auth
+      end
+
+      # handle authenticate pass/fail
+      if @user
+        # user exists and password is correct ... horray! 
+        if @user.methods.include? 'lastlogin'         # note last login 
+          @session['lastlogin'] = user.lastlogin 
+          @user.last.login = Time.now 
+          @user.save() 
+          @session["User.id"] = @user.id 
+        end             
+      else 
+        # no auth, the user does not exist or the password was wrong
+        response.headers["Status"] = "Unauthorized" 
+        response.headers["WWW-Authenticate"] = "Basic realm=\"#{realm}\"" 
+        render_text(errormessage, 401) # :unauthorized
+      end 
     end
-    
-    # handle authenticate pass/fail
-    if @user
-      # user exists and password is correct ... horray! 
-      if @user.methods.include? 'lastlogin'         # note last login 
-        @session['lastlogin'] = user.lastlogin 
-        @user.last.login = Time.now 
-        @user.save() 
-        @session["User.id"] = @user.id 
-      end             
-    else 
-      # no auth, the user does not exist or the password was wrong
-      response.headers["Status"] = "Unauthorized" 
-      response.headers["WWW-Authenticate"] = "Basic realm=\"#{realm}\"" 
-      render_text(errormessage, 401) # :unauthorized
-    end 
   end 
 
   # Report and error to the user
index 82232e496c3e47ae5bf2a94bd911d7a12b7eb7d8..acf2153001bb065ddf4a04e879f1ed375cb11711 100755 (executable)
@@ -23,17 +23,26 @@ while($running) do
       begin
 
         logger.info("GPX Import importing #{trace.name} (#{trace.id}) from #{trace.user.email}")
-        \r
+
         # TODO *nix specific, could do to work on windows... would be functionally inferior though - check for '.gz'
-        gzipped = `file -b /home/osm/gpx/#{trace.id}.gpx`.chomp =~/^gzip/\r
+        filetype = `file -b /home/osm/gpx/#{trace.id}.gpx`.chomp
+        gzipped = filetype =~ /^gzip/
+        zipped = filetype =~ /^Zip/
 
         if gzipped
           logger.info("gzipped")
+          filename = "/tmp/#{rand}"
+          system("gunzip -c /home/osm/gpx/#{trace.id}.gpx > #{filename}")
+        elsif zipped
+          logger.info("zipped")
+          filename = "/tmp/#{rand}"
+          system("unzip -p /home/osm/gpx/#{trace.id}.gpx > #{filename}")
         else
           logger.info("not gzipped")
+          filename = "/home/osm/gpx/#{trace.id}.gpx"
         end
 
-        gpx = OSM::GPXImporter.new("/home/osm/gpx/#{trace.id}.gpx")
+        gpx = OSM::GPXImporter.new(filename)
 
         f_lat = 0
         f_lon = 0
@@ -74,10 +83,14 @@ while($running) do
           trace.inserted = true
           trace.save
 
+          if gzipped || zipped
+            FileUtils.rm_f(filename)
+          end
+
           logger.info "done trace #{trace.id}"
           Notifier::deliver_gpx_success(trace, gpx.possible_points)
         else
-          `rm /home/osm/gpx/#{trace.id}.gpx`
+          FileUtils.rm_f("/home/osm/gpx/#{trace.id}.gpx", filename)
           trace.destroy
           Notifier::deliver_gpx_failure(trace, '0 points parsed ok. Do they all have lat,lng,alt,timestamp?')
         end
@@ -85,7 +98,7 @@ while($running) do
       rescue Exception => ex
         logger.info ex
         ex.backtrace.each {|l| logger.info l }
-          `rm /home/osm/gpx/#{trace.id}.gpx`
+          FileUtils.rm_f("/home/osm/gpx/#{trace.id}.gpx", filename)
           trace.destroy
           Notifier::deliver_gpx_failure(trace, ex.to_s + ex.backtrace.join("\n") )
       end
@@ -93,7 +106,7 @@ while($running) do
   end
 
   Trace.find(:all, :conditions => ['inserted = ?', false]).each do |trace|
-     `rm /home/osm/gpx/#{trace.id}.gpx`
+     FileUtils.rm_f("/home/osm/gpx/#{trace.id}.gpx")
      trace.destroy
   end
   exit