Inheritance: extends MongoCollection
 /**
  * Delete chunks older than expiration time.
  *
  * @param \MongoGridFS $gridFs
  * @param int $expirationTime seconds
  *
  * @throws FileOpenException
  */
 public static function pruneChunks($gridFs, $expirationTime = 172800)
 {
     $result = $gridFs->remove(['flowUpdated' => ['$lt' => new \MongoDate(time() - $expirationTime)], 'flowStatus' => 'uploading']);
     if (!$result) {
         throw new FileOpenException("Could not remove chunks!");
     }
 }
 protected function tearDown()
 {
     if ($this->legacy) {
         $this->clean($this->workspace);
     } else {
         $this->gridfs->drop();
     }
 }
 /**
  * Разворачиваем данные назад из Grid-а в исходный массив
  * 
  * @param array $record данные содержащие то, что нужно развернуть
  * @return готовый массив с развёрнутыми данными
  */
 protected function _unserialize($record)
 {
     foreach ($this->_settings['fields'] as $field) {
         $paths = explode('.', $field);
         $data = $record;
         foreach ($paths as $path) {
             if (!empty($data[$path])) {
                 $data = $data[$path];
             } else {
                 $data = null;
             }
         }
         if (!empty($data) && $data instanceof MongoId) {
             // сохраняем содержимое поля в gridfs, а в значение поля записываем id в grid-е
             $value = @unserialize($this->_Grid->get($data)->getBytes());
             if (!empty($value)) {
                 if (count($paths) == 1) {
                     $record[$paths[0]] = $value;
                 } elseif (count($paths) == 2) {
                     $record[$paths[0]][$paths[1]] = $value;
                 } elseif (count($paths) == 3) {
                     $record[$paths[0]][$paths[1]][$paths[2]] = $value;
                 } elseif (count($paths) == 4) {
                     $record[$paths[0]][$paths[1]][$paths[2]][$paths[3]] = $value;
                 }
             }
         }
     }
     return $record;
 }
 /**
  * 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);
 }
Exemple #6
0
 /**
  */
 protected function _getFile($path, $name)
 {
     $query = array($this->_mdKey(self::FNAME) => $name, $this->_mdKey(self::PATH) => $this->_convertPath($path));
     try {
         return $this->_files->findOne($query);
     } catch (MongoException $e) {
         throw new Horde_Vfs_Exception($e);
     }
 }
 /**
  * validate.
  */
 public function validate($scanData = false)
 {
     $this->time->start();
     $return = parent::validate($scanData);
     $time = $this->time->stop();
     $this->log(array('type' => 'validate', 'scanData' => $scanData, 'time' => $time));
     return $return;
 }
Exemple #8
0
 /**
  * Execute the update query and persist its GridFSFile if necessary.
  *
  * @see Collection::doFindOne()
  * @param array $query
  * @param array $newObj
  * @param array $options
  * @return array|null
  */
 protected function doUpdate(array $query, array $newObj, array $options = [])
 {
     $file = isset($newObj['$set']['file']) ? $newObj['$set']['file'] : null;
     unset($newObj['$set']['file']);
     if ($file === null) {
         $file = isset($newObj['file']) ? $newObj['file'] : null;
         unset($newObj['file']);
     }
     /* Before we inspect $newObj, remove an empty $set operator we may have
      * left behind due to extracting the file field above.
      */
     if (empty($newObj['$set'])) {
         unset($newObj['$set']);
     }
     /* Determine if $newObj includes atomic modifiers, which will tell us if
      * we can get by with a storeFile() in some situations or if a two-step
      * storeFile() and update() is necessary.
      */
     $newObjHasModifiers = false;
     foreach (array_keys($newObj) as $key) {
         if ('$' === $key[0]) {
             $newObjHasModifiers = true;
         }
     }
     // Is there a file in need of persisting?
     if (isset($file) && $file->isDirty()) {
         /* It is impossible to overwrite a file's chunks in GridFS so we
          * must remove it and re-persist a new file with the same data.
          *
          * First, use findAndRemove() to remove the file metadata and chunks
          * prior to storing the file again below. Exclude metadata fields
          * from the result, since those will be reset later.
          */
         $document = $this->findAndRemove($query, ['fields' => ['filename' => 0, 'length' => 0, 'chunkSize' => 0, 'uploadDate' => 0, 'md5' => 0, 'file' => 0]]);
         /* If findAndRemove() returned nothing (no match or removal), create
          * a new document with the query's "_id" if available.
          */
         if (!isset($document)) {
             /* If $newObj had modifiers, we'll need to do an update later,
              * so default to an empty array for now. Otherwise, we can do
              * without that update and store $newObj now.
              */
             $document = $newObjHasModifiers ? [] : $newObj;
             /* If the document has no "_id" but there was one in the query
              * or $newObj, we can use that instead of having storeFile()
              * generate one.
              */
             if (!isset($document['_id']) && isset($query['_id'])) {
                 $document['_id'] = $query['_id'];
             }
             if (!isset($document['_id']) && isset($newObj['_id'])) {
                 $document['_id'] = $newObj['_id'];
             }
         }
         // Document will definitely have an "_id" after storing the file.
         $this->storeFile($file, $document);
         if (!$newObjHasModifiers) {
             /* TODO: MongoCollection::update() would return a boolean if
              * $newObj was not empty, or an array describing the update
              * operation. Improvise, since we only stored the file and that
              * returns the "_id" field.
              */
             return true;
         }
     }
     // Now send the original update bringing the file up to date
     $options = isset($options['safe']) ? $this->convertWriteConcern($options) : $options;
     return $this->mongoCollection->update($query, $newObj, $options);
 }
 /**
  * @param \MongoGridFS $gridFs
  */
 function it_should_calculate_checksum($gridFs)
 {
     $someFile = new \stdClass();
     $someFile->file = array('md5' => 'md5123');
     $gridFs->findOne('filename', array('md5'))->willReturn($someFile);
     $this->checksum('filename')->shouldReturn('md5123');
 }
Exemple #10
0
 /**
  * 删除陈旧的文件
  *
  * @param mixed $id
  *            \MongoID or String
  * @return bool true or false
  */
 public function removeFileFromGridFS($id)
 {
     if (!$id instanceof \MongoId) {
         $id = new \MongoId($id);
     }
     return $this->_fs->remove(array('_id' => $id));
 }