Class: Neofiles::DataStore::Mongo

Inherits:
Object
  • Object
show all
Defined in:
app/models/neofiles/data_store/mongo.rb

Defined Under Namespace

Modules: FileHelper

Constant Summary collapse

DEFAULT_CHUNK_SIZE =
Rails.application.config.neofiles.mongo_default_chunk_size

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Mongo

Returns a new instance of Mongo.



21
22
23
# File 'app/models/neofiles/data_store/mongo.rb', line 21

def initialize(id)
  @id = id
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



19
20
21
# File 'app/models/neofiles/data_store/mongo.rb', line 19

def data
  @data
end

#idObject (readonly)

Returns the value of attribute id.



19
20
21
# File 'app/models/neofiles/data_store/mongo.rb', line 19

def id
  @id
end

#lengthObject (readonly)

Returns the value of attribute length.



19
20
21
# File 'app/models/neofiles/data_store/mongo.rb', line 19

def length
  @length
end

#md5Object (readonly)

Returns the value of attribute md5.



19
20
21
# File 'app/models/neofiles/data_store/mongo.rb', line 19

def md5
  @md5
end

Class Method Details

.chunks(id) ⇒ Object



5
6
7
# File 'app/models/neofiles/data_store/mongo.rb', line 5

def self.chunks(id)
  Neofiles::FileChunk.where(file_id: id).order_by(n: :asc)
end

.find(id) ⇒ Object



9
10
11
12
13
14
15
# File 'app/models/neofiles/data_store/mongo.rb', line 9

def self.find(id)
  if chunks(id).any?
    new(id)
  else
    raise Neofiles::DataStore::NotFoundException
  end
end

Instance Method Details

#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