1<?php 2 3namespace GitList\Controller; 4 5use Silex\ControllerProviderInterface; 6use Silex\Application; 7use Symfony\Component\HttpFoundation\Response; 8 9class BlobController implements ControllerProviderInterface 10{ 11 public function connect(Application $app) 12 { 13 $route = $app['controllers_factory']; 14 15 $route->get('{repo}/blob/{commitishPath}', function ($repo, $commitishPath) use ($app) { 16 $repository = $app['git']->getRepositoryFromName($app['git.repos'], $repo); 17 18 list($branch, $file) = $app['util.routing'] 19 ->parseCommitishPathParam($commitishPath, $repo); 20 21 list($branch, $file) = $app['util.repository']->extractRef($repository, $branch, $file); 22 23 $blob = $repository->getBlob("$branch:\"$file\""); 24 $breadcrumbs = $app['util.view']->getBreadcrumbs($file); 25 $fileType = $app['util.repository']->getFileType($file); 26 27 if ($fileType !== 'image' && $app['util.repository']->isBinary($file)) { 28 return $app->redirect($app['url_generator']->generate('blob_raw', array( 29 'repo' => $repo, 30 'commitishPath' => $commitishPath, 31 ))); 32 } 33 34 return $app['twig']->render('file.twig', array( 35 'file' => $file, 36 'fileType' => $fileType, 37 'blob' => $blob->output(), 38 'repo' => $repo, 39 'branch' => $branch, 40 'breadcrumbs' => $breadcrumbs, 41 'branches' => $repository->getBranches(), 42 'tags' => $repository->getTags(), 43 )); 44 })->assert('repo', $app['util.routing']->getRepositoryRegex()) 45 ->assert('commitishPath', '.+') 46 ->convert('commitishPath', 'escaper.argument:escape') 47 ->bind('blob'); 48 49 $route->get('{repo}/raw/{commitishPath}', function ($repo, $commitishPath) use ($app) { 50 $repository = $app['git']->getRepositoryFromName($app['git.repos'], $repo); 51 52 list($branch, $file) = $app['util.routing'] 53 ->parseCommitishPathParam($commitishPath, $repo); 54 55 list($branch, $file) = $app['util.repository']->extractRef($repository, $branch, $file); 56 57 $blob = $repository->getBlob("$branch:\"$file\"")->output(); 58 59 $headers = array(); 60 if ($app['util.repository']->isBinary($file)) { 61 $headers['Content-Disposition'] = 'attachment; filename="' . $file . '"'; 62 $headers['Content-Type'] = 'application/octet-stream'; 63 } else { 64 $headers['Content-Type'] = 'text/plain'; 65 } 66 67 return new Response($blob, 200, $headers); 68 })->assert('repo', $app['util.routing']->getRepositoryRegex()) 69 ->assert('commitishPath', $app['util.routing']->getCommitishPathRegex()) 70 ->convert('commitishPath', 'escaper.argument:escape') 71 ->bind('blob_raw'); 72 73 $route->get('{repo}/logpatch/{commitishPath}', function ($repo, $commitishPath) use ($app) { 74 $repository = $app['git']->getRepositoryFromName($app['git.repos'], $repo); 75 76 list($branch, $file) = $app['util.routing'] 77 ->parseCommitishPathParam($commitishPath, $repo); 78 79 $filePatchesLog = $repository->getCommitsLogPatch($file); 80 $breadcrumbs = $app['util.view']->getBreadcrumbs($file); 81 82 return $app['twig']->render('logpatch.twig', array( 83 'branch' => $branch, 84 'repo' => $repo, 85 'breadcrumbs' => $breadcrumbs, 86 'commits' => $filePatchesLog, 87 )); 88 })->assert('repo', $app['util.routing']->getRepositoryRegex()) 89 ->assert('commitishPath', '.+') 90 ->convert('commitishPath', 'escaper.argument:escape') 91 ->bind('logpatch'); 92 93 return $route; 94 } 95} 96