Stores a file in the database
public storeFile ( string $filename, array $extra = [], array $options = [] ) : mixed | ||
$filename | string | The name of the file |
$extra | array | Other metadata to add to the file saved |
$options | array | Options for the store. "safe": Check that this store succeeded |
return | mixed | Returns the _id of the saved object |
/** */ protected function _write($type, $path, $name, $data, $autocreate) { $this->_checkQuotaWrite($type, $data, $path, $name); if ($autocreate) { $this->autocreatePath($path); } elseif (!$this->_isFolder($path)) { throw new Horde_Vfs_Exception(sprintf('Folder "%s" does not exist', $path)); } $orig = $this->_getFile($path, $name); $mdata = array(self::MD => array(self::FNAME => $name, self::OWNER => $this->_params['user'], self::PATH => $this->_convertPath($path))); try { switch ($type) { case 'file': $this->_files->storeFile($data, $mdata); break; case 'string': $this->_files->storeBytes($data, $mdata); break; } } catch (MongoException $e) { throw new Horde_Vfs_Exception('Unable to write file data.'); } if ($orig) { $this->_files->delete($orig->file['_id']); } }
/** * Wrapper method for MongoGridFS::storeFile(). * * This method returns the GridFSFile object, unlike the base MongoGridFS * method, which returns the "_id" field of the saved document. The "_id" * will be set on the $document parameter, which is passed by reference. * * @see http://php.net/manual/en/mongogridfs.storefile.php * @param string|GridFSFile $file String filename or a GridFSFile object * @param array $document * @param array $options * @return GridFSFile */ public function storeFile($file, array &$document, array $options = []) { if (!$file instanceof GridFSFile) { $file = new GridFSFile($file); } $options = isset($options['safe']) ? $this->convertWriteConcern($options) : $options; if ($file->hasUnpersistedFile()) { $id = $this->mongoCollection->storeFile($file->getFilename(), $document, $options); } else { $id = $this->mongoCollection->storeBytes($file->getBytes(), $document, $options); } $document = array_merge(['_id' => $id], $document); $gridFsFile = $this->mongoCollection->get($id); // TODO: Consider throwing exception if file cannot be fetched $file->setMongoGridFSFile($this->mongoCollection->get($id)); return $file; }
protected function setUp() { $this->legacy = isset($_SERVER['LEGACY_TESTS']) ? (bool) $_SERVER['LEGACY_TESTS'] : false; $this->workspace = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid(); $this->time = time(); $old = umask(0); mkdir($this->workspace . '/bar', 0777, true); chmod($this->workspace . '/bar', 01777); touch($this->workspace . '/foo.txt', $this->time, $this->time); touch($this->workspace . '/bar/foo.txt'); file_put_contents($this->workspace . '/bar/dummy.txt', 'bar'); umask($old); if (!$this->legacy) { $this->gridfs = MongoGridTestHelper::getGridFS(); $this->gridfs->storeBytes('', array('uploadDate' => new \MongoDate($this->time), 'filename' => '/', 'type' => 'dir')); $this->gridfs->storeBytes('', array('uploadDate' => new \MongoDate($this->time), 'filename' => getcwd(), 'type' => 'dir')); $this->gridfs->storeBytes('', array('uploadDate' => new \MongoDate($this->time), 'filename' => $this->workspace, 'type' => 'dir')); $this->gridfs->storeBytes('', array('uploadDate' => new \MongoDate($this->time), 'filename' => $this->workspace . '/bar', 'type' => 'dir')); $this->gridfs->storeFile($this->workspace . '/foo.txt', array('uploadDate' => new \MongoDate($this->time), 'type' => 'file')); $this->gridfs->storeFile($this->workspace . '/bar/foo.txt', array('uploadDate' => new \MongoDate($this->time), 'type' => 'file')); $this->gridfs->storeFile($this->workspace . '/bar/dummy.txt', array('uploadDate' => new \MongoDate($this->time), 'type' => 'file')); $this->clean($this->workspace); } }
/** * storeFile. */ public function storeFile($filename, array $extra, array $options = array()) { $this->time->start(); $return = parent::storeFile($filename, $extra, $options); $time = $this->time->stop(); $this->log(array('type' => 'storeFile', 'filename' => $filename, 'extra' => $extra, 'options' => $options, 'time' => $time)); return $return; }