1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace UniSharp\LaravelFilemanager; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
7
|
|
|
|
8
|
|
|
class LfmItem |
9
|
|
|
{ |
10
|
|
|
private $lfm; |
11
|
|
|
private $helper; |
12
|
|
|
|
13
|
|
|
private $columns = ['name', 'url', 'time', 'icon', 'is_file', 'is_image', 'thumb_url']; |
14
|
|
|
public $attributes = []; |
15
|
|
|
|
16
|
|
|
public function __construct(LfmPath $lfm, Lfm $helper) |
17
|
|
|
{ |
18
|
|
|
$this->lfm = $lfm->thumb(false); |
19
|
|
|
$this->helper = $helper; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function __get($var_name) |
23
|
|
|
{ |
24
|
|
|
if (!array_key_exists($var_name, $this->attributes)) { |
25
|
|
|
$function_name = Str::camel($var_name); |
26
|
|
|
$this->attributes[$var_name] = $this->$function_name(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
return $this->attributes[$var_name]; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function fill() |
33
|
|
|
{ |
34
|
|
|
foreach ($this->columns as $column) { |
35
|
|
|
$this->__get($column); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function name() |
42
|
|
|
{ |
43
|
|
|
return $this->lfm->getName(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function path($type = 'absolute') |
47
|
|
|
{ |
48
|
|
|
return $this->lfm->path($type); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function isDirectory() |
52
|
|
|
{ |
53
|
|
|
return $this->lfm->isDirectory(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function isFile() |
57
|
|
|
{ |
58
|
|
|
return ! $this->isDirectory(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Check a file is image or not. |
63
|
|
|
* |
64
|
|
|
* @param mixed $file Real path of a file or instance of UploadedFile. |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
public function isImage() |
68
|
|
|
{ |
69
|
|
|
if (!$this->isDirectory()) { |
70
|
|
|
return Str::startsWith($this->mimeType(), 'image'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get mime type of a file. |
78
|
|
|
* |
79
|
|
|
* @param mixed $file Real path of a file or instance of UploadedFile. |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
// TODO: uploaded file |
83
|
|
|
public function mimeType() |
84
|
|
|
{ |
85
|
|
|
// if ($file instanceof UploadedFile) { |
86
|
|
|
// return $file->getMimeType(); |
87
|
|
|
// } |
88
|
|
|
|
89
|
|
|
return $this->lfm->mimeType(); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function extension() |
93
|
|
|
{ |
94
|
|
|
return $this->lfm->extension(); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function url() |
98
|
|
|
{ |
99
|
|
|
if ($this->isDirectory()) { |
100
|
|
|
return $this->lfm->path('working_dir'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $this->lfm->url(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function size() |
107
|
|
|
{ |
108
|
|
|
return $this->isFile() ? $this->humanFilesize($this->lfm->size()) : ''; |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function time() |
112
|
|
|
{ |
113
|
|
|
if (!$this->isDirectory()) { |
114
|
|
|
return $this->lfm->lastModified(); |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function thumbUrl() |
121
|
|
|
{ |
122
|
|
|
if ($this->isDirectory()) { |
123
|
|
|
return asset('vendor/' . Lfm::PACKAGE_NAME . '/img/folder.png'); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if ($this->isImage()) { |
127
|
|
|
return $this->lfm->thumb($this->hasThumb())->url(true); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return null; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function icon() |
134
|
|
|
{ |
135
|
|
|
if ($this->isDirectory()) { |
136
|
|
|
return 'fa-folder-o'; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
if ($this->isImage()) { |
140
|
|
|
return 'fa-image'; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $this->extension(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function type() |
147
|
|
|
{ |
148
|
|
|
if ($this->isDirectory()) { |
149
|
|
|
return trans(Lfm::PACKAGE_NAME . '::lfm.type-folder'); |
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if ($this->isImage()) { |
153
|
|
|
return $this->mimeType(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $this->helper->getFileType($this->extension()); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function hasThumb() |
160
|
|
|
{ |
161
|
|
|
if (!$this->isImage()) { |
162
|
|
|
return false; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
if (!$this->lfm->thumb()->exists()) { |
|
|
|
|
166
|
|
|
return false; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return true; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function shouldCreateThumb() |
173
|
|
|
{ |
174
|
|
|
if (!$this->helper->config('should_create_thumbnails')) { |
175
|
|
|
return false; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
if (!$this->isImage()) { |
179
|
|
|
return false; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
if (in_array($this->mimeType(), ['image/gif', 'image/svg+xml'])) { |
183
|
|
|
return false; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return true; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function get() |
190
|
|
|
{ |
191
|
|
|
return $this->lfm->get(); |
|
|
|
|
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Make file size readable. |
196
|
|
|
* |
197
|
|
|
* @param int $bytes File size in bytes. |
198
|
|
|
* @param int $decimals Decimals. |
199
|
|
|
* @return string |
200
|
|
|
*/ |
201
|
|
|
public function humanFilesize($bytes, $decimals = 2) |
202
|
|
|
{ |
203
|
|
|
$size = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; |
204
|
|
|
$factor = floor((strlen($bytes) - 1) / 3); |
205
|
|
|
|
206
|
|
|
return sprintf("%.{$decimals}f %s", $bytes / pow(1024, $factor), @$size[$factor]); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.