Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: eventmachine/eventmachine
...
head fork: lmars/eventmachine
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 3 files changed
  • 0 commit comments
  • 1 contributor
View
2  lib/em/connection.rb
@@ -637,6 +637,8 @@ def send_file_data filename
#
# @option args [Boolean] :http_chunks (false) If true, this method will stream the file data in a format
# compatible with the HTTP chunked-transfer encoding
+ # @option args [Fixnum] :position (0) Position (in bytes) to start reading the file from. Only
+ # applies to files larger than 16k.
#
# @param [String] filename Local path of the file to stream
# @param [Hash] args Options
View
10 lib/em/streamer.rb
@@ -33,16 +33,23 @@ class FileStreamer
# @param [String] filename File path
#
# @option args [Boolean] :http_chunks (false) Use HTTP 1.1 style chunked-encoding semantics.
+ # @option args [Fixnum] :position (0) Position (in bytes) to start reading the file
+ # from. Only applies to files larger than 16k.
def initialize connection, filename, args = {}
@connection = connection
@http_chunks = args[:http_chunks]
+ @position = args[:position] || 0
if File.exist?(filename)
@size = File.size(filename)
if @size <= MappingThreshold
stream_without_mapping filename
else
- stream_with_mapping filename
+ if @position > @size
+ fail "position #{@position} is past the end of the file"
+ else
+ stream_with_mapping filename
+ end
end
else
fail "file not found"
@@ -66,7 +73,6 @@ def stream_without_mapping filename # :nodoc:
def stream_with_mapping filename # :nodoc:
ensure_mapping_extension_is_present
- @position = 0
@mapping = EventMachine::FastFileReader::Mapper.new filename
stream_one_chunk
end
View
34 tests/test_send_file.rb
@@ -170,9 +170,21 @@ def test_em_send_file_data_not_implemented
begin
require 'fastfilereaderext'
+ module PositionStreamTestModule
+ def initialize filename
+ @filename = filename
+ end
+
+ def post_init
+ EM::Deferrable.future( stream_file_data(@filename, :position => 50000)) {
+ close_connection_after_writing
+ }
+ end
+ end
+
def test_stream_large_file_data
File.open( @filename, "w" ) {|f|
- f << ("A" * 10000)
+ f << ("A" * 100000)
}
data = ''
@@ -185,7 +197,7 @@ def test_stream_large_file_data
end
}
- assert_equal( "A" * 10000, data )
+ assert_equal( "A" * 100000, data )
end
def test_stream_large_chunked_file_data
@@ -210,6 +222,24 @@ def test_stream_large_chunked_file_data
].join
assert_equal( expected, data )
end
+
+ def test_stream_large_file_data_from_position
+ File.open( @filename, "w" ) {|f|
+ f << ("A" * 100000)
+ }
+
+ data = ''
+
+ EM.run {
+ EM.start_server "127.0.0.1", @port, PositionStreamTestModule, @filename
+ setup_timeout
+ EM.connect "127.0.0.1", @port, TestClient do |c|
+ c.data_to { |d| data << d }
+ end
+ }
+
+ assert_equal( "A" * 50000, data )
+ end
rescue LoadError
warn "require 'fastfilereaderext' failed, skipping tests in #{__FILE__}"
end

No commit comments for this range

Something went wrong with that request. Please try again.