| | <?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 |
| | + ); |
| | + } |
| | +} |
| | |