1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Jitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) Jitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jitamin\Helper; |
13
|
|
|
|
14
|
|
|
use Jitamin\Foundation\Base; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* File helpers. |
18
|
|
|
*/ |
19
|
|
|
class FileHelper extends Base |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Get file icon. |
23
|
|
|
* |
24
|
|
|
* @param string $filename Filename |
25
|
|
|
* |
26
|
|
|
* @return string Font-Awesome-Icon-Name |
27
|
|
|
*/ |
28
|
|
|
public function icon($filename) |
29
|
|
|
{ |
30
|
|
|
switch ($this->getFileExtension($filename)) { |
31
|
|
|
case 'jpeg': |
32
|
|
|
case 'jpg': |
33
|
|
|
case 'png': |
34
|
|
|
case 'gif': |
35
|
|
|
return 'fa-file-image-o text-warning'; |
36
|
|
|
case 'xls': |
37
|
|
|
case 'xlsx': |
38
|
|
|
return 'fa-file-excel-o text-success'; |
39
|
|
|
case 'doc': |
40
|
|
|
case 'docx': |
41
|
|
|
return 'fa-file-word-o text-info'; |
42
|
|
|
case 'ppt': |
43
|
|
|
case 'pptx': |
44
|
|
|
return 'fa-file-powerpoint-o text-warning'; |
45
|
|
|
case 'zip': |
46
|
|
|
case 'rar': |
47
|
|
|
case 'tar': |
48
|
|
|
case 'bz2': |
49
|
|
|
case 'xz': |
50
|
|
|
case 'gz': |
51
|
|
|
return 'fa-file-archive-o text-success'; |
52
|
|
|
case 'mp3': |
53
|
|
|
return 'fa-file-audio-o text-primary'; |
54
|
|
|
case 'avi': |
55
|
|
|
case 'mov': |
56
|
|
|
return 'fa-file-video-o text-primary'; |
57
|
|
|
case 'php': |
58
|
|
|
case 'html': |
59
|
|
|
case 'css': |
60
|
|
|
return 'fa-file-code-o text-success'; |
61
|
|
|
case 'pdf': |
62
|
|
|
return 'fa-file-pdf-o text-danger'; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return 'fa-file-o'; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Return the image mimetype based on the file extension. |
70
|
|
|
* |
71
|
|
|
* @param $filename |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function getImageMimeType($filename) |
76
|
|
|
{ |
77
|
|
|
switch ($this->getFileExtension($filename)) { |
78
|
|
|
case 'jpeg': |
79
|
|
|
case 'jpg': |
80
|
|
|
return 'image/jpeg'; |
81
|
|
|
case 'png': |
82
|
|
|
return 'image/png'; |
83
|
|
|
case 'gif': |
84
|
|
|
return 'image/gif'; |
85
|
|
|
default: |
86
|
|
|
return 'image/jpeg'; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Return the browser view mimetype based on the file extension. |
92
|
|
|
* |
93
|
|
|
* @param $filename |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function getBrowserViewType($filename) |
98
|
|
|
{ |
99
|
|
|
switch ($this->getFileExtension($filename)) { |
100
|
|
|
case 'pdf': |
101
|
|
|
return 'application/pdf'; |
102
|
|
|
default: |
103
|
|
|
return; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get the preview type. |
109
|
|
|
* |
110
|
|
|
* @param string $filename |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function getPreviewType($filename) |
115
|
|
|
{ |
116
|
|
|
switch ($this->getFileExtension($filename)) { |
117
|
|
|
case 'md': |
118
|
|
|
case 'markdown': |
119
|
|
|
return 'markdown'; |
120
|
|
|
case 'txt': |
121
|
|
|
case 'log': |
122
|
|
|
return 'text'; |
123
|
|
|
default: |
124
|
|
|
return; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get file extension. |
130
|
|
|
* |
131
|
|
|
* @param $filename |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
protected function getFileExtension($filename) |
136
|
|
|
{ |
137
|
|
|
return strtolower(pathinfo($filename, PATHINFO_EXTENSION)); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|