RSS Git Download  Clone
Raw Blame History
<?php

namespace GitList\Controller;

use Silex\Application;
use Silex\Api\ControllerProviderInterface;
use Symfony\Component\HttpFoundation\Response;

class BlobController implements ControllerProviderInterface
{
    public function connect(Application $app)
    {
        $route = $app['controllers_factory'];

        $route->get('{repo}/blob/{commitishPath}', function ($repo, $commitishPath) use ($app) {
            $repository = $app['git']->getRepositoryFromName($app['git.repos'], $repo);

            list($branch, $file) = $app['util.routing']
                ->parseCommitishPathParam($commitishPath, $repo);

            list($branch, $file) = $app['util.repository']->extractRef($repository, $branch, $file);

            $blob = $repository->getBlob("$branch:\"$file\"");
            $breadcrumbs = $app['util.view']->getBreadcrumbs($file);
            $fileType = $app['util.repository']->getFileType($file);

            $binary = $app['util.repository']->isBinary($file) && $fileType !== 'image';

            /*
            if ($fileType !== 'image' && $app['util.repository']->isBinary($file)) {
                return $app->redirect($app['url_generator']->generate('blob_raw', array(
                    'repo'   => $repo,
                    'commitishPath' => $commitishPath,
                )));
            }
            */

            if (!$binary) {
                $output = $blob->output();
            } else {
                $output = '';
            }

            $extension = '';
            $pathinfo = (pathinfo($file));
            if (isset($pathinfo['extension'])) {
                $extension = $pathinfo['extension'];
            }

            return $app['twig']->render('file.twig', array(
                'binary'        => $binary,
                'fileSize'       => strlen($output),
                'extension'      => $extension,
                'file'           => $file,
                'fileType'       => $fileType,
                'blob'           => $output,
                'repo'           => $repo,
                'breadcrumbs'    => $breadcrumbs,
                'branch'         => $branch,
                'branches'       => $repository->getBranches(),
                'browse_type'    => 'blob',
                'tags'           => $repository->getTags(),
                'enforceCodemirror' => isset($_GET['codemirror'])
            ));
        })->assert('repo', $app['util.routing']->getRepositoryRegex())
          ->assert('commitishPath', '.+')
          ->convert('commitishPath', 'escaper.argument:escape')
          ->bind('blob');

        $route->get('{repo}/raw/{commitishPath}', function ($repo, $commitishPath) use ($app) {
            $repository = $app['git']->getRepositoryFromName($app['git.repos'], $repo);

            list($branch, $file) = $app['util.routing']
                ->parseCommitishPathParam($commitishPath, $repo);

            list($branch, $file) = $app['util.repository']->extractRef($repository, $branch, $file);

            $blob = $repository->getBlob("$branch:\"$file\"")->output();

            $headers = array();
            if ($app['util.repository']->isBinary($file)) {
                $headers['Content-Disposition'] = 'attachment; filename="' .  $file . '"';
                $headers['Content-Type'] = 'application/octet-stream';
            } else {
                $headers['Content-Type'] = 'text/plain';
            }

            return new Response($blob, 200, $headers);
        })->assert('repo', $app['util.routing']->getRepositoryRegex())
          ->assert('commitishPath', $app['util.routing']->getCommitishPathRegex())
          ->convert('commitishPath', 'escaper.argument:escape')
          ->bind('blob_raw');

        return $route;
    }
}