Part-DB.Part-DB-server/src/Services/Misc/FAIconGenerator.php

95 lines
3.5 KiB
PHP
Raw Normal View History

<?php
2020-02-22 18:14:36 +01:00
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
2022-11-29 22:28:53 +01:00
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
2020-02-22 18:14:36 +01:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2020-01-05 15:46:58 +01:00
declare(strict_types=1);
2022-12-18 17:28:42 +01:00
namespace App\Services\Misc;
use App\Entity\Attachments\Attachment;
2020-01-05 22:49:00 +01:00
use function in_array;
2023-06-11 15:02:59 +02:00
/**
* @see \App\Tests\Services\Misc\FAIconGeneratorTest
*/
class FAIconGenerator
{
protected const EXT_MAPPING = [
2023-03-06 00:01:54 +01:00
'fa-file-pdf' => ['pdf', 'ps', 'eps'],
'fa-file-image' => Attachment::PICTURE_EXTS,
2023-03-06 00:01:54 +01:00
'fa-file-lines' => ['txt', 'md', 'log', 'rst', 'tex'],
'fa-file-csv' => ['csv', 'tsv'],
'fa-file-word' => ['doc', 'docx', 'odt', 'rtf'],
2023-03-06 00:12:46 +01:00
'fa-file-zipper' => ['zip', 'rar', 'bz2', 'tar', '7z', 'gz', 'tgz', 'xz', 'txz', 'tbz'],
2023-03-06 00:01:54 +01:00
'fa-file-audio' => ['mp3', 'wav', 'aac', 'm4a', 'wma', 'ogg', 'flac', 'alac'],
'fa-file-powerpoint' => ['ppt', 'pptx', 'odp', 'pps', 'key'],
2023-03-06 00:01:54 +01:00
'fa-file-excel' => ['xls', 'xlr', 'xlsx', 'ods', 'numbers'],
2023-03-06 00:12:46 +01:00
'fa-file-code' => ['php', 'xml', 'html', 'js', 'ts', 'htm', 'c', 'cpp', 'json', 'py', 'css', 'yml', 'yaml',
'sql', 'sh', 'bat', 'exe', 'dll', 'lib', 'so', 'a', 'o', 'h', 'hpp', 'java', 'class', 'jar', 'rb', 'rbw', 'rake', 'gem',],
'fa-file-video' => ['webm', 'avi', 'mp4', 'mkv', 'wmv'],
];
/**
* Gets the Font awesome icon class for a file with the specified extension.
2020-01-04 20:24:09 +01:00
* For example 'pdf' gives you 'fa-file-pdf'.
*
* @param string $extension The file extension (without dot). Must be ASCII chars only!
2020-01-04 20:24:09 +01:00
*
* @return string The fontawesome class with leading 'fa-'
*/
2020-01-04 20:24:09 +01:00
public function fileExtensionToFAType(string $extension): string
{
//Normalize file extension
$extension = strtolower($extension);
foreach (self::EXT_MAPPING as $fa => $exts) {
2020-01-05 22:49:00 +01:00
if (in_array($extension, $exts, true)) {
return $fa;
}
}
// When the extension is not found in the mapping array, we return the generic icon
return 'fa-file';
}
/**
* Returns HTML code to show the given fontawesome icon.
2020-01-04 20:24:09 +01:00
* E.g. <i class="fas fa-file-text"></i>.
*
* @param string $icon_class The icon which should be shown (e.g. fa-file-text)
2020-01-04 20:24:09 +01:00
* @param string $style The style of the icon 'fas'
2020-08-21 21:36:22 +02:00
* @param string $options any other css class attributes like size, etc
2020-01-04 20:24:09 +01:00
*
* @return string The final html
*/
2022-09-18 17:50:25 +02:00
public function generateIconHTML(string $icon_class, string $style = 'fa-solid', string $options = ''): string
{
//XSS protection
$icon_class = htmlspecialchars($icon_class);
$style = htmlspecialchars($style);
$options = htmlspecialchars($options);
return sprintf(
'<i class="%s %s %s"></i>',
$style,
$icon_class,
$options
);
}
2020-01-04 20:24:09 +01:00
}