You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
186 lines
6.5 KiB
186 lines
6.5 KiB
11 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
11 years ago
|
namespace yii\mongodb\file;
|
||
11 years ago
|
|
||
11 years ago
|
use yii\mongodb\Exception;
|
||
11 years ago
|
use Yii;
|
||
11 years ago
|
|
||
|
/**
|
||
|
* Collection represents the Mongo GridFS collection information.
|
||
|
*
|
||
11 years ago
|
* A file collection object is usually created by calling [[Database::getFileCollection()]] or [[Connection::getFileCollection()]].
|
||
|
*
|
||
|
* File collection inherits all interface from regular [[\yii\mongo\Collection]], adding methods to store files.
|
||
|
*
|
||
11 years ago
|
* @property \yii\mongo\Collection $chunkCollection file chunks Mongo collection. This property is read-only.
|
||
11 years ago
|
* @method \MongoGridFSCursor find() returns a cursor for the search results.
|
||
|
*
|
||
|
* @author Paul Klimov <klimov.paul@gmail.com>
|
||
|
* @since 2.0
|
||
|
*/
|
||
11 years ago
|
class Collection extends \yii\mongodb\Collection
|
||
11 years ago
|
{
|
||
|
/**
|
||
|
* @var \MongoGridFS Mongo GridFS collection instance.
|
||
|
*/
|
||
|
public $mongoCollection;
|
||
11 years ago
|
/**
|
||
11 years ago
|
* @var \yii\mongodb\Collection file chunks Mongo collection.
|
||
11 years ago
|
*/
|
||
|
private $_chunkCollection;
|
||
|
|
||
|
/**
|
||
|
* Returns the Mongo collection for the file chunks.
|
||
|
* @param boolean $refresh whether to reload the collection instance even if it is found in the cache.
|
||
11 years ago
|
* @return \yii\mongodb\Collection mongo collection instance.
|
||
11 years ago
|
*/
|
||
|
public function getChunkCollection($refresh = false)
|
||
|
{
|
||
|
if ($refresh || !is_object($this->_chunkCollection)) {
|
||
|
$this->_chunkCollection = Yii::createObject([
|
||
11 years ago
|
'class' => 'yii\mongodb\Collection',
|
||
11 years ago
|
'mongoCollection' => $this->mongoCollection->chunks
|
||
|
]);
|
||
|
}
|
||
|
return $this->_chunkCollection;
|
||
|
}
|
||
11 years ago
|
|
||
|
/**
|
||
|
* Removes data from the collection.
|
||
|
* @param array $condition description of records to remove.
|
||
|
* @param array $options list of options in format: optionName => optionValue.
|
||
|
* @return integer|boolean number of updated documents or whether operation was successful.
|
||
|
* @throws Exception on failure.
|
||
|
*/
|
||
|
public function remove($condition = [], $options = [])
|
||
|
{
|
||
|
$result = parent::remove($condition, $options);
|
||
|
$this->tryLastError(); // MongoGridFS::remove will return even if the remove failed
|
||
|
return $result;
|
||
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* Creates new file in GridFS collection from given local filesystem file.
|
||
|
* Additional attributes can be added file document using $metadata.
|
||
11 years ago
|
* @param string $filename name of the file to store.
|
||
|
* @param array $metadata other metadata fields to include in the file document.
|
||
|
* @param array $options list of options in format: optionName => optionValue
|
||
|
* @return mixed the "_id" of the saved file document. This will be a generated [[\MongoId]]
|
||
|
* unless an "_id" was explicitly specified in the metadata.
|
||
11 years ago
|
* @throws Exception on failure.
|
||
11 years ago
|
*/
|
||
11 years ago
|
public function insertFile($filename, $metadata = [], $options = [])
|
||
11 years ago
|
{
|
||
11 years ago
|
$token = 'Inserting file into ' . $this->getFullName();
|
||
|
Yii::info($token, __METHOD__);
|
||
|
try {
|
||
|
Yii::beginProfile($token, __METHOD__);
|
||
|
$options = array_merge(['w' => 1], $options);
|
||
|
$result = $this->mongoCollection->storeFile($filename, $metadata, $options);
|
||
|
Yii::endProfile($token, __METHOD__);
|
||
|
return $result;
|
||
|
} catch (\Exception $e) {
|
||
|
Yii::endProfile($token, __METHOD__);
|
||
|
throw new Exception($e->getMessage(), (int)$e->getCode(), $e);
|
||
|
}
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* Creates new file in GridFS collection with specified content.
|
||
|
* Additional attributes can be added file document using $metadata.
|
||
11 years ago
|
* @param string $bytes string of bytes to store.
|
||
11 years ago
|
* @param array $metadata other metadata fields to include in the file document.
|
||
|
* @param array $options list of options in format: optionName => optionValue
|
||
|
* @return mixed the "_id" of the saved file document. This will be a generated [[\MongoId]]
|
||
|
* unless an "_id" was explicitly specified in the metadata.
|
||
11 years ago
|
* @throws Exception on failure.
|
||
11 years ago
|
*/
|
||
11 years ago
|
public function insertFileContent($bytes, $metadata = [], $options = [])
|
||
11 years ago
|
{
|
||
11 years ago
|
$token = 'Inserting file content into ' . $this->getFullName();
|
||
|
Yii::info($token, __METHOD__);
|
||
|
try {
|
||
|
Yii::beginProfile($token, __METHOD__);
|
||
|
$options = array_merge(['w' => 1], $options);
|
||
|
$result = $this->mongoCollection->storeBytes($bytes, $metadata, $options);
|
||
|
Yii::endProfile($token, __METHOD__);
|
||
|
return $result;
|
||
|
} catch (\Exception $e) {
|
||
|
Yii::endProfile($token, __METHOD__);
|
||
|
throw new Exception($e->getMessage(), (int)$e->getCode(), $e);
|
||
|
}
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* Creates new file in GridFS collection from uploaded file.
|
||
|
* Additional attributes can be added file document using $metadata.
|
||
11 years ago
|
* @param string $name name of the uploaded file to store. This should correspond to
|
||
|
* the file field's name attribute in the HTML form.
|
||
|
* @param array $metadata other metadata fields to include in the file document.
|
||
|
* @return mixed the "_id" of the saved file document. This will be a generated [[\MongoId]]
|
||
|
* unless an "_id" was explicitly specified in the metadata.
|
||
11 years ago
|
* @throws Exception on failure.
|
||
11 years ago
|
*/
|
||
11 years ago
|
public function insertUploads($name, $metadata = [])
|
||
11 years ago
|
{
|
||
11 years ago
|
$token = 'Inserting file uploads into ' . $this->getFullName();
|
||
|
Yii::info($token, __METHOD__);
|
||
|
try {
|
||
|
Yii::beginProfile($token, __METHOD__);
|
||
|
$result = $this->mongoCollection->storeUpload($name, $metadata);
|
||
|
Yii::endProfile($token, __METHOD__);
|
||
|
return $result;
|
||
|
} catch (\Exception $e) {
|
||
|
Yii::endProfile($token, __METHOD__);
|
||
|
throw new Exception($e->getMessage(), (int)$e->getCode(), $e);
|
||
|
}
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* Retrieves the file with given _id.
|
||
11 years ago
|
* @param mixed $id _id of the file to find.
|
||
|
* @return \MongoGridFSFile|null found file, or null if file does not exist
|
||
11 years ago
|
* @throws Exception on failure.
|
||
11 years ago
|
*/
|
||
|
public function get($id)
|
||
|
{
|
||
11 years ago
|
$token = 'Inserting file uploads into ' . $this->getFullName();
|
||
|
Yii::info($token, __METHOD__);
|
||
|
try {
|
||
|
Yii::beginProfile($token, __METHOD__);
|
||
|
$result = $this->mongoCollection->get($id);
|
||
|
Yii::endProfile($token, __METHOD__);
|
||
|
return $result;
|
||
|
} catch (\Exception $e) {
|
||
|
Yii::endProfile($token, __METHOD__);
|
||
|
throw new Exception($e->getMessage(), (int)$e->getCode(), $e);
|
||
|
}
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* Deletes the file with given _id.
|
||
11 years ago
|
* @param mixed $id _id of the file to find.
|
||
|
* @return boolean whether the operation was successful.
|
||
11 years ago
|
* @throws Exception on failure.
|
||
11 years ago
|
*/
|
||
|
public function delete($id)
|
||
|
{
|
||
11 years ago
|
$token = 'Inserting file uploads into ' . $this->getFullName();
|
||
|
Yii::info($token, __METHOD__);
|
||
|
try {
|
||
|
Yii::beginProfile($token, __METHOD__);
|
||
|
$result = $this->mongoCollection->delete($id);
|
||
|
$this->tryResultError($result);
|
||
|
Yii::endProfile($token, __METHOD__);
|
||
|
return true;
|
||
|
} catch (\Exception $e) {
|
||
|
Yii::endProfile($token, __METHOD__);
|
||
|
throw new Exception($e->getMessage(), (int)$e->getCode(), $e);
|
||
|
}
|
||
11 years ago
|
}
|
||
|
}
|