/**
  * Gets the mime type for a blob
  *
  * @param GitPHP_Blob $blob blob
  * @return string mime type
  */
 public function GetMime($blob)
 {
     if (!$blob) {
         return false;
     }
     $file = $blob->GetName();
     if (empty($file)) {
         return '';
     }
     $dotpos = strrpos($file, '.');
     if ($dotpos !== FALSE) {
         $file = substr($file, $dotpos + 1);
     }
     switch ($file) {
         case 'jpg':
         case 'jpeg':
         case 'jpe':
             return 'image/jpeg';
             break;
         case 'gif':
             return 'image/gif';
             break;
         case 'png':
             return 'image/png';
             break;
     }
     return '';
 }
예제 #2
0
 /**
  * Gets the data for a blob
  *
  * @param GitPHP_Blob $blob blob
  * @return string blob data
  */
 public function Load($blob)
 {
     if (!$blob) {
         return;
     }
     $result = $this->exe->GetObjectData($blob->GetProject()->GetPath(), $blob->GetHash());
     return $result['contents'];
 }
예제 #3
0
 /**
  * Gets the size of a blob
  *
  * @param GitPHP_Blob $blob blob
  * @return int blob size
  */
 public function Size($blob)
 {
     if (!$blob) {
         return;
     }
     if ($blob->DataLoaded()) {
         return strlen($blob->GetData());
     }
     return $this->LoadSize($blob);
 }
예제 #4
0
 /**
  * Load blob size using git
  *
  * @param GitPHP_Blob $blob blob
  * @return int blob size
  */
 protected function LoadSize($blob)
 {
     if (!$blob) {
         return;
     }
     $args = array();
     $args[] = '-s';
     $args[] = $blob->GetHash();
     return $this->exe->Execute($blob->GetProject()->GetPath(), GIT_CAT_FILE, $args);
 }
예제 #5
0
 public function testObserver()
 {
     $projectmock = $this->getMockBuilder('GitPHP_Project')->disableOriginalConstructor()->getMock();
     $blob = new GitPHP_Blob($projectmock, '1234567890abcdef1234567890ABCDEF12345678', $this->getMock('GitPHP_BlobLoadStrategy_Interface'));
     $observermock = $this->getMock('GitPHP_Observer_Interface');
     $matcher = $this->once();
     $observermock->expects($matcher)->method('ObjectChanged')->with($this->isInstanceOf('GitPHP_Blob'), $this->equalTo(GitPHP_Observer_Interface::CacheableDataChange));
     $blob->AddObserver($observermock);
     $data = $blob->GetData();
     $data = $blob->GetData();
     //cached
     $this->assertEquals(1, $matcher->getInvocationCount());
 }
 /**
  * Gets the mime type for a blob
  *
  * @param GitPHP_Blob $blob blob
  * @return string mime type
  */
 public function GetMime($blob)
 {
     if (!$blob) {
         return false;
     }
     $data = $blob->GetData();
     if (empty($data)) {
         return false;
     }
     $mime = '';
     $finfo = @finfo_open(FILEINFO_MIME, $this->magicdb);
     if ($finfo) {
         $mime = finfo_buffer($finfo, $data, FILEINFO_MIME);
         if ($mime && strpos($mime, '/')) {
             if (strpos($mime, ';')) {
                 $mime = strtok($mime, ';');
             }
         }
         finfo_close($finfo);
     }
     return $mime;
 }
예제 #7
0
 /**
  * Sets the matching object
  *
  * @param GitPHP_Tree|GitPHP_Blob $object matching object
  */
 public function SetObject($object)
 {
     if ($object instanceof GitPHP_Tree) {
         $this->objectType = 'tree';
         $this->objectHash = $object->GetHash();
     } else {
         if ($object instanceof GitPHP_Blob) {
             $this->objectType = 'blob';
             $this->objectHash = $object->GetHash();
         } else {
             throw new Exception('Invalid file search result object');
         }
     }
 }
예제 #8
0
 /**
  * Gets a blob
  *
  * @param string $hash blob hash
  * @return GitPHP_Blob blob object
  */
 public function GetBlob($hash)
 {
     if (empty($hash)) {
         return null;
     }
     if (preg_match('/^[0-9A-Fa-f]{4,39}$/', $hash) && !$this->compat) {
         $fullHash = $this->project->ExpandHash($hash);
         if ($fullHash == $hash) {
             throw new GitPHP_InvalidHashException($hash);
         }
         $hash = $fullHash;
     }
     if (!preg_match('/^[0-9A-Fa-f]{40}$/', $hash)) {
         return null;
     }
     $key = GitPHP_Blob::CacheKey($this->project->GetProject(), $hash);
     $blob = null;
     if ($this->memoryCache) {
         $blob = $this->memoryCache->Get($key);
     }
     if (!$blob) {
         if ($this->cache) {
             $blob = $this->cache->Get($key);
         }
         $strategy = null;
         if ($this->compat) {
             $strategy = new GitPHP_BlobLoad_Git($this->exe);
         } else {
             $strategy = new GitPHP_BlobLoad_Raw($this->objectLoader, $this->exe);
         }
         if ($blob) {
             $blob->SetProject($this->project);
             $blob->SetStrategy($strategy);
         } else {
             $blob = new GitPHP_Blob($this->project, $hash, $strategy);
         }
         $blob->AddObserver($this);
         if ($this->memoryCache) {
             $this->memoryCache->Set($key, $blob);
         }
     }
     return $blob;
 }
예제 #9
0
 /**
  * Gets the cache key to use for this object
  *
  * @return string cache key
  */
 public function GetCacheKey()
 {
     return GitPHP_Blob::CacheKey($this->project->GetProject(), $this->hash);
 }
예제 #10
0
 /**
  * GetToFileType
  *
  * Gets the to file type
  *
  * @access public
  * @param boolean $local true if caller wants localized type
  * @return string to file type
  */
 public function GetToFileType($local = false)
 {
     if (!$this->diffInfoRead) {
         $this->ReadDiffInfo();
     }
     return GitPHP_Blob::FileType($this->toMode, $local);
 }
예제 #11
-1
 /**
  * Gets the mime type for a blob
  *
  * @param GitPHP_Blob $blob blob
  * @return string mime type
  */
 public function GetMime($blob)
 {
     if (!$blob) {
         return false;
     }
     $data = $blob->GetData();
     if (empty($data)) {
         return false;
     }
     $descspec = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'));
     $proc = proc_open('file -b --mime -', $descspec, $pipes);
     if (is_resource($proc)) {
         fwrite($pipes[0], $data);
         fclose($pipes[0]);
         $mime = stream_get_contents($pipes[1]);
         fclose($pipes[1]);
         proc_close($proc);
         if ($mime && strpos($mime, '/')) {
             if (strpos($mime, ';')) {
                 $mime = strtok($mime, ';');
             }
             return $mime;
         }
     }
     return false;
 }