mirror of
https://github.com/klaussilveira/gitlist.git
synced 2025-11-17 19:20:56 +01:00
Merge pull request #159 from sstok/binary-files_issue_144
Properly detect if the file is binary, and download (if not an image). fixes #144
This commit is contained in:
@@ -11,4 +11,10 @@ debug = false
|
||||
; If you need to specify custom filetypes for certain extensions, do this here
|
||||
[filetypes]
|
||||
; extension = type
|
||||
; dist = xml
|
||||
; dist = xml
|
||||
|
||||
; If you need to set file types as binary or not, do this here
|
||||
[binary_filetypes]
|
||||
; extension = true
|
||||
; svh = false
|
||||
; map = true
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user