2024-02-11 12:38:50 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace app\utils;
|
|
|
|
|
|
|
|
|
|
class FileTypeDetector
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Detects the file type based on the file extension and returns the corresponding Font Awesome icon class.
|
|
|
|
|
* Be careful to use this method, as it does not check the file content.
|
|
|
|
|
* And match is a new feature in PHP 8.0, so you need to upgrade your PHP version to 8.0 or higher to use it.
|
|
|
|
|
* 对着wikipedia的文件扩展名列表写的,可能有些不准确,反正是累死了...
|
|
|
|
|
*
|
|
|
|
|
* @param $filePath
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public static function detect($filePath): string
|
|
|
|
|
{
|
|
|
|
|
if (is_dir($filePath)) {
|
|
|
|
|
return 'fa-regular fa-folder';
|
|
|
|
|
} else {
|
|
|
|
|
$extension = pathinfo($filePath, PATHINFO_EXTENSION);
|
|
|
|
|
return match ($extension) {
|
|
|
|
|
'txt', 'md', 'xml', 'xps', 'markdown', 'rss', 'ini', 'json', 'yaml', 'log', 'text', 'asc' => 'fa-regular fa-file-lines',
|
|
|
|
|
'pdf' => 'fa-regular fa-file-pdf',
|
|
|
|
|
'doc', 'docx', 'docm', 'dot', 'dotx', 'odm', 'odt', 'ott', 'wps' => 'fa-regular fa-file-word',
|
|
|
|
|
'xls', 'xlsx', 'xlk', 'xlsb', 'xlsm', 'xlt', 'xltm', 'xlw' => 'fa-regular fa-file-excel',
|
2024-02-12 13:30:12 +08:00
|
|
|
|
'csv' => 'fa-solid fa-file-csv',
|
2024-02-11 12:38:50 +08:00
|
|
|
|
'ppt', 'pptx' => 'fa-regular fa-file-powerpoint',
|
|
|
|
|
'jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'psd', 'clip', 'raw', 'tiff', 'tif' => 'fa-regular fa-file-image',
|
|
|
|
|
'zip', 'rar', '7z', 'tar', 'tar.gz', 'xz', 'cab' => 'fa-regular fa-file-zipper',
|
2024-02-19 13:40:34 +08:00
|
|
|
|
'mp3', 'wav', 'flac', 'acc', 'ogg', 'wma', 'ac3', 'm4a' => 'fa-regular fa-file-audio',
|
2024-02-11 12:38:50 +08:00
|
|
|
|
'mp4', 'mkv', '3gp', 'avi', 'flv', 'm4v', 'mov', 'mpeg', 'mpg', 'mpe', 'rm', 'wmv', 'webm' => 'fa-regular fa-file-video',
|
2024-02-20 16:23:39 +08:00
|
|
|
|
'xhtml','js', 'asp', 'aspx', 'cgi', 'jsp', 'pl', 'phtml', 'php', 'html', 'htm', 'css', 'c', 'cpp', 'py', 'cs', 'ahk', 'as', 'au3', 'bat', 'fs', 'go', 'ipynb', 'kt', 'lua', 'o', 'ps1', 'ps1xml', 'psc1', 'psd1', 'psm1', 'pyc', 'pyo', 'r', 'rb', 'vbs', 'vb', 'cls', 'cc', 'cxx', 'cbp', 'csproj', 'h', 'hpp', 'hxx', 'vbg', 'vbp', 'vip', 'vbproj', 'vcproj', 'vdproj' => 'fa-regular fa-file-code',
|
2024-02-11 12:38:50 +08:00
|
|
|
|
'abb', 'apk' => 'fa-brands fa-android',
|
|
|
|
|
'exe', 'appx', 'esd', 'wim', 'msi', 'cpl', 'com', 'dll' => 'fa-brands fa-windows',
|
|
|
|
|
'ass', 'srt' => 'fa-regular fa-closed-captioning',
|
|
|
|
|
'deb' => 'fa-brands fa-debian',
|
|
|
|
|
'java', 'jar', 'class' => 'fa-brands fa-java',
|
2024-02-12 13:30:12 +08:00
|
|
|
|
'iso', 'img', 'mds', 'mdx', 'dmg', 'cdi', 'cue' => 'fa-solid fa-compact-disc',
|
2024-02-11 12:38:50 +08:00
|
|
|
|
'vmdk', 'vhdx', 'vhd', 'vfd', 'vud', 'vdi', 'hdd', 'cow', 'qcow', 'qcow2', 'qed' => 'fa-regular fa-hard-drive',
|
2024-02-12 13:30:12 +08:00
|
|
|
|
'db', 'frm', 'mdb', 'mdf', 'myd', 'myi', 'sqlite', 'sql' => 'fa-solid fa-database',
|
2024-02-11 12:38:50 +08:00
|
|
|
|
'epub', 'cpz' => 'fa-brands fa-leanpub',
|
|
|
|
|
'otf', 'tff', 'ttc', 'woff' => 'fa-regular fa-font',
|
2024-02-12 13:30:12 +08:00
|
|
|
|
'ico' => 'fa-solid fa-icons',
|
2024-02-11 12:38:50 +08:00
|
|
|
|
'lnk', 'url' => 'fa-regular fa-link',
|
|
|
|
|
'ipa' => 'fa-brands fa-apple',
|
|
|
|
|
'so' => 'fa-brands fa-linux',
|
2024-02-12 13:30:12 +08:00
|
|
|
|
'rdp' => 'fa-solid fa-laptop-file',
|
|
|
|
|
'omf', 'ssh', 'pub', 'ppk', 'cer', 'crt', 'der', 'p7b', 'p7c', 'p12', 'pfx', 'pem' => 'fa-solid fa-key',
|
2024-02-11 12:38:50 +08:00
|
|
|
|
'm3u', 'm3u8' => 'fa-regular fa-rectangle-list',
|
2024-02-12 13:30:12 +08:00
|
|
|
|
'cnf', 'conf', 'cfg' => 'fa-solid fa-gears',
|
2024-02-11 12:38:50 +08:00
|
|
|
|
default => 'fa-regular fa-file',
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|