main • app/controllers/BrowseController.php
<?php
class BrowseController
{
private array $cfg;
public function __construct(array $cfg){ $this->cfg=$cfg; }
public function browse(int $repoId): void {
$db = db();
$uid = auth_user_id();
if (!repo_can_read($db, $repoId, $uid)) { http_response_code(403); exit("403"); }
$repo = repo_get($db, $repoId);
$branch = safe_branch($_GET['branch'] ?? $repo['default_branch']);
$dir = safe_path($_GET['dir'] ?? '');
$blobs = new BlobStore($this->cfg['storage_base']);
$engine = new RepoEngine($blobs);
$treeBuilder = new TreeBuilder();
$head = $engine->getBranchHead($db, $repoId, $branch);
$snapshot = $head ? $engine->getSnapshot($db, $head) : [];
$tree = $treeBuilder->build($snapshot);
$items = $treeBuilder->listDir($tree, $dir);
$st = $db->prepare("SELECT name FROM branches WHERE repo_id=? ORDER BY name ASC");
$st->execute([$repoId]);
$branches = $st->fetchAll();
$title = $repo['owner_username'] . "/" . $repo['name'] . " / Browse";
require __DIR__ . '/../views/repos/browse.php';
}
public function file(int $repoId): void {
$db = db();
$uid = auth_user_id();
if (!repo_can_read($db, $repoId, $uid)) { http_response_code(403); exit("403"); }
$repo = repo_get($db, $repoId);
$branch = safe_branch($_GET['branch'] ?? $repo['default_branch']);
$path = safe_path($_GET['path'] ?? '');
$blobs = new BlobStore($this->cfg['storage_base']);
$engine = new RepoEngine($blobs);
$head = $engine->getBranchHead($db, $repoId, $branch);
if (!$head) { flash_set('err','No commits yet.'); redirect("/r/$repoId"); }
$snap = $engine->getSnapshot($db, $head);
if (!isset($snap[$path])) { http_response_code(404); exit("File not found"); }
$content = $blobs->readBlobById($db, (int)$snap[$path]);
if (substr_count($content, "\0") > 0) $content = "[Binary file preview not supported]";
$title = $repo['owner_username'] . "/" . $repo['name'] . " / " . $path;
require __DIR__ . '/../views/repos/file.php';
}
}