/** * (non-PHPdoc) * @see ModelBehavior::beforeSave() */ public function beforeSave(Model $Model) { // проверяем необходимость сворачивания полей в gridfs foreach ($this->_settings['fields'] as $field) { $data = $Model->data[$Model->alias]; $paths = explode('.', $field); foreach ($paths as $path) { if (!empty($data[$path])) { $data = $data[$path]; } else { $data = null; } } $bytes = serialize($data); if (!empty($data) && mb_strlen($bytes, '8bit') > $this->_settings['size']) { // сохраняем содержимое поля в gridfs, а в значение поля записываем id в grid-е $value = $this->_Grid->storeBytes($bytes); if (count($paths) == 1) { $Model->data[$Model->alias][$paths[0]] = $value; } elseif (count($paths) == 2) { $Model->data[$Model->alias][$paths[0]][$paths[1]] = $value; } elseif (count($paths) == 3) { $Model->data[$Model->alias][$paths[0]][$paths[1]][$paths[2]] = $value; } elseif (count($paths) == 4) { $Model->data[$Model->alias][$paths[0]][$paths[1]][$paths[2]][$paths[3]] = $value; } } } return parent::beforeSave($Model); }
/** */ 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; }
/** * Write a string to a file * * @param string $path file path * @param string $content new file content * @return bool **/ protected function _filePutContents($path, $content) { $oldFile = $this->getFile($path); if (!$oldFile) { return false; } return $this->db->storeBytes($content, array("filename" => $oldFile->file['filename'], "metadata" => $oldFile->file['metadata'])); }
public function testDumpFileOverwritesAnExistingFile() { $filename = $this->workspace . DIRECTORY_SEPARATOR . 'foo.txt'; $this->gridfs->storeBytes('FOO BAR', array('filename' => $filename, 'type' => 'file')); $this->filesystem->dumpFile($filename, 'bar'); $this->assertFileExists($filename); $this->assertFileContent('bar', $filename); }
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); } }
/** * storeBytes. */ public function storeBytes($bytes, array $extra, array $options = array()) { $this->time->start(); $return = parent::storeBytes($bytes, $extra, $options); $time = $this->time->stop(); $this->log(array('type' => 'storeBytes', 'bytes_sha1' => sha1($bytes), 'extra' => $extra, 'options' => $options, 'time' => $time)); return $return; }
/** * @param \MongoGridFS $gridFs * @param \MongoGridFSFile $file */ function it_should_rename_file($gridFs, $file) { $file->getBytes()->willReturn('some content'); $file->getSize()->willReturn(12); $gridFs->findOne('otherFilename', array())->willReturn(null); $gridFs->findOne('filename', array())->shouldBeCalled()->willReturn($file); $gridFs->storeBytes('some content', array('date' => 1234, 'filename' => 'otherFilename'))->shouldBeCalled()->willReturn('someId'); $fileToDelete = new \stdClass(); $fileToDelete->file = array('_id' => 123); $gridFs->findOne('filename', array('_id'))->willReturn($fileToDelete); $gridFs->findOne(array('_id' => 'someId'))->willReturn($file); $gridFs->delete(123)->shouldBeCalled()->willReturn(true); $this->setMetadata('otherFilename', array('date' => 1234)); $this->rename('filename', 'otherFilename')->shouldReturn(true); }
/** * 存储二进制内容 * * @param bytes $bytes * @param string $filename * @param array $metadata */ public function storeBytesToGridFS($bytes, $filename = '', $metadata = array()) { if (!is_array($metadata)) { $metadata = array(); } if (!empty($filename)) { $metadata['filename'] = $filename; } $finfo = new \finfo(FILEINFO_MIME); $mime = $finfo->buffer($bytes); if ($mime !== false) { $metadata['mime'] = $mime; } $id = $this->_fs->storeBytes($bytes, $metadata); $gridfsFile = $this->_fs->get($id); return $gridfsFile->file; }