mogilefs-client.git  about / heads / tags
MogileFS client library for Ruby
blob 7d914526ca05e9206337fd4c8064d625be56a803 796 bytes (raw)
$ git show HEAD:lib/mogilefs/chunker.rb	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 
# -*- encoding: binary -*-
class MogileFS::Chunker
  attr_reader :io

  def initialize(io, md5, expect_md5)
    @io = io
    @md5 = md5
    @expect_md5 = expect_md5
  end

  def write(buf)
    rv = buf.bytesize
    @io.write("#{rv.to_s(16)}\r\n")
    @io.write(buf)
    @md5.update(buf) if @md5
    @io.write("\r\n".freeze)
    rv
  end

  def flush
    if @md5
      content_md5 = [ @md5.digest ].pack('m').rstrip!
      if @expect_md5.respond_to?(:call)
        expect = @expect_md5.call.strip
        if expect != content_md5
          raise MogileFS::ChecksumMismatchError,
            "expected: #{expect.inspect} actual: #{content_md5.inspect}"
        end
      end
      @io.write("0\r\nContent-MD5: #{content_md5}\r\n\r\n")
    else
      @io.write("0\r\n\r\n".freeze)
    end
  end
end

git clone https://yhbt.net/mogilefs-client.git