Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BlobController.php 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace GitList\Controller;
  3. use Silex\Application;
  4. use Silex\ControllerProviderInterface;
  5. use Symfony\Component\HttpFoundation\Response;
  6. class BlobController implements ControllerProviderInterface
  7. {
  8. public function connect(Application $app)
  9. {
  10. $route = $app['controllers_factory'];
  11. $route->get('{repo}/blob/{commitishPath}', function ($repo, $commitishPath) use ($app) {
  12. $repository = $app['git']->getRepositoryFromName($app['git.repos'], $repo);
  13. list($branch, $file) = $app['util.routing']
  14. ->parseCommitishPathParam($commitishPath, $repo);
  15. list($branch, $file) = $app['util.repository']->extractRef($repository, $branch, $file);
  16. $blob = $repository->getBlob("$branch:\"$file\"");
  17. $breadcrumbs = $app['util.view']->getBreadcrumbs($file);
  18. $fileType = $app['util.repository']->getFileType($file);
  19. if ($fileType !== 'image' && $app['util.repository']->isBinary($file)) {
  20. return $app->redirect($app['url_generator']->generate('blob_raw', array(
  21. 'repo' => $repo,
  22. 'commitishPath' => $commitishPath,
  23. )));
  24. }
  25. return $app['twig']->render('file.twig', array(
  26. 'file' => $file,
  27. 'fileType' => $fileType,
  28. 'blob' => $blob->output(),
  29. 'repo' => $repo,
  30. 'branch' => $branch,
  31. 'breadcrumbs' => $breadcrumbs,
  32. 'branches' => $repository->getBranches(),
  33. 'tags' => $repository->getTags(),
  34. ));
  35. })->assert('repo', $app['util.routing']->getRepositoryRegex())
  36. ->assert('commitishPath', '.+')
  37. ->convert('commitishPath', 'escaper.argument:escape')
  38. ->bind('blob');
  39. $route->get('{repo}/raw/{commitishPath}', function ($repo, $commitishPath) use ($app) {
  40. $repository = $app['git']->getRepositoryFromName($app['git.repos'], $repo);
  41. list($branch, $file) = $app['util.routing']
  42. ->parseCommitishPathParam($commitishPath, $repo);
  43. list($branch, $file) = $app['util.repository']->extractRef($repository, $branch, $file);
  44. $blob = $repository->getBlob("$branch:\"$file\"")->output();
  45. $headers = array();
  46. if ($app['util.repository']->isBinary($file)) {
  47. $headers['Content-Disposition'] = 'attachment; filename="' . $file . '"';
  48. $headers['Content-Type'] = 'application/octet-stream';
  49. } else {
  50. $headers['Content-Type'] = 'text/plain';
  51. }
  52. return new Response($blob, 200, $headers);
  53. })->assert('repo', $app['util.routing']->getRepositoryRegex())
  54. ->assert('commitishPath', $app['util.routing']->getCommitishPathRegex())
  55. ->convert('commitishPath', 'escaper.argument:escape')
  56. ->bind('blob_raw');
  57. return $route;
  58. }
  59. }