Method: Mongo::Grid::FSBucket#open_download_stream_by_name

Defined in:
lib/mongo/grid/fs_bucket.rb

#open_download_stream_by_name(filename, opts = {}) {|The| ... } ⇒ Stream::Read

Opens a stream from which the application can read the contents of the stored file specified by filename and the revision in options.

Revision numbers are defined as follows: 0 = the original stored file 1 = the first revision 2 = the second revision etc…-2 = the second most recent revision -1 = the most recent revision

# @example Open a stream to download the original file.

fs.open_download_stream_by_name('some-file.txt', revision: 0)

Examples:

Open a stream to download the most recent revision.

fs.open_download_stream_by_name('some-file.txt')

Open a stream to download the second revision of the stored file.

fs.open_download_stream_by_name('some-file.txt', revision: 2)

Parameters:

  • filename (String)

    The file’s name.

  • opts (Hash) (defaults to: {})

    Options for the download.

Options Hash (opts):

  • :revision (Integer)

    The revision number of the file to download. Defaults to -1, the most recent version.

Yield Parameters:

  • The (Hash)

    read stream.

Returns:

Raises:

Since:

  • 2.1.0



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/mongo/grid/fs_bucket.rb', line 313

def open_download_stream_by_name(filename, opts = {}, &block)
  revision = opts.fetch(:revision, -1)
  if revision < 0
    skip = revision.abs - 1
    sort = { 'uploadDate' => Mongo::Index::DESCENDING }
  else
    skip = revision
    sort = { 'uploadDate' => Mongo::Index::ASCENDING }
  end
  file_info_doc = files_collection.find({ filename: filename} ,
                                     sort: sort,
                                     skip: skip,
                                     limit: -1).first
  unless file_info_doc
    raise Error::FileNotFound.new(filename, :filename) unless opts[:revision]
    raise Error::InvalidFileRevision.new(filename, opts[:revision])
  end
  open_download_stream(file_info_doc[:_id], file_info_doc: file_info_doc, &block)
end