|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics) |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software; you can redistribute it and/or |
|
8
|
|
|
* modify it under the terms of the GNU General Public License |
|
9
|
|
|
* as published by the Free Software Foundation; either version 2 |
|
10
|
|
|
* of the License, or (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU General Public License |
|
18
|
|
|
* along with this program; if not, write to the Free Software |
|
19
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
namespace App\Services; |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
use App\Entity\Attachments\Attachment; |
|
26
|
|
|
|
|
27
|
|
|
class FAIconGenerator |
|
28
|
|
|
{ |
|
29
|
|
|
protected const EXT_MAPPING = [ |
|
30
|
|
|
'fa-file-pdf' => ['pdf'], |
|
31
|
|
|
'fa-file-image' => Attachment::PICTURE_EXTS, |
|
32
|
|
|
'fa-file-alt' => ['txt', 'md', 'rtf', 'log', 'rst', 'tex'], |
|
33
|
|
|
'fa-file-csv' => ['csv'], |
|
34
|
|
|
'fa-file-word' => ['doc', 'docx', 'odt'], |
|
35
|
|
|
'fa-file-archive' => ['zip', 'rar', 'bz2', 'tar', '7z', 'gz'], |
|
36
|
|
|
'fa-file-audio' => ['mp3', 'wav', 'aac', 'm4a', 'wma'], |
|
37
|
|
|
'fa-file-powerpoint' => ['ppt', 'pptx', 'odp', 'pps', 'key'], |
|
38
|
|
|
'fa-file-excel' => ['xls', 'xlr', 'xlsx', 'ods'], |
|
39
|
|
|
'fa-file-code' => ['php', 'xml', 'html', 'js', 'ts', 'htm', 'c', 'cpp'], |
|
40
|
|
|
'fa-file-video' => ['webm', 'avi', 'mp4', 'mkv', 'wmv'], |
|
41
|
|
|
]; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Gets the Font awesome icon class for a file with the specified extension. |
|
45
|
|
|
* For example 'pdf' gives you 'fa-file-pdf' |
|
46
|
|
|
* @param string $extension The file extension (without dot). Must be ASCII chars only! |
|
47
|
|
|
* @return string The fontawesome class with leading 'fa-' |
|
48
|
|
|
*/ |
|
49
|
|
|
public function fileExtensionToFAType(string $extension) : string |
|
50
|
|
|
{ |
|
51
|
|
|
if ($extension === '') { |
|
52
|
|
|
throw new \InvalidArgumentException('You must specify an extension!'); |
|
53
|
|
|
} |
|
54
|
|
|
//Normalize file extension |
|
55
|
|
|
$extension = strtolower($extension); |
|
56
|
|
|
foreach (self::EXT_MAPPING as $fa => $exts) { |
|
57
|
|
|
if (in_array($extension, $exts, true)) { |
|
58
|
|
|
return $fa; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// When the extension is not found in the mapping array, we return the generic icon |
|
63
|
|
|
return 'fa-file'; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Returns HTML code to show the given fontawesome icon. |
|
68
|
|
|
* E.g. <i class="fas fa-file-text"></i> |
|
69
|
|
|
* @param string $icon_class The icon which should be shown (e.g. fa-file-text) |
|
70
|
|
|
* @param string $style The style of the icon 'fas' |
|
71
|
|
|
* @param string $options Any other css class attributes like size, etc. |
|
72
|
|
|
* @return string The final html |
|
73
|
|
|
*/ |
|
74
|
|
|
public function generateIconHTML(string $icon_class, string $style = 'fas', string $options = '') : string |
|
75
|
|
|
{ |
|
76
|
|
|
//XSS protection |
|
77
|
|
|
$icon_class = htmlspecialchars($icon_class); |
|
78
|
|
|
$style = htmlspecialchars($style); |
|
79
|
|
|
$options = htmlspecialchars($options); |
|
80
|
|
|
|
|
81
|
|
|
return sprintf( |
|
82
|
|
|
'<i class="%s %s %s"></i>', |
|
83
|
|
|
$style, |
|
84
|
|
|
$icon_class, |
|
85
|
|
|
$options |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
} |