/** * Return the file contents. * * @param string $fileName * @throws \Exception * @return array(string, string) [$contentType, $content] */ public function readFile($fileName) { $this->validateConnectionSettings(); /** @var LocalGitRepoConnectionSettings $connectionSettings */ $connectionSettings = $this->getConnectionSettings(); $content = $this->gitArchiveFile($connectionSettings->getHostName(), $connectionSettings->getGroupName(), $connectionSettings->getRepoName(), $fileName, $connectionSettings->getNamedReference(), $connectionSettings->getGitExecutable()); $contentType = FileUtils::getContentTypeFromFileName($fileName); return array($contentType, $content); }
/** * Return the file contents. * * @param string $fileName * @throws \Exception * @return array(string, string) [$contentType, $content] */ public function readFile($fileName) { $this->validateConnectionSettings(); /** @var LocalFileConnectionSettings $connectionSettings */ $connectionSettings = $this->getConnectionSettings(); $rootPath = $connectionSettings->getRootPath(); $normalizedPath = FileUtils::normalizePath($fileName, true); $localFileName = $rootPath . DIRECTORY_SEPARATOR . $normalizedPath; if (!file_exists($localFileName)) { throw new \Exception($this->getFileNotFoundMessage($fileName)); } $content = file_get_contents($fileName); $contentType = FileUtils::getContentTypeFromFileName($fileName); return array($contentType, $content); }
/** * Return the file contents. * * @param string $fileName * @throws \Exception * @return array(string, string) [$contentType, $content] */ public function readFile($fileName) { $this->validateConnectionSettings(); /** @var GitLabRepoConnectionSettings $connectionSettings */ $connectionSettings = $this->getConnectionSettings(); $remote = sprintf('git@%s:%s/%s.git', $connectionSettings->getHostName(), $connectionSettings->getGroupName(), $connectionSettings->getRepoName()); $url = sprintf("%s/projects/%s/repository/blobs/%s?private_token=%s&filepath=%s", $connectionSettings->getUrl(), str_replace('.', '%2E', urlencode($remote)), $connectionSettings->getNamedReference(), $connectionSettings->getApiToken(), $fileName); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, "application/json"); $content = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpCode != 200) { throw new FileClientConnectionException(sprintf('Unable to fetch file "%s" from %s (%s) via GitLab API %s. Got response code %s.', $fileName, $remote, $connectionSettings->getNamedReference(), $connectionSettings->getUrl(), $httpCode)); } $contentType = FileUtils::getContentTypeFromFileName($fileName); return array($contentType, $content); }
/** * Serve the specified file to standard output. * * @param string $file The file to be served. * @param boolean $output If true, error messages will be printed. * @throws \Exception * @return boolean */ public function serveFile($file, $output = true) { /** @var LocalFileConnectionSettings $connectionSettings */ $connectionSettings = $this->getConnectionSettings(); if (!$connectionSettings instanceof LocalFileConnectionSettings) { throw new \Exception('Invalid connection settings.'); } $connectionSettings->validate(); $normalizedFilePath = FileUtils::normalizePath($file, true); $filePath = $connectionSettings->getRootPath() . DIRECTORY_SEPARATOR . $normalizedFilePath; if (!file_exists($filePath)) { $this->setHttpResponseCode(404, $output); if ($output) { printf('<p>The file <strong>%s</strong> could not be found.</p>', $normalizedFilePath); } return false; } list($fileSize, $contentType) = $this->getFileInfo($filePath); $this->writeHeaders($contentType, $fileSize, $filePath); readfile($filePath); return true; }