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:
Klaus Silveira
2012-10-01 16:16:45 -07:00
3 changed files with 60 additions and 3 deletions

View File

@@ -11,4 +11,10 @@ debug = false
; If you need to specify custom filetypes for certain extensions, do this here ; If you need to specify custom filetypes for certain extensions, do this here
[filetypes] [filetypes]
; extension = type ; 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

View File

@@ -19,6 +19,14 @@ class BlobController implements ControllerProviderInterface
$breadcrumbs = $app['util.view']->getBreadcrumbs($file); $breadcrumbs = $app['util.view']->getBreadcrumbs($file);
$fileType = $app['util.repository']->getFileType($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( return $app['twig']->render('file.twig', array(
'file' => $file, 'file' => $file,
'fileType' => $fileType, 'fileType' => $fileType,
@@ -38,7 +46,17 @@ class BlobController implements ControllerProviderInterface
$repository = $app['git']->getRepository($app['git.repos'] . $repo); $repository = $app['git']->getRepository($app['git.repos'] . $repo);
$blob = $repository->getBlob("$branch:\"$file\"")->output(); $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('file', '.+')
->assert('repo', '[\w-._]+') ->assert('repo', '[\w-._]+')
->assert('branch', '[\w-._]+') ->assert('branch', '[\w-._]+')
@@ -46,4 +64,4 @@ class BlobController implements ControllerProviderInterface
return $route; return $route;
} }
} }

View File

@@ -91,6 +91,13 @@ class Repository
'csproj' => 'xml', '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) public function __construct(Application $app)
{ {
$this->app = $app; $this->app = $app;
@@ -126,6 +133,32 @@ class Repository
return 'text'; 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') public function getReadme($repo, $branch = 'master')
{ {
$repository = $this->app['git']->getRepository($this->app['git.repos'] . $repo); $repository = $this->app['git']->getRepository($this->app['git.repos'] . $repo);