-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathFileTypeService.php
More file actions
168 lines (145 loc) · 5.77 KB
/
FileTypeService.php
File metadata and controls
168 lines (145 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
namespace Artgris\Bundle\FileManagerBundle\Service;
use Artgris\Bundle\FileManagerBundle\Helpers\FileManager;
use SplFileInfo;
use Symfony\Component\Routing\RouterInterface;
use Twig\Environment;
class FileTypeService {
const IMAGE_SIZE = [
FileManager::VIEW_LIST => '22',
FileManager::VIEW_THUMBNAIL => '100',
];
/**
* FileTypeService constructor.
*/
public function __construct(private RouterInterface $router, private Environment $twig) {
}
public function preview(FileManager $fileManager, SplFileInfo $file): array {
if ($fileManager->getImagePath()) {
$filePath = $fileManager->getImagePath().rawurlencode($file->getFilename());
} else {
$filePath = $this->router->generate(
'file_manager_file',
array_merge($fileManager->getQueryParameters(), ['fileName' => $file->getFilename()])
);
}
$extension = $file->getExtension();
$type = $file->getType();
if ('file' === $type) {
$size = $this::IMAGE_SIZE[$fileManager->getView()];
return $this->fileIcon($filePath, $extension, $size, true, $fileManager->getConfigurationParameter('twig_extension'), $fileManager->getConfigurationParameter('cachebreaker'));
}
if ('dir' === $type) {
$href = $this->router->generate(
'file_manager', array_merge(
$fileManager->getQueryParameters(),
['route' => $fileManager->getRoute().'/'.$file->getFilename()]
)
);
return [
'path' => $filePath,
'html' => "<i class='fas fa-folder-open' aria-hidden='true'></i>",
'folder' => '<a href="'.$href.'">'.$file->getFilename().'</a>',
];
}
if ('link' === $type) {
$href = $this->router->generate(
'file_manager', array_merge(
$fileManager->getQueryParameters(),
['route' => $fileManager->getRoute().'/'.$file->getFilename()],
),
);
return [
'path' => $filePath,
'html' => "<i class='fas fa-link' aria-hidden='true'></i>",
'folder' => '<a href="'.$href.'">'.$file->getFilename().' → '.$file->getLinkTarget().'</a>',
];
}
return [];
}
public function accept($type): bool|string {
switch ($type) {
case 'image':
$accept = 'image/*';
break;
case 'media':
$accept = 'video/*';
break;
default:
return false;
}
return $accept;
}
public function fileIcon(string $filePath,?string $extension = null, ?int $size = 75, ?bool $lazy = false, ?string $twigExtension = null, ?bool $cachebreaker = null): array {
$imageTemplate = null;
if (null === $extension) {
$filePathTmp = strtok($filePath, '?');
$extension = pathinfo($filePathTmp, PATHINFO_EXTENSION);
}
switch (true) {
case $this->isYoutubeVideo($filePath):
case preg_match('/(mp4|ogg|webm|avi|wmv|mov)$/i', $extension):
$fa = 'far fa-file-video';
break;
case preg_match('/(mp3|wav)$/i', $extension):
$fa = 'far fa-file-audio';
break;
case preg_match('/(gif|png|jpe?g|svg|webp)$/i', $extension):
$fileName = $filePath;
if ($cachebreaker) {
$query = parse_url($filePath, PHP_URL_QUERY);
$time = 'time='.time();
$fileName = $query ? $filePath.'&'.$time : $filePath.'?'.$time;
}
if ($twigExtension) {
$imageTemplate = str_replace('$IMAGE$', 'file_path', $twigExtension);
}
$html = $this->twig->render('@ArtgrisFileManager/views/preview.html.twig', [
'filename' => $fileName,
'size' => $size,
'lazy' => $lazy,
'twig_extension' => $twigExtension,
'image_template' => $imageTemplate,
'file_path' => $filePath,
]);
return [
'path' => $filePath,
'html' => $html,
'image' => true,
];
case preg_match('/(pdf)$/i', $extension):
$fa = 'far fa-file-pdf';
break;
case preg_match('/(docx?)$/i', $extension):
$fa = 'far fa-file-word';
break;
case preg_match('/(xlsx?|csv)$/i', $extension):
$fa = 'far fa-file-excel';
break;
case preg_match('/(pptx?)$/i', $extension):
$fa = 'far fa-file-powerpoint';
break;
case preg_match('/(zip|rar|gz)$/i', $extension):
$fa = 'far fa-file-archive';
break;
case filter_var($filePath, FILTER_VALIDATE_URL):
$fa = 'fab fa-internet-explorer';
break;
default:
$fa = 'far fa-file';
}
return [
'path' => $filePath,
'html' => "<i class='{$fa}' aria-hidden='true'></i>",
];
}
public function isYoutubeVideo($url): bool|int {
$rx = '~
^(?:https?://)?
(?:www[.])?
(?:youtube[.]com/watch[?]v=|youtu[.]be/)
([^&]{11})
~x';
return preg_match($rx, $url, $matches);
}
}