class Mongo::Grid::File::Info

Encapsulates behaviour around GridFS files collection file document.

@since 2.0.0

Constants

COLLECTION

Name of the files collection.

@since 2.0.0

DEFAULT_CONTENT_TYPE

Default content type for stored files.

@since 2.0.0

MAPPINGS

Mappings of user supplied fields to db specification.

@since 2.0.0

Attributes

document[R]

@return [ BSON::Document ] document The files collection document.

Public Class Methods

new(document) click to toggle source

Create the new file information document.

@example Create the new file information document.

Info.new(:filename => 'test.txt')

@param [ BSON::Document ] document The document to create from.

@since 2.0.0

# File lib/mongo/grid/file/info.rb, line 133
def initialize(document)
  @document = default_document.merge(Options::Mapper.transform(document, MAPPINGS))
  @client_md5 = Digest::MD5.new
end

Public Instance Methods

==(other) click to toggle source

Is this file information document equal to another?

@example Check file information document equality.

file_info == other

@param [ Object ] other The object to check against.

@return [ true, false ] If the objects are equal.

@since 2.0.0

# File lib/mongo/grid/file/info.rb, line 62
def ==(other)
  return false unless other.is_a?(Info)
  document == other.document
end
bson_type() click to toggle source

Get the BSON type for a files information document.

@example Get the BSON type.

file_info.bson_type

@return [ Integer ] The BSON type.

@since 2.0.0

# File lib/mongo/grid/file/info.rb, line 75
def bson_type
  BSON::Hash::BSON_TYPE
end
chunk_size() click to toggle source

Get the file chunk size.

@example Get the chunk size.

file_info.chunk_size

@return [ Integer ] The chunksize in bytes.

@since 2.0.0

# File lib/mongo/grid/file/info.rb, line 87
def chunk_size
  document[:chunkSize]
end
content_type() click to toggle source

Get the file information content type.

@example Get the content type.

file_info.content_type

@return [ String ] The content type.

@since 2.0.0

# File lib/mongo/grid/file/info.rb, line 99
def content_type
  document[:contentType]
end
filename() click to toggle source

Get the filename from the file information.

@example Get the filename.

file_info.filename

@return [ String ] The filename.

# File lib/mongo/grid/file/info.rb, line 109
def filename
  document[:filename]
end
id() click to toggle source

Get the file id from the file information.

@example Get the file id.

file_info.id

@return [ BSON::ObjectId ] The file id.

@since 2.0.0

# File lib/mongo/grid/file/info.rb, line 121
def id
  document[:_id]
end
inspect() click to toggle source

Get a readable inspection for the object.

@example Inspect the file information.

file_info.inspect

@return [ String ] The nice inspection.

@since 2.0.0

# File lib/mongo/grid/file/info.rb, line 146
def inspect
  "#<Mongo::Grid::File::Info:0x#{object_id} chunk_size=#{chunk_size} " +
    "filename=#{filename} content_type=#{content_type} id=#{id} md5=#{md5}>"
end
length() click to toggle source

Get the length of the document in bytes.

@example Get the file length from the file information document.

file_info.length

@return [ Integer ] The file length.

@since 2.0.0

# File lib/mongo/grid/file/info.rb, line 159
def length
  document[:length]
end
Also aliased as: size
md5() click to toggle source

Get the md5 hash.

@example Get the md5 hash.

file_info.md5

@return [ String ] The md5 hash as a string.

@since 2.0.0

# File lib/mongo/grid/file/info.rb, line 184
def md5
  document[:md5] || @client_md5
end
metadata() click to toggle source

Get the additional metadata from the file information document.

@example Get additional metadata.

file_info.metadata

@return [ String ] The additional metadata from file information document.

@since 2.0.0

# File lib/mongo/grid/file/info.rb, line 172
def metadata
  document[:metadata]
end
size()
Alias for: length
to_bson(encoded = ''.force_encoding(BSON::BINARY)) click to toggle source

Convert the file information document to BSON for storage.

@note If no md5 exists in the file information document (it was loaded

from the server and is not a new file) then we digest the md5 and set it.

@example Convert the file information document to BSON.

file_info.to_bson

@param [ String ] encoded The encoded data to append to.

@return [ String ] The raw BSON data.

@since 2.0.0

# File lib/mongo/grid/file/info.rb, line 201
def to_bson(encoded = ''.force_encoding(BSON::BINARY))
  document[:md5] ||= @client_md5.hexdigest
  document.to_bson(encoded)
end
upload_date() click to toggle source

Get the upload date.

@example Get the upload date.

file_info.upload_date

@return [ Time ] The upload date.

@since 2.0.0

# File lib/mongo/grid/file/info.rb, line 214
def upload_date
  document[:uploadDate]
end

Private Instance Methods

default_document() click to toggle source
# File lib/mongo/grid/file/info.rb, line 220
def default_document
  BSON::Document.new(
    :_id => BSON::ObjectId.new,
    :chunkSize => Chunk::DEFAULT_SIZE,
    :uploadDate => Time.now.utc,
    :contentType => DEFAULT_CONTENT_TYPE
  )
end