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 | private $isDirectory; |
||||
13 | private $mimeType = null; |
||||
14 | |||||
15 | private $columns = []; |
||||
16 | public $attributes = []; |
||||
17 | |||||
18 | public function __construct(LfmPath $lfm, Lfm $helper, $isDirectory = false) |
||||
19 | { |
||||
20 | $this->lfm = $lfm->thumb(false); |
||||
21 | $this->helper = $helper; |
||||
22 | $this->isDirectory = $isDirectory; |
||||
23 | $this->columns = $helper->config('item_columns') ?: |
||||
24 | ['name', 'url', 'time', 'icon', 'is_file', 'is_image', 'thumb_url']; |
||||
25 | } |
||||
26 | |||||
27 | public function __get($var_name) |
||||
28 | { |
||||
29 | if (!array_key_exists($var_name, $this->attributes)) { |
||||
30 | $function_name = Str::camel($var_name); |
||||
31 | $this->attributes[$var_name] = $this->$function_name(); |
||||
32 | } |
||||
33 | |||||
34 | return $this->attributes[$var_name]; |
||||
35 | } |
||||
36 | |||||
37 | public function fill() |
||||
38 | { |
||||
39 | foreach ($this->columns as $column) { |
||||
40 | $this->__get($column); |
||||
41 | } |
||||
42 | |||||
43 | return $this; |
||||
44 | } |
||||
45 | |||||
46 | public function name() |
||||
47 | { |
||||
48 | return $this->lfm->getName(); |
||||
49 | } |
||||
50 | |||||
51 | public function path($type = 'absolute') |
||||
52 | { |
||||
53 | return $this->lfm->path($type); |
||||
54 | } |
||||
55 | |||||
56 | public function isDirectory() |
||||
57 | { |
||||
58 | return $this->isDirectory; |
||||
59 | } |
||||
60 | |||||
61 | public function isFile() |
||||
62 | { |
||||
63 | return ! $this->isDirectory(); |
||||
64 | } |
||||
65 | |||||
66 | /** |
||||
67 | * Check a file is image or not. |
||||
68 | * |
||||
69 | * @param mixed $file Real path of a file or instance of UploadedFile. |
||||
70 | * @return bool |
||||
71 | */ |
||||
72 | public function isImage() |
||||
73 | { |
||||
74 | return $this->isFile() && Str::startsWith($this->mimeType(), 'image'); |
||||
75 | } |
||||
76 | |||||
77 | /** |
||||
78 | * Get mime type of a file. |
||||
79 | * |
||||
80 | * @param mixed $file Real path of a file or instance of UploadedFile. |
||||
81 | * @return string |
||||
82 | */ |
||||
83 | public function mimeType() |
||||
84 | { |
||||
85 | if (is_null($this->mimeType)) { |
||||
86 | $this->mimeType = $this->lfm->mimeType(); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
87 | } |
||||
88 | |||||
89 | return $this->mimeType; |
||||
90 | } |
||||
91 | |||||
92 | public function extension() |
||||
93 | { |
||||
94 | return $this->lfm->extension(); |
||||
0 ignored issues
–
show
The method
extension() does not exist on UniSharp\LaravelFilemanager\LfmPath . Since you implemented __call , consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
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()) : ''; |
||||
0 ignored issues
–
show
The method
size() does not exist on UniSharp\LaravelFilemanager\LfmPath . Since you implemented __call , consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
109 | } |
||||
110 | |||||
111 | public function time() |
||||
112 | { |
||||
113 | return $this->lfm->lastModified(); |
||||
0 ignored issues
–
show
The method
lastModified() does not exist on UniSharp\LaravelFilemanager\LfmPath . Since you implemented __call , consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
114 | } |
||||
115 | |||||
116 | public function thumbUrl() |
||||
117 | { |
||||
118 | if ($this->isDirectory()) { |
||||
119 | return asset('vendor/' . Lfm::PACKAGE_NAME . '/img/folder.png'); |
||||
0 ignored issues
–
show
The function
asset was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
120 | } |
||||
121 | |||||
122 | if ($this->isImage()) { |
||||
123 | return $this->lfm->thumb($this->hasThumb())->url(true); |
||||
0 ignored issues
–
show
The call to
UniSharp\LaravelFilemanager\LfmPath::url() has too many arguments starting with true .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue. If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above. ![]() |
|||||
124 | } |
||||
125 | |||||
126 | return null; |
||||
127 | } |
||||
128 | |||||
129 | public function icon() |
||||
130 | { |
||||
131 | if ($this->isDirectory()) { |
||||
132 | return 'fa-folder-o'; |
||||
133 | } |
||||
134 | |||||
135 | if ($this->isImage()) { |
||||
136 | return 'fa-image'; |
||||
137 | } |
||||
138 | |||||
139 | return $this->extension(); |
||||
140 | } |
||||
141 | |||||
142 | public function type() |
||||
143 | { |
||||
144 | if ($this->isDirectory()) { |
||||
145 | return trans(Lfm::PACKAGE_NAME . '::lfm.type-folder'); |
||||
0 ignored issues
–
show
The function
trans was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
146 | } |
||||
147 | |||||
148 | if ($this->isImage()) { |
||||
149 | return $this->mimeType(); |
||||
150 | } |
||||
151 | |||||
152 | return $this->helper->getFileType($this->extension()); |
||||
153 | } |
||||
154 | |||||
155 | public function hasThumb() |
||||
156 | { |
||||
157 | if (!$this->isImage()) { |
||||
158 | return false; |
||||
159 | } |
||||
160 | |||||
161 | if (!$this->lfm->thumb()->exists()) { |
||||
0 ignored issues
–
show
The method
exists() does not exist on UniSharp\LaravelFilemanager\LfmPath . Since you implemented __call , consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
162 | return false; |
||||
163 | } |
||||
164 | |||||
165 | return true; |
||||
166 | } |
||||
167 | |||||
168 | public function shouldCreateThumb() |
||||
169 | { |
||||
170 | if (!$this->helper->config('should_create_thumbnails')) { |
||||
171 | return false; |
||||
172 | } |
||||
173 | |||||
174 | if (!$this->isImage()) { |
||||
175 | return false; |
||||
176 | } |
||||
177 | |||||
178 | if (in_array($this->mimeType(), ['image/gif', 'image/svg+xml'])) { |
||||
179 | return false; |
||||
180 | } |
||||
181 | |||||
182 | return true; |
||||
183 | } |
||||
184 | |||||
185 | public function get() |
||||
186 | { |
||||
187 | return $this->lfm->get(); |
||||
0 ignored issues
–
show
The method
get() does not exist on UniSharp\LaravelFilemanager\LfmPath . Since you implemented __call , consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
188 | } |
||||
189 | |||||
190 | /** |
||||
191 | * Make file size readable. |
||||
192 | * |
||||
193 | * @param int $bytes File size in bytes. |
||||
194 | * @param int $decimals Decimals. |
||||
195 | * @return string |
||||
196 | */ |
||||
197 | public function humanFilesize($bytes, $decimals = 2) |
||||
198 | { |
||||
199 | $size = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; |
||||
200 | $factor = floor((strlen($bytes) - 1) / 3); |
||||
201 | |||||
202 | return sprintf("%.{$decimals}f %s", $bytes / pow(1024, $factor), @$size[$factor]); |
||||
203 | } |
||||
204 | } |
||||
205 |