Forked from guedressel/font-awesome-mime-type-icons.php
Last active
July 23, 2024 10:06
-
-
Save colemanw/9c9a12aae16a4bfe2678de86b661d922 to your computer and use it in GitHub Desktop.
Font Awesome File Icons: Mapping MIME Types to correct icon classes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Get font awesome file icon class for specific MIME Type | |
* @see https://gist.github.com/guedressel/0daa170c0fde65ce5551 | |
* | |
*/ | |
function ($mime_type) { | |
// List of official MIME Types: http://www.iana.org/assignments/media-types/media-types.xhtml | |
$icon_classes = array( | |
// Media | |
'image' => 'fa-file-image-o', | |
'audio' => 'fa-file-audio-o', | |
'video' => 'fa-file-video-o', | |
// Documents | |
'application/pdf' => 'fa-file-pdf-o', | |
'application/msword' => 'fa-file-word-o', | |
'application/vnd.ms-word' => 'fa-file-word-o', | |
'application/vnd.oasis.opendocument.text' => 'fa-file-word-o', | |
'application/vnd.openxmlformats-officedocument.wordprocessingml' => 'fa-file-word-o', | |
'application/vnd.ms-excel' => 'fa-file-excel-o', | |
'application/vnd.openxmlformats-officedocument.spreadsheetml' => 'fa-file-excel-o', | |
'application/vnd.oasis.opendocument.spreadsheet' => 'fa-file-excel-o', | |
'application/vnd.ms-powerpoint' => 'fa-file-powerpoint-o', | |
'application/vnd.openxmlformats-officedocument.presentationml' => 'fa-file-powerpoint-o', | |
'application/vnd.oasis.opendocument.presentation' => 'fa-file-powerpoint-o', | |
'text/plain' => 'fa-file-text-o', | |
'text/html' => 'fa-file-code-o', | |
'application/json' => 'fa-file-code-o', | |
// Archives | |
'application/gzip' => 'fa-file-archive-o', | |
'application/zip' => 'fa-file-archive-o', | |
); | |
foreach ($icon_classes as $text => $icon) { | |
if (strpos($mime_type, $text) === 0) { | |
return $icon; | |
} | |
} | |
return 'fa-file-o'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I agree that the loop is not very efficient, @7sedam7's solution is incomplete, since the loops are designed to find a prefix of the mimetype. For example, the actual prefix for a DOCX document is
application/vnd.openxmlformats-officedocument.wordprocessingml.document
. Given icon class only contain the prefix. An optimal implementation will probably require a variant of tries. Note that the problem here is to find the longest prefix of a given string, which is not the standard use case for tries. This will be overkill for such a small data set.I propose the following TypeScript code to simplify the loop, however (using the Fontawesome React package) :