| | ]; |
| | |
| | + private const TEXT_FILENAMES = [ |
| | + 'Containerfile', 'Dockerfile', 'Makefile', 'Jenkinsfile' |
| | + ]; |
| | + |
| | + private const TEXT_EXTENSIONS = [ |
| | + 'php', 'phtml', 'php8', 'php7', 'c', 'h', 'cpp', 'hpp', 'cc', 'cxx', |
| | + 'cs', 'csx', 'java', 'kt', 'kts', 'scala', 'sc', 'groovy', 'gvy', 'js', |
| | + 'jsx', 'mjs', 'ts', 'tsx', 'dart', 'swift', 'go', 'rs', 'py', 'pyw', |
| | + 'rb', 'erb', 'pl', 'pm', 't', 'lua', 'sh', 'bash', 'zsh', 'ps1', |
| | + 'psm1', 'psd1', 'bat', 'cmd', 'md', 'markdown', 'rmd', 'r', 'xml', |
| | + 'svg', 'html', 'htm', 'css', 'json', 'lock', 'sql', 'yaml', 'yml', |
| | + 'gradle', 'tex', 'sty', 'cls', 'ltx', 'properties', 'prop', 'ini', |
| | + 'cfg', 'conf', 'toml', 'mk', 'mak', 'diff', 'patch' |
| | + ]; |
| | + |
| | private string $name; |
| | private string $sha; |
 |
| | string $contents = '' |
| | ) { |
| | - $this->name = $name; |
| | - $this->sha = $sha; |
| | - $this->mode = $mode; |
| | + $this->name = $name; |
| | + $this->sha = $sha; |
| | + $this->mode = $mode; |
| | $this->timestamp = $timestamp; |
| | - $this->size = $size; |
| | - $this->isDir = $mode === '40000' || $mode === '040000'; |
| | + $this->size = $size; |
| | + $this->isDir = $mode === '40000' || $mode === '040000'; |
| | |
| | $buffer = $this->isDir ? '' : $contents; |
| | |
| | $this->mediaType = $this->detectMediaType( $buffer ); |
| | - $this->category = $this->detectCategory( $name ); |
| | - $this->binary = $this->detectBinary(); |
| | + $this->category = $this->detectCategory( $name ); |
| | + $this->binary = $this->detectBinary(); |
| | } |
| | |
 |
| | : match( $this->category ) { |
| | 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', |
| | + 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 { |
| | if( $buffer === '' ) return 'application/x-empty'; |
| | |
| | - $finfo = new finfo( FILEINFO_MIME_TYPE ); |
| | + $finfo = new finfo( FILEINFO_MIME_TYPE ); |
| | $mediaType = $finfo->buffer( $buffer ); |
| | |
| | return $mediaType ?: 'application/octet-stream'; |
| | } |
| | |
| | private function detectCategory( string $filename = '' ): string { |
| | $parts = explode( '/', $this->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, |
| | + $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( $this->mediaType, 'compressed' ) => self::CAT_ARCHIVE, |
| | - default => self::CAT_BINARY, |
| | + $this->isTextFile( $filename ) => self::CAT_TEXT, |
| | + default => self::CAT_BINARY, |
| | }; |
| | } |
| | |
| | private function detectBinary(): bool { |
| | return $this->mediaType !== 'application/x-empty' |
| | - && !str_starts_with( $this->mediaType, 'text/' ); |
| | + && !str_starts_with( $this->mediaType, 'text/' ) |
| | + && !$this->isTextFile( $this->name ); |
| | } |
| | |
| | private function isArchiveFile( string $filename ): bool { |
| | return in_array( |
| | strtolower( pathinfo( $filename, PATHINFO_EXTENSION ) ), |
| | self::ARCHIVE_EXTENSIONS, |
| | true |
| | ); |
| | + } |
| | + |
| | + private function isTextFile( string $filename ): bool { |
| | + $basename = basename( $filename ); |
| | + $extension = strtolower( pathinfo( $filename, PATHINFO_EXTENSION ) ); |
| | + |
| | + return in_array( $basename, self::TEXT_FILENAMES, true ) |
| | + || in_array( $extension, self::TEXT_EXTENSIONS, true ); |
| | } |
| | } |