Пример #1
0
 /**
  * just a test method to upload a file and execute methods from
  * DataModel to check the result
  * @return void
  */
 public function testDataModelFilesMethods()
 {
     $api = new Api\Files();
     $fn = tempnam(sys_get_temp_dir(), 'cb_test');
     file_put_contents($fn, 'testing');
     $data = array('pid' => 1, 'localFile' => $fn, 'oid' => 1);
     $rez = $api->upload($data);
     $this->assertTrue($rez['success'], 'Upload test file failed: ' . $fn);
     $fileData = $rez['data']['file'];
     $id = $fileData['id'];
     $rez = DM\Files::getContentIds($id);
     $this->assertTrue(!empty($rez[$id]), 'Cant get content id');
     $rez = DM\Files::getContentPaths($id);
     $this->assertTrue(!empty($rez[$id]), 'Cant get content path');
     //delete the file from system and check if same actions return empty results
     $f = Objects::getCachedObject($id);
     $f->delete(true);
     // permanently delete
     unset($f);
     $rez = DM\Files::getContentIds($id);
     $this->assertTrue(empty($rez[$id]), 'Obtaining content id for a permanently deleted file');
     $rez = DM\Files::getContentPaths($id);
     $this->assertTrue(empty($rez[$id]), 'Obtaining content path for a permanently deleted file');
 }
Пример #2
0
 /**
  * append file contents to content field for file records
  * @param  array &$records
  * @return void
  */
 protected function appendFileContents(&$records)
 {
     $fileRecords = array();
     foreach ($records as &$r) {
         if (!empty($r['template_type']) && $r['template_type'] == 'file') {
             $fileRecords[$r['id']] =& $r;
         }
     }
     if (!empty($fileRecords)) {
         $filesDir = Config::get('files_dir');
         $cpaths = DM\Files::getContentPaths(array_keys($fileRecords));
         foreach ($cpaths as $id => $cpath) {
             $r =& $fileRecords[$id];
             $filename = $filesDir . $cpath . '.gz';
             if (file_exists($filename)) {
                 $content = file_get_contents($filename);
                 $r['content'] .= "\n" . gzuncompress($content);
             }
             unset($content);
             unset($r);
         }
     }
 }