diff --git a/config.ini-example b/config.ini-example index 058fba9..70815f7 100644 --- a/config.ini-example +++ b/config.ini-example @@ -11,4 +11,10 @@ debug = false ; If you need to specify custom filetypes for certain extensions, do this here [filetypes] ; extension = type -; dist = xml \ No newline at end of file +; dist = xml + +; If you need to set file types as binary or not, do this here +[binary_filetypes] +; extension = true +; svh = false +; map = true diff --git a/src/GitList/Controller/BlobController.php b/src/GitList/Controller/BlobController.php index 035a332..06c445b 100644 --- a/src/GitList/Controller/BlobController.php +++ b/src/GitList/Controller/BlobController.php @@ -19,6 +19,14 @@ class BlobController implements ControllerProviderInterface $breadcrumbs = $app['util.view']->getBreadcrumbs($file); $fileType = $app['util.repository']->getFileType($file); + if ($fileType !== 'image' && $app['util.repository']->isBinary($file)) { + return $app->redirect($app['url_generator']->generate('blob_raw', array( + 'repo' => $repo, + 'branch' => $branch, + 'file' => $file, + ))); + } + return $app['twig']->render('file.twig', array( 'file' => $file, 'fileType' => $fileType, @@ -38,7 +46,17 @@ class BlobController implements ControllerProviderInterface $repository = $app['git']->getRepository($app['git.repos'] . $repo); $blob = $repository->getBlob("$branch:\"$file\"")->output(); - return new Response($blob, 200, array('Content-Type' => 'text/plain')); + $headers = array(); + + if ($app['util.repository']->isBinary($file)) { + $headers['Content-Disposition'] = 'attachment; filename="' . $file . '"'; + $headers['Content-Transfer-Encoding'] = 'application/octet-stream'; + $headers['Content-Transfer-Encoding'] = 'binary'; + } else { + $headers['Content-Transfer-Encoding'] = 'text/plain'; + } + + return new Response($blob, 200, $headers); })->assert('file', '.+') ->assert('repo', '[\w-._]+') ->assert('branch', '[\w-._]+') @@ -46,4 +64,4 @@ class BlobController implements ControllerProviderInterface return $route; } -} \ No newline at end of file +} diff --git a/src/GitList/Util/Repository.php b/src/GitList/Util/Repository.php index fa7c871..2d928af 100644 --- a/src/GitList/Util/Repository.php +++ b/src/GitList/Util/Repository.php @@ -91,6 +91,13 @@ class Repository 'csproj' => 'xml', ); + protected static $binaryTypes = array( + 'exe', 'com', 'so', 'la', 'o', 'dll', 'pyc', + 'jpg', 'jpeg', 'bmp', 'gif', 'png', 'xmp', 'pcx', 'svgz', 'ttf', 'tiff', 'oet', + 'gz', 'tar', 'rar', 'zip', '7z', 'jar', 'class', + 'odt', 'ods', 'pdf', 'doc', 'docx', 'dot', 'xls', 'xlsx', + ); + public function __construct(Application $app) { $this->app = $app; @@ -126,6 +133,32 @@ class Repository return 'text'; } + /** + * Returns whether the file is binary. + * + * @param string $file + * + * @return boolean + */ + public function isBinary($file) + { + if (($pos = strrpos($file, '.')) !== false) { + $fileType = substr($file, $pos + 1); + } else { + return false; + } + + if (in_array($fileType, self::$binaryTypes)) { + return true; + } + + if (!empty($this->app['binary_filetypes']) && array_key_exists($fileType, $this->app['binary_filetypes'])) { + return $this->app['binary_filetypes'][$fileType]; + } + + return false; + } + public function getReadme($repo, $branch = 'master') { $repository = $this->app['git']->getRepository($this->app['git.repos'] . $repo);