Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/treetrek.git

Moves rendering out of file

AuthorDave Jarvis <email>
Date2026-02-14 18:20:19 GMT-0800
Commitd23f15be044fe1aa6b476166f4b47a1920ef6ba7
Parentb8a8ad8
File.php
private int $size;
private bool $isDir;
- private string $icon;
private string $mediaType;
$this->category = $this->detectCategory( $name );
$this->binary = $this->detectBinary();
- $this->icon = $this->resolveIcon();
}
public function compare( File $other ): int {
return $this->isDir !== $other->isDir
? ($this->isDir ? -1 : 1)
: strcasecmp( $this->name, $other->name );
}
- public function render( FileRenderer $renderer ): void {
- $renderer->renderFile(
+ public function renderListEntry( FileRenderer $renderer ): void {
+ $renderer->renderListEntry(
$this->name,
$this->sha,
$this->mode,
- $this->icon,
+ $this->resolveIcon(),
$this->timestamp,
$this->size
);
- }
-
- public function renderSize( FileRenderer $renderer ): void {
- $renderer->renderSize( $this->size );
}
-
- public function renderMedia( string $url ): bool {
- $rendered = false;
-
- if( $this->isImage() ) {
- echo '<div class="blob-content blob-content-image">' .
- '<img src="' . $url . '"></div>';
- $rendered = true;
- } elseif( $this->isVideo() ) {
- echo '<div class="blob-content blob-content-video">' .
- '<video controls><source src="' . $url . '" type="' .
- $this->mediaType . '"></video></div>';
- $rendered = true;
- } elseif( $this->isAudio() ) {
- echo '<div class="blob-content blob-content-audio">' .
- '<audio controls><source src="' . $url . '" type="' .
- $this->mediaType . '"></audio></div>';
- $rendered = true;
- }
- return $rendered;
+ public function renderMedia( FileRenderer $renderer, string $url ): bool {
+ return $renderer->renderMedia( $this, $url, $this->mediaType );
}
- public function emitRawHeaders(): void {
- header( "Content-Type: " . $this->mediaType );
- header( "Content-Length: " . $this->size );
- header( "Content-Disposition: attachment; filename=\"" .
- addslashes( basename( $this->name ) ) . "\"" );
+ public function renderSize( FileRenderer $renderer ): void {
+ $renderer->renderSize( $this->size );
}
public function isBinary(): bool {
return $this->binary;
+ }
+
+ public function emitRawHeaders(): void {
+ header( "Content-Type: " . $this->mediaType );
+ header( "Content-Length: " . $this->size );
+ header( "Content-Disposition: attachment; filename=\"" .
+ addslashes( basename( $this->name ) ) . "\"" );
}
pages/FilePage.php
foreach( $entries as $file ) {
- $file->render( $renderer );
+ $file->renderListEntry( $renderer );
}
'&name=' . urlencode( $filename );
- if( !$file->renderMedia( $rawUrl ) ) {
+ if( !$file->renderMedia( $renderer, $rawUrl ) ) {
if( $file->isText() ) {
if( $size > 524288 ) {
render/FileRenderer.php
<?php
+require_once __DIR__ . '/../File.php';
+
interface FileRenderer {
- public function renderFile(
+ public function renderListEntry(
string $name,
string $sha,
string $mode,
string $iconClass,
int $timestamp,
- int $size = 0
+ int $size
): void;
- public function renderTime( int $timestamp ): void;
+ public function renderMedia(
+ File $file,
+ string $url,
+ string $mediaType
+ ): bool;
public function renderSize( int $bytes ): void;
}
- public function renderFile(
+ public function renderListEntry(
string $name,
string $sha,
string $mode,
string $iconClass,
int $timestamp,
- int $size = 0
+ int $size
): void {
$fullPath = ($this->currentPath === '' ? '' : $this->currentPath . '/') .
$name;
- $url = '?repo=' . urlencode( $this->repoSafeName ) . '&hash=' . $sha .
- '&name=' . urlencode( $fullPath );
+ $url = '?repo=' . urlencode( $this->repoSafeName ) .
+ '&hash=' . $sha .
+ '&name=' . urlencode( $fullPath );
echo '<a href="' . $url . '" class="file-item">';
}
- public function renderTime( int $timestamp ): void {
+ public function renderMedia(
+ File $file,
+ string $url,
+ string $mediaType
+ ): bool {
+ $rendered = false;
+
+ if( $file->isImage() ) {
+ echo '<div class="blob-content blob-content-image">' .
+ '<img src="' . $url . '"></div>';
+ $rendered = true;
+ } elseif( $file->isVideo() ) {
+ echo '<div class="blob-content blob-content-video">' .
+ '<video controls><source src="' . $url . '" type="' .
+ $mediaType . '"></video></div>';
+ $rendered = true;
+ } elseif( $file->isAudio() ) {
+ echo '<div class="blob-content blob-content-audio">' .
+ '<audio controls><source src="' . $url . '" type="' .
+ $mediaType . '"></audio></div>';
+ $rendered = true;
+ }
+
+ return $rendered;
+ }
+
+ public function renderSize( int $bytes ): void {
+ echo $this->formatSize( $bytes );
+ }
+
+ private function renderTime( int $timestamp ): void {
$tokens = [
31536000 => 'year',
echo $result;
- }
-
- public function renderSize( int $bytes ): void {
- echo $this->formatSize( $bytes );
}
Delta61 lines added, 48 lines removed, 13-line increase