123456789_123456789_123456789_123456789_123456789_

Class: Mongo::Grid::File::Info Deprecated

Relationships & Source Files
Inherits: Object
Defined in: lib/mongo/grid/file/info.rb

Overview

Deprecated.

Please use the ‘stream’ API on a ::Mongo::Grid::FSBucket instead. Will be removed in driver version 3.0.

Encapsulates behavior around GridFS files collection file document.

Since:

  • 2.0.0

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(document) ⇒ Info

Create the new file information document.

Examples:

Create the new file information document.

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

Parameters:

  • document (BSON::Document)

    The document to create from.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/file/info.rb', line 139

def initialize(document)
  @client_md5 = Digest::MD5.new unless document[:disable_md5] == true
  # document contains a mix of user options and keys added
  # internally by the driver, like session.
  # Remove the keys that driver adds but keep user options.
  document = document.reject do |key, value|
    key.to_s == 'session'
  end
  @document = default_document.merge(Options::Mapper.transform(document, MAPPINGS))
end

Instance Attribute Details

#documentBSON::Document (readonly)

Returns:

  • (BSON::Document)

    document The files collection document.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/file/info.rb', line 56

attr_reader :document

Instance Method Details

#==(other) ⇒ true, false

Is this file information document equal to another?

Examples:

Check file information document equality.

file_info == other

Parameters:

  • other (Object)

    The object to check against.

Returns:

  • (true, false)

    If the objects are equal.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/file/info.rb', line 68

def ==(other)
  return false unless other.is_a?(Info)
  document == other.document
end

#bson_typeInteger

Get the BSON type for a files information document.

Examples:

Get the BSON type.

file_info.bson_type

Returns:

  • (Integer)

    The BSON type.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/file/info.rb', line 81

def bson_type
  BSON::Hash::BSON_TYPE
end

#chunk_sizeInteger

Get the file chunk size.

Examples:

Get the chunk size.

file_info.chunk_size

Returns:

  • (Integer)

    The chunksize in bytes.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/file/info.rb', line 93

def chunk_size
  document[:chunkSize]
end

#content_typeString

Get the file information content type.

Examples:

Get the content type.

file_info.content_type

Returns:

  • (String)

    The content type.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/file/info.rb', line 105

def content_type
  document[:contentType]
end

#default_document (private)

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/file/info.rb', line 257

def default_document
  BSON::Document.new(
    :_id => BSON::ObjectId.new,
    :chunkSize => Chunk::DEFAULT_SIZE,
    # MongoDB stores times with millisecond precision
    :uploadDate => Time.now.utc.round(3),
    :contentType => DEFAULT_CONTENT_TYPE
  )
end

#filenameString

Get the filename from the file information.

Examples:

Get the filename.

file_info.filename

Returns:

  • (String)

    The filename.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/file/info.rb', line 115

def filename
  document[:filename]
end

#idBSON::ObjectId

Get the file id from the file information.

Examples:

Get the file id.

file_info.id

Returns:

  • (BSON::ObjectId)

    The file id.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/file/info.rb', line 127

def id
  document[:_id]
end

#inspectString

Get a readable inspection for the object.

Examples:

Inspect the file information.

file_info.inspect

Returns:

  • (String)

    The nice inspection.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/file/info.rb', line 158

def inspect
  "#<Mongo::Grid::File::Info:0x#{object_id} chunk_size=#{chunk_size} " +
    "filename=#{filename} content_type=#{content_type} id=#{id} md5=#{md5}>"
end

#lengthInteger Also known as: #size

Get the length of the document in bytes.

Examples:

Get the file length from the file information document.

file_info.length

Returns:

  • (Integer)

    The file length.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/file/info.rb', line 171

def length
  document[:length]
end

#md5String

Deprecated.

as of 2.6.0

Get the md5 hash.

Examples:

Get the md5 hash.

file_info.md5

Returns:

  • (String)

    The md5 hash as a string.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/file/info.rb', line 198

def md5
  document[:md5] || @client_md5
end

#metadataString

Get the additional metadata from the file information document.

Examples:

Get additional metadata.

file_info.

Returns:

  • (String)

    The additional metadata from file information document.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/file/info.rb', line 184

def 
  document[:]
end

#size

Alias for #length.

[ GitHub ]

  
# File 'lib/mongo/grid/file/info.rb', line 174

alias :size :length

#to_bson(buffer = BSON::ByteBuffer.new, validating_keys = nil) ⇒ String

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.

Convert the file information document to BSON for storage.

Examples:

Convert the file information document to BSON.

file_info.to_bson

Parameters:

  • buffer (BSON::ByteBuffer) (defaults to: BSON::ByteBuffer.new)

    The encoded BSON buffer to append to.

  • validating_keys (true, false) (defaults to: nil)

    Whether keys should be validated when serializing. This option is deprecated and will not be used. It will removed in version 3.0.

Returns:

  • (String)

    The raw BSON data.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/file/info.rb', line 236

def to_bson(buffer = BSON::ByteBuffer.new, validating_keys = nil)
  if @client_md5 && !document[:md5]
    document[:md5] = @client_md5.hexdigest
  end
  document.to_bson(buffer)
end

#update_md5(bytes) ⇒ Digest::MD5

Deprecated.

as of 2.6.0

Note:

This method is transitional and is provided for backwards compatibility.

Update the md5 hash if there is one.

It will be removed when md5 support is deprecated entirely.

Examples:

Update the md5 hash.

file_info.update_md5(bytes)

Parameters:

  • bytes (String)

    The bytes to use to update the digest.

Returns:

  • (Digest::MD5)

    The md5 hash object.

Since:

  • 2.6.0

[ GitHub ]

  
# File 'lib/mongo/grid/file/info.rb', line 217

def update_md5(bytes)
  md5.update(bytes) if md5
end

#upload_dateTime

Get the upload date.

Examples:

Get the upload date.

file_info.upload_date

Returns:

  • (Time)

    The upload date.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/file/info.rb', line 251

def upload_date
  document[:uploadDate]
end