| | private function renderBlob($targetHash) { |
| | $repoParam = '&repo=' . urlencode($this->currentRepo['safe_name']); |
| | - $size = $this->git->getObjectSize($targetHash); |
| | |
| | - // 1. Check for size limit (512KB = 524288 bytes) |
| | - if ($size > 524288) { |
| | - $this->renderDownloadState($targetHash, "File is too large to display (" . $this->formatSize($size) . ")."); |
| | - return; |
| | - } |
| | + // 1. Get size first (cheap operation) |
| | + $size = $this->git->getObjectSize($targetHash); |
| | |
| | - // 2. Sniff content type (read first 12 bytes) |
| | + // 2. Sniff content type (requires reading data) |
| | + // Note: This relies on the Git class reading the object. |
| | $buffer = ''; |
| | $this->git->stream($targetHash, function($d) use (&$buffer) { |
| | if (strlen($buffer) < 12) $buffer .= $d; |
| | }); |
| | |
| | - // UPDATED: Retrieve the filename from the URL to assist detection |
| | $filename = $_GET['name'] ?? ''; |
| | $category = MediaTypeSniffer::isCategory($buffer, $filename); |
| | $mimeType = MediaTypeSniffer::isMediaType($buffer, $filename); |
| | |
| | $this->renderBreadcrumbs($targetHash, 'File'); |
| | - echo '<h2>' . substr($targetHash, 0, 7) . '</h2>'; |
| | |
| | - $rawUrl = '?action=raw&hash=' . $targetHash . $repoParam; |
| | + // Calculate Raw URL for media sources |
| | + $rawUrl = '?action=raw&hash=' . $targetHash . $repoParam . '&name=' . urlencode($filename); |
| | |
| | - // 3. Render View based on Category |
| | - if ($category === MediaTypeSniffer::CAT_IMAGE) { |
| | + // 3. Render logic based on Category -> Size |
| | + if ($category === MediaTypeSniffer::CAT_VIDEO) { |
| | + // Allow Video Playback (Browser handles streaming via raw URL) |
| | + echo '<div class="blob-content" style="text-align:center; padding: 20px; background: #000;">'; |
| | + echo '<video controls style="max-width: 100%; max-height: 80vh;">'; |
| | + echo '<source src="' . $rawUrl . '" type="' . $mimeType . '">'; |
| | + echo 'Your browser does not support the video element.'; |
| | + echo '</video>'; |
| | + echo '</div>'; |
| | + |
| | + } elseif ($category === MediaTypeSniffer::CAT_AUDIO) { |
| | + // Allow Audio Playback |
| | + echo '<div class="blob-content" style="text-align:center; padding: 40px; background: #f6f8fa;">'; |
| | + echo '<audio controls style="width: 100%; max-width: 600px;">'; |
| | + echo '<source src="' . $rawUrl . '" type="' . $mimeType . '">'; |
| | + echo 'Your browser does not support the audio element.'; |
| | + echo '</audio>'; |
| | + echo '</div>'; |
| | + |
| | + } elseif ($category === MediaTypeSniffer::CAT_IMAGE) { |
| | + // Allow Image Viewing |
| | echo '<div class="blob-content" style="text-align:center; padding: 20px; background: #f6f8fa;">'; |
| | echo '<img src="' . $rawUrl . '" style="max-width: 100%; border: 1px solid #dfe2e5;">'; |
| | - echo '</div>'; |
| | - } elseif ($category === MediaTypeSniffer::CAT_VIDEO) { |
| | - echo '<div class="blob-content" style="text-align:center; padding: 20px;">'; |
| | - echo '<video controls style="max-width: 100%;"><source src="' . $rawUrl . '" type="' . $mimeType . '"></video>'; |
| | echo '</div>'; |
| | + |
| | } elseif ($category === MediaTypeSniffer::CAT_TEXT) { |
| | - $content = ''; |
| | - $this->git->stream($targetHash, function($d) use (&$content) { $content .= $d; }); |
| | - // UPDATED: Changed from div to pre to preserve whitespace |
| | - echo '<div class="blob-content"><pre class="blob-code">' . htmlspecialchars($content) . '</pre></div>'; |
| | + // Text: Enforce 512KB Limit |
| | + if ($size > 524288) { |
| | + $this->renderDownloadState($targetHash, "File is too large to display (" . $this->formatSize($size) . ")."); |
| | + } else { |
| | + $content = ''; |
| | + $this->git->stream($targetHash, function($d) use (&$content) { $content .= $d; }); |
| | + echo '<div class="blob-content"><pre class="blob-code">' . htmlspecialchars($content) . '</pre></div>'; |
| | + } |
| | + |
| | } else { |
| | + // Binary/Other: Download |
| | $this->renderDownloadState($targetHash, "This is a binary file."); |
| | } |