/** * 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 ''; }
/** * 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']; }
/** * 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); }
/** * 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); }
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; }
/** * 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'); } } }
/** * 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; }
/** * 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); }
/** * 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); }
/** * 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; }