From 8c0e45164cf0ae4faa8fcb120fc6711e16787332 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Sat, 14 Jul 2012 12:47:23 +0200 Subject: [PATCH] Create zipball / tarball feature Archives are cached in cache/archives/repo/tree to never generate the same tree archive twice. --- controllers/archiveController.php | 32 +++++++++++++++++++++++++++++++ index.php | 3 +++ 2 files changed, 35 insertions(+) create mode 100644 controllers/archiveController.php diff --git a/controllers/archiveController.php b/controllers/archiveController.php new file mode 100644 index 0000000..f0e89f9 --- /dev/null +++ b/controllers/archiveController.php @@ -0,0 +1,32 @@ +get('{repo}/{format}ball/{branch}', function($repo, $format, $branch) use($app) { + $repository = $app['git']->getRepository($app['git.repos'] . $repo); + $tree = $repository->getBranchTree($branch); + + if (false === $tree) { + return $app->abort(404, 'Invalid commit or tree reference: '.$branch); + } + + $file = $app['cache.archives'].DIRECTORY_SEPARATOR.$repo.DIRECTORY_SEPARATOR.substr($tree, 0, 2).DIRECTORY_SEPARATOR.substr($tree, 2).'.'.$format; + + if (!file_exists($file)) { + $repository->createArchive($tree, $file, $format); + } + + return new StreamedResponse(function () use($file) { + readfile($file); + }, 200, array( + 'Content-type' => ('zip' === $format) ? 'application/zip' : 'application/x-tar', + 'Content-Description' => 'File Transfer', + 'Content-Disposition' => 'attachment; filename="'.$repo.'-'.substr($tree, 0, 6).'.'.$format.'"', + 'Content-Transfer-Encoding' => 'binary', + 'Content-Length' => filesize($file), + )); +})->assert('format', '(zip|tar)') + ->assert('repo', '[\w-._]+') + ->assert('branch', '[\w-._]+') + ->value('format', 'zip') + ->bind('archive'); diff --git a/index.php b/index.php index a6370a9..abf2694 100644 --- a/index.php +++ b/index.php @@ -22,6 +22,8 @@ $app['filetypes'] = $config['filetypes']; $app['hidden'] = isset($config['git']['hidden']) ? $config['git']['hidden'] : array(); $config['git']['repositories'] = rtrim($config['git']['repositories'], DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; +$app['cache.archives'] = __DIR__.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR.'archives'; + // Register Git and Twig service providersclass_path $app->register(new Silex\Provider\TwigServiceProvider(), array( 'twig.path' => __DIR__.'/views', @@ -42,6 +44,7 @@ $app['twig'] = $app->share($app->extend('twig', function($twig, $app) { })); // Load controllers +include 'controllers/archiveController.php'; include 'controllers/indexController.php'; include 'controllers/treeController.php'; include 'controllers/blobController.php';