Dave Jarvis' Repositories

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

Merges File and Media Type Sniffer

AuthorDave Jarvis <email>
Date2026-02-14 15:04:00 GMT-0800
Commit52a262a4847de51d2e2a86988f3e09f85eb21b42
Parent7813282
Delta62 lines added, 66 lines removed, 4-line decrease
File.php
<?php
-require_once __DIR__ . '/MediaTypeSniffer.php';
require_once __DIR__ . '/render/FileRenderer.php';
class File {
+ private const CAT_IMAGE = 'image';
+ private const CAT_VIDEO = 'video';
+ private const CAT_AUDIO = 'audio';
+ private const CAT_TEXT = 'text';
+ private const CAT_ARCHIVE = 'archive';
+ private const CAT_BINARY = 'binary';
+
+ private const ARCHIVE_EXTENSIONS = [
+ 'zip', 'tar', 'gz', '7z', 'rar', 'jar', 'lha', 'bz', 'tgz', 'cab',
+ 'iso', 'dmg', 'xz', 'z', 'ar', 'war', 'ear', 'pak', 'hqx', 'arj',
+ 'zoo', 'rpm', 'deb', 'apk'
+ ];
+
private string $name;
private string $sha;
$buffer = $this->isDir ? '' : $contents;
- $this->mediaType = MediaTypeSniffer::isMediaType( $buffer );
- $this->category = MediaTypeSniffer::isCategory( $buffer, $name );
- $this->binary = MediaTypeSniffer::isBinary( $buffer );
+ $this->mediaType = $this->detectMediaType( $buffer );
+ $this->category = $this->detectCategory( $buffer, $name );
+ $this->binary = $this->detectBinary( $buffer );
$this->icon = $this->resolveIcon();
}
public function isImage(): bool {
- return $this->category === MediaTypeSniffer::CAT_IMAGE;
+ return $this->category === self::CAT_IMAGE;
}
public function isVideo(): bool {
- return $this->category === MediaTypeSniffer::CAT_VIDEO;
+ return $this->category === self::CAT_VIDEO;
}
public function isAudio(): bool {
- return $this->category === MediaTypeSniffer::CAT_AUDIO;
+ return $this->category === self::CAT_AUDIO;
}
public function isText(): bool {
- return $this->category === MediaTypeSniffer::CAT_TEXT;
+ return $this->category === self::CAT_TEXT;
}
? 'fa-file-pdf'
: match ($this->category) {
- MediaTypeSniffer::CAT_ARCHIVE => 'fa-file-archive',
- MediaTypeSniffer::CAT_IMAGE => 'fa-file-image',
- MediaTypeSniffer::CAT_AUDIO => 'fa-file-audio',
- MediaTypeSniffer::CAT_VIDEO => 'fa-file-video',
- MediaTypeSniffer::CAT_TEXT => 'fa-file-code',
- default => 'fa-file',
+ self::CAT_ARCHIVE => 'fa-file-archive',
+ self::CAT_IMAGE => 'fa-file-image',
+ self::CAT_AUDIO => 'fa-file-audio',
+ self::CAT_VIDEO => 'fa-file-video',
+ self::CAT_TEXT => 'fa-file-code',
+ default => 'fa-file',
});
}
-}
+
+ private function detectMediaType( string $buffer ): string {
+ $finfo = new finfo( FILEINFO_MIME_TYPE );
+ $mediaType = $finfo->buffer( $buffer );
+ return $mediaType ?: 'application/octet-stream';
+ }
+
+ private function detectCategory(
+ string $buffer,
+ string $filename = ''
+ ): string {
+ $mediaType = $this->detectMediaType( $buffer );
+ $parts = explode( '/', $mediaType );
+
+ return match( true ) {
+ $parts[0] === 'image' => self::CAT_IMAGE,
+ $parts[0] === 'video' => self::CAT_VIDEO,
+ $parts[0] === 'audio' => self::CAT_AUDIO,
+ $parts[0] === 'text' => self::CAT_TEXT,
+ $this->isArchiveFile( $filename ) => self::CAT_ARCHIVE,
+ str_contains( $mediaType, 'compressed' ) => self::CAT_ARCHIVE,
+ default => self::CAT_BINARY,
+ };
+ }
+
+ private function detectBinary( string $buffer ): bool {
+ return !str_starts_with( $this->detectMediaType( $buffer ), 'text/' );
+ }
+ private function isArchiveFile( string $filename ): bool {
+ return in_array(
+ strtolower( pathinfo( $filename, PATHINFO_EXTENSION ) ),
+ self::ARCHIVE_EXTENSIONS,
+ true
+ );
+ }
+}
MediaTypeSniffer.php
-<?php
-class MediaTypeSniffer {
- public const CAT_IMAGE = 'image';
- public const CAT_VIDEO = 'video';
- public const CAT_AUDIO = 'audio';
- public const CAT_TEXT = 'text';
- public const CAT_ARCHIVE = 'archive';
- public const CAT_BINARY = 'binary';
-
- private const ARCHIVE_EXTENSIONS = [
- 'zip', 'tar', 'gz', '7z', 'rar', 'jar', 'lha', 'bz', 'tgz', 'cab',
- 'iso', 'dmg', 'xz', 'z', 'ar', 'war', 'ear', 'pak', 'hqx', 'arj',
- 'zoo', 'rpm', 'deb', 'apk'
- ];
-
- public static function isMediaType( string $buffer ): string {
- $finfo = new finfo( FILEINFO_MIME_TYPE );
- $mediaType = $finfo->buffer( $buffer );
- return $mediaType ?: 'application/octet-stream';
- }
-
- public static function isCategory(
- string $buffer,
- string $filename = ''
- ): string {
- $mediaType = self::isMediaType( $buffer );
- $parts = explode( '/', $mediaType );
-
- return match( true ) {
- $parts[0] === 'image' => self::CAT_IMAGE,
- $parts[0] === 'video' => self::CAT_VIDEO,
- $parts[0] === 'audio' => self::CAT_AUDIO,
- $parts[0] === 'text' => self::CAT_TEXT,
- self::isArchive( $filename ) => self::CAT_ARCHIVE,
- str_contains( $mediaType, 'compressed' ) => self::CAT_ARCHIVE,
- default => self::CAT_BINARY,
- };
- }
-
- public static function isBinary( string $buffer ): bool {
- return !str_starts_with( self::isMediaType( $buffer ), 'text/' );
- }
-
- private static function isArchive( string $filename ): bool {
- return in_array(
- strtolower( pathinfo( $filename, PATHINFO_EXTENSION ) ),
- self::ARCHIVE_EXTENSIONS,
- true
- );
- }
-}