Method: Neofiles::DataStore::Mongo#write

Defined in:
app/models/neofiles/data_store/mongo.rb

#write(data) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/neofiles/data_store/mongo.rb', line 41

def write(data)
  data_buf  = []
  md5       = Digest::MD5.new
  length, n = 0, 0

  reading(data) do |io|
    chunking(io, DEFAULT_CHUNK_SIZE) do |buf|
      md5 << buf
      data_buf << buf
      length += buf.size
      chunk = chunks.build file_id: id
      chunk.data = binary_for buf
      chunk.n = n
      n += 1
      chunk.save!
    end
  end

  @data   = data_buf.join
  @length = length
  @md5    = md5.hexdigest
end