Dave Jarvis' Repositories

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

Simplifies media type detection

AuthorDave Jarvis <email>
Date2026-02-14 15:07:44 GMT-0800
Commita76b3ada984ffd1852f54aaa831b3c565c69d50f
Parent52a262a
File.php
$buffer = $this->isDir ? '' : $contents;
- $this->mediaType = $this->detectMediaType( $buffer );
- $this->category = $this->detectCategory( $buffer, $name );
- $this->binary = $this->detectBinary( $buffer );
+ $this->mediaType = $this->detectMediaType($buffer);
+ $this->category = $this->detectCategory($name);
+ $this->binary = $this->detectBinary();
$this->icon = $this->resolveIcon();
}
}
- private function detectMediaType( string $buffer ): string {
- $finfo = new finfo( FILEINFO_MIME_TYPE );
- $mediaType = $finfo->buffer( $buffer );
+ 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 );
+ private function detectCategory(string $filename = ''): string {
+ $parts = explode('/', $this->mediaType);
- return match( true ) {
+ 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,
+ $this->isArchiveFile($filename) => self::CAT_ARCHIVE,
+ str_contains($this->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 detectBinary(): bool {
+ return !str_starts_with($this->mediaType, 'text/');
}
- private function isArchiveFile( string $filename ): bool {
+ private function isArchiveFile(string $filename): bool {
return in_array(
- strtolower( pathinfo( $filename, PATHINFO_EXTENSION ) ),
+ strtolower(pathinfo($filename, PATHINFO_EXTENSION)),
self::ARCHIVE_EXTENSIONS,
true
Delta15 lines added, 19 lines removed, 4-line decrease