]> git.openstreetmap.org Git - rails.git/commitdiff
Use send_data for GPX traces intead of monkey patching send_file
authorTom Hughes <tom@compton.nu>
Mon, 9 Oct 2017 19:38:08 +0000 (20:38 +0100)
committerTom Hughes <tom@compton.nu>
Mon, 9 Oct 2017 19:38:08 +0000 (20:38 +0100)
app/controllers/trace_controller.rb
config/initializers/streaming.rb [deleted file]
test/controllers/trace_controller_test.rb

index e61e51e55cbc28367e6ed82d10ba674c6eba8605..105405ccfdad7e0fd11b525443a4786ed496f307 100644 (file)
@@ -149,9 +149,9 @@ class TraceController < ApplicationController
       if Acl.no_trace_download(request.remote_ip)
         head :forbidden
       elsif request.format == Mime[:xml]
       if Acl.no_trace_download(request.remote_ip)
         head :forbidden
       elsif request.format == Mime[:xml]
-        send_file(trace.xml_file, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
+        send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
       elsif request.format == Mime[:gpx]
       elsif request.format == Mime[:gpx]
-        send_file(trace.xml_file, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
+        send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
       else
         send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
       end
       else
         send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
       end
@@ -290,9 +290,9 @@ class TraceController < ApplicationController
 
     if trace.public? || trace.user == current_user
       if request.format == Mime[:xml]
 
     if trace.public? || trace.user == current_user
       if request.format == Mime[:xml]
-        send_file(trace.xml_file, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
+        send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
       elsif request.format == Mime[:gpx]
       elsif request.format == Mime[:gpx]
-        send_file(trace.xml_file, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
+        send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
       else
         send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
       end
       else
         send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
       end
diff --git a/config/initializers/streaming.rb b/config/initializers/streaming.rb
deleted file mode 100644 (file)
index 0c27ba2..0000000
+++ /dev/null
@@ -1,23 +0,0 @@
-# Hack ActionController::DataStreaming to allow streaming from a file handle
-module ActionController
-  module DataStreaming
-    alias old_send_file send_file
-
-    def send_file(file, options = {})
-      if file.is_a?(File) || file.is_a?(Tempfile)
-        headers["Content-Length"] ||= file.size.to_s
-
-        options[:filename] ||= File.basename(file.path) unless options[:url_based_filename]
-        send_file_headers! options
-
-        self.status = options[:status] || 200
-        self.content_type = options[:content_type] if options.key?(:content_type)
-        self.response_body = file
-      else
-        headers["Content-Length"] ||= File.size(file).to_s
-
-        old_send_file(file, options)
-      end
-    end
-  end
-end
index a23b5947359506b2f659ef211663066f86572739..182fe8c0295444af61b99c1bf0e63a1d6d84014e 100644 (file)
@@ -1,4 +1,5 @@
 require "test_helper"
 require "test_helper"
+require "digest"
 require "minitest/mock"
 
 class TraceControllerTest < ActionController::TestCase
 require "minitest/mock"
 
 class TraceControllerTest < ActionController::TestCase
@@ -355,15 +356,15 @@ class TraceControllerTest < ActionController::TestCase
 
     # First with no auth, which should work since the trace is public
     get :data, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
 
     # First with no auth, which should work since the trace is public
     get :data, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
-    check_trace_data public_trace_file
+    check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
 
     # Now with some other user, which should work since the trace is public
     get :data, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
 
     # Now with some other user, which should work since the trace is public
     get :data, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
-    check_trace_data public_trace_file
+    check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
 
     # And finally we should be able to do it with the owner of the trace
     get :data, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
 
     # And finally we should be able to do it with the owner of the trace
     get :data, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
-    check_trace_data public_trace_file
+    check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
   end
 
   # Test downloading a compressed trace
   end
 
   # Test downloading a compressed trace
@@ -372,15 +373,15 @@ class TraceControllerTest < ActionController::TestCase
 
     # First get the data as is
     get :data, :params => { :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id }
 
     # First get the data as is
     get :data, :params => { :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id }
-    check_trace_data identifiable_trace_file, "application/x-gzip", "gpx.gz"
+    check_trace_data identifiable_trace_file, "c6422a3d8750faae49ed70e7e8a51b93", "application/x-gzip", "gpx.gz"
 
     # Now ask explicitly for XML format
     get :data, :params => { :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id, :format => "xml" }
 
     # Now ask explicitly for XML format
     get :data, :params => { :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id, :format => "xml" }
-    check_trace_data identifiable_trace_file, "application/xml", "xml"
+    check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d", "application/xml", "xml"
 
     # Now ask explicitly for GPX format
     get :data, :params => { :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id, :format => "gpx" }
 
     # Now ask explicitly for GPX format
     get :data, :params => { :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id, :format => "gpx" }
-    check_trace_data identifiable_trace_file
+    check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d"
   end
 
   # Check an anonymous trace can't be downloaded by another user
   end
 
   # Check an anonymous trace can't be downloaded by another user
@@ -397,7 +398,7 @@ class TraceControllerTest < ActionController::TestCase
 
     # And finally we should be able to do it with the owner of the trace
     get :data, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user }
 
     # And finally we should be able to do it with the owner of the trace
     get :data, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user }
-    check_trace_data anon_trace_file
+    check_trace_data anon_trace_file, "66179ca44f1e93d8df62e2b88cbea732"
   end
 
   # Test downloading a trace that doesn't exist
   end
 
   # Test downloading a trace that doesn't exist
@@ -751,12 +752,12 @@ class TraceControllerTest < ActionController::TestCase
     # Now with some other user, which should work since the trace is public
     basic_authorization create(:user).display_name, "test"
     get :api_data, :params => { :id => public_trace_file.id }
     # Now with some other user, which should work since the trace is public
     basic_authorization create(:user).display_name, "test"
     get :api_data, :params => { :id => public_trace_file.id }
-    check_trace_data public_trace_file
+    check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
 
     # And finally we should be able to do it with the owner of the trace
     basic_authorization public_trace_file.user.display_name, "test"
     get :api_data, :params => { :id => public_trace_file.id }
 
     # And finally we should be able to do it with the owner of the trace
     basic_authorization public_trace_file.user.display_name, "test"
     get :api_data, :params => { :id => public_trace_file.id }
-    check_trace_data public_trace_file
+    check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
   end
 
   # Test downloading a compressed trace through the api
   end
 
   # Test downloading a compressed trace through the api
@@ -768,15 +769,15 @@ class TraceControllerTest < ActionController::TestCase
 
     # First get the data as is
     get :api_data, :params => { :id => identifiable_trace_file.id }
 
     # First get the data as is
     get :api_data, :params => { :id => identifiable_trace_file.id }
-    check_trace_data identifiable_trace_file, "application/x-gzip", "gpx.gz"
+    check_trace_data identifiable_trace_file, "c6422a3d8750faae49ed70e7e8a51b93", "application/x-gzip", "gpx.gz"
 
     # Now ask explicitly for XML format
     get :api_data, :params => { :id => identifiable_trace_file.id, :format => "xml" }
 
     # Now ask explicitly for XML format
     get :api_data, :params => { :id => identifiable_trace_file.id, :format => "xml" }
-    check_trace_data identifiable_trace_file, "application/xml", "xml"
+    check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d", "application/xml", "xml"
 
     # Now ask explicitly for GPX format
     get :api_data, :params => { :id => identifiable_trace_file.id, :format => "gpx" }
 
     # Now ask explicitly for GPX format
     get :api_data, :params => { :id => identifiable_trace_file.id, :format => "gpx" }
-    check_trace_data identifiable_trace_file
+    check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d"
   end
 
   # Check an anonymous trace can't be downloaded by another user through the api
   end
 
   # Check an anonymous trace can't be downloaded by another user through the api
@@ -795,7 +796,7 @@ class TraceControllerTest < ActionController::TestCase
     # And finally we should be able to do it with the owner of the trace
     basic_authorization anon_trace_file.user.display_name, "test"
     get :api_data, :params => { :id => anon_trace_file.id }
     # And finally we should be able to do it with the owner of the trace
     basic_authorization anon_trace_file.user.display_name, "test"
     get :api_data, :params => { :id => anon_trace_file.id }
-    check_trace_data anon_trace_file
+    check_trace_data anon_trace_file, "66179ca44f1e93d8df62e2b88cbea732"
   end
 
   # Test downloading a trace that doesn't exist through the api
   end
 
   # Test downloading a trace that doesn't exist through the api
@@ -1034,8 +1035,9 @@ class TraceControllerTest < ActionController::TestCase
     end
   end
 
     end
   end
 
-  def check_trace_data(trace, content_type = "application/gpx+xml", extension = "gpx")
+  def check_trace_data(trace, digest, content_type = "application/gpx+xml", extension = "gpx")
     assert_response :success
     assert_response :success
+    assert_equal digest, Digest::MD5.hexdigest(response.body)
     assert_equal content_type, response.content_type
     assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"", @response.header["Content-Disposition"]
   end
     assert_equal content_type, response.content_type
     assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"", @response.header["Content-Disposition"]
   end