Class yii\mongodb\file\Download
Inheritance | yii\mongodb\file\Download » yii\base\BaseObject |
---|---|
Available since extension's version | 2.1 |
Source Code | https://github.com/yiisoft/yii2-mongodb/blob/master/src/file/Download.php |
Download represents the GridFS download operation.
A Download
object is usually created by calling yii\mongodb\file\Collection::get() or yii\mongodb\file\Collection::createDownload().
Usage example:
Yii::$app->mongodb->getFileCollection()->createDownload($document['_id'])->toFile('/path/to/file.dat');
You can use Download::substr()
to read a specific part of the file:
$filePart = Yii::$app->mongodb->getFileCollection()->createDownload($document['_id'])->substr(256, 1024);
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$bytes | string | File content. | yii\mongodb\file\Download |
$chunkCursor | \MongoDB\Driver\Cursor | Chuck list cursor. | yii\mongodb\file\Download |
$chunkIterator | Iterator | Chuck cursor iterator. | yii\mongodb\file\Download |
$collection | yii\mongodb\file\Collection | File collection to be used. | yii\mongodb\file\Download |
$document | array | Document to be downloaded. | yii\mongodb\file\Download |
$filename | string|null | File name. | yii\mongodb\file\Download |
$resource | resource | File stream resource. | yii\mongodb\file\Download |
$size | integer | File size. | yii\mongodb\file\Download |
Public Methods
Method | Description | Defined By |
---|---|---|
getBytes() | Alias of toString() method. | yii\mongodb\file\Download |
getChunkCursor() | Returns file chunks read cursor. | yii\mongodb\file\Download |
getChunkIterator() | Returns iterator for the file chunks cursor. | yii\mongodb\file\Download |
getDocument() | yii\mongodb\file\Download | |
getFilename() | Returns associated file's filename. | yii\mongodb\file\Download |
getResource() | Returns persistent stream resource, which can be used to read file. | yii\mongodb\file\Download |
getSize() | Returns the size of the associated file. | yii\mongodb\file\Download |
setDocument() | Sets data of the document to be downloaded. | yii\mongodb\file\Download |
substr() | Return part of a file. | yii\mongodb\file\Download |
toFile() | Saves download to the physical file. | yii\mongodb\file\Download |
toResource() | Returns an opened stream resource, which can be used to read file. | yii\mongodb\file\Download |
toStream() | Saves file into the given stream. | yii\mongodb\file\Download |
toString() | Returns a string of the bytes in the associated file. | yii\mongodb\file\Download |
write() | Alias of toFile() method. | yii\mongodb\file\Download |
Property Details
File collection to be used.
Document to be downloaded. Note that the type of this property differs in getter and setter. See getDocument() and setDocument() for details.
Method Details
Alias of toString() method.
public string getBytes ( ) | ||
return | string |
File content. |
---|
public function getBytes()
{
return $this->toString();
}
Returns file chunks read cursor.
public \MongoDB\Driver\Cursor getChunkCursor ( $refresh = false ) | ||
$refresh | boolean |
Whether to recreate cursor, if it is already exist. |
return | \MongoDB\Driver\Cursor |
Chuck list cursor. |
---|---|---|
throws | \yii\base\InvalidConfigException |
public function getChunkCursor($refresh = false)
{
if ($refresh || $this->_chunkCursor === null) {
$file = $this->getDocument();
$this->_chunkCursor = $this->collection->getChunkCollection()->find(
['files_id' => $file['_id']],
[],
['sort' => ['n' => 1]]
);
}
return $this->_chunkCursor;
}
Returns iterator for the file chunks cursor.
public Iterator getChunkIterator ( $refresh = false ) | ||
$refresh | boolean |
Whether to recreate iterator, if it is already exist. |
return | Iterator |
Chuck cursor iterator. |
---|
public function getChunkIterator($refresh = false)
{
if ($refresh || $this->_chunkIterator === null) {
$this->_chunkIterator = new \IteratorIterator($this->getChunkCursor($refresh));
$this->_chunkIterator->rewind();
}
return $this->_chunkIterator;
}
public array getDocument ( ) | ||
return | array |
Document to be downloaded. |
---|---|---|
throws | \yii\base\InvalidConfigException |
on invalid document configuration. |
public function getDocument()
{
if (!is_array($this->_document)) {
if (is_scalar($this->_document) || $this->_document instanceof ObjectID) {
$document = $this->collection->findOne(['_id' => $this->_document]);
if (empty($document)) {
throw new InvalidConfigException('Document id=' . $this->_document . ' does not exist at collection "' . $this->collection->getFullName() . '"');
}
$this->_document = $document;
} else {
$this->_document = (array)$this->_document;
}
}
return $this->_document;
}
Returns associated file's filename.
public string|null getFilename ( ) | ||
return | string|null |
File name. |
---|
public function getFilename()
{
$document = $this->getDocument();
return isset($document['filename']) ? $document['filename'] : null;
}
Returns persistent stream resource, which can be used to read file.
public resource getResource ( ) | ||
return | resource |
File stream resource. |
---|
public function getResource()
{
if ($this->_resource === null) {
$this->_resource = $this->toResource();
}
return $this->_resource;
}
Returns the size of the associated file.
public integer getSize ( ) | ||
return | integer |
File size. |
---|
public function getSize()
{
$document = $this->getDocument();
return isset($document['length']) ? $document['length'] : 0;
}
Sets data of the document to be downloaded.
Document can be specified by its ID, in this case its data will be fetched automatically via extra query.
public void setDocument ( $document ) | ||
$document | array|\MongoDB\BSON\ObjectID |
Document raw data or document ID. |
public function setDocument($document)
{
$this->_document = $document;
}
Return part of a file.
public string|false substr ( $start, $length ) | ||
$start | integer |
Reading start position. If non-negative, the returned string will start at the start'th position in file, counting from zero. If negative, the returned string will start at the start'th character from the end of file. |
$length | integer |
Number of bytes to read. If given and is positive, the string returned will contain at most length characters beginning from start (depending on the length of file). If given and is negative, then that many characters will be omitted from the end of file (after the start position has been calculated when a start is negative). |
return | string|false |
The extracted part of file or |
---|
public function substr($start, $length)
{
$document = $this->getDocument();
if ($start < 0) {
$start = max($document['length'] + $start, 0);
}
if ($start > $document['length']) {
return false;
}
if ($length < 0) {
$length = $document['length'] - $start + $length;
if ($length < 0) {
return false;
}
}
$chunkSize = $document['chunkSize'];
$startChunkNumber = floor($start / $chunkSize);
$chunkIterator = $this->getChunkIterator();
if (!$chunkIterator->valid()) {
// invalid iterator state - recreate iterator
// unable to use `rewind` due to error "Cursors cannot rewind after starting iteration"
$chunkIterator = $this->getChunkIterator(true);
}
if ($chunkIterator->key() > $startChunkNumber) {
// unable to go back by iterator
// unable to use `rewind` due to error "Cursors cannot rewind after starting iteration"
$chunkIterator = $this->getChunkIterator(true);
}
$result = '';
$chunkDataOffset = $start - $startChunkNumber * $chunkSize;
while ($chunkIterator->valid()) {
if ($chunkIterator->key() >= $startChunkNumber) {
$chunk = $chunkIterator->current();
$data = $chunk['data']->getData();
$readLength = min($chunkSize - $chunkDataOffset, $length);
$result .= StringHelper::byteSubstr($data, $chunkDataOffset, $readLength);
$length -= $readLength;
if ($length <= 0) {
break;
}
$chunkDataOffset = 0;
}
$chunkIterator->next();
}
return $result;
}
Saves download to the physical file.
public integer toFile ( $filename ) | ||
$filename | string |
Name of the physical file. |
return | integer |
Number of written bytes. |
---|
public function toFile($filename)
{
$filename = Yii::getAlias($filename);
FileHelper::createDirectory(dirname($filename));
return $this->toStream(fopen($filename, 'w+'));
}
Returns an opened stream resource, which can be used to read file.
Note: each invocation of this method will create new file resource.
public resource toResource ( ) | ||
return | resource |
Stream resource. |
---|
public function toResource()
{
$protocol = $this->collection->database->connection->registerFileStreamWrapper();
$context = stream_context_create([
$protocol => [
'download' => $this,
]
]);
$document = $this->getDocument();
$url = "{$protocol}://{$this->collection->database->name}.{$this->collection->prefix}?_id={$document['_id']}";
return fopen($url, 'r', false, $context);
}
Saves file into the given stream.
public integer toStream ( $stream ) | ||
$stream | resource |
Stream, which file should be saved to. |
return | integer |
Number of written bytes. |
---|
public function toStream($stream)
{
$bytesWritten = 0;
foreach ($this->getChunkCursor() as $chunk) {
$bytesWritten += fwrite($stream, $chunk['data']->getData());
}
return $bytesWritten;
}