1 | <?php |
||
24 | abstract class FilesService extends Service { |
||
25 | |||
26 | /** @var int */ |
||
27 | protected $virtualRootLevel = null; |
||
28 | /** @var string[] */ |
||
29 | protected $features; |
||
30 | /** @var string */ |
||
31 | protected $ignoreAlbum = '.nomedia'; |
||
32 | |||
33 | /** |
||
34 | * Retrieves all files and sub-folders contained in a folder |
||
35 | * |
||
36 | * If we can't find anything in the current folder, we throw an exception as there is no point |
||
37 | * in doing any more work, but if we're looking at a sub-folder, we return an empty array so |
||
38 | * that it can be simply ignored |
||
39 | * |
||
40 | * @param Folder $folder |
||
41 | * @param int $subDepth |
||
42 | * |
||
43 | * @return array |
||
44 | */ |
||
45 | 15 | protected function getNodes($folder, $subDepth) { |
|
46 | try { |
||
47 | 15 | $nodes = $folder->getDirectoryListing(); |
|
48 | 2 | } catch (\Exception $exception) { |
|
49 | 2 | $nodes = $this->recoverFromGetNodesError($subDepth, $exception); |
|
50 | } |
||
51 | |||
52 | 14 | return $nodes; |
|
53 | } |
||
54 | |||
55 | /** |
||
56 | * Determines if the files are hosted locally (shared or not) and can be used by the preview |
||
57 | * system |
||
58 | * |
||
59 | * isMounted() doesn't include externally hosted shares, so we need to exclude those from the |
||
60 | * non-mounted nodes |
||
61 | * |
||
62 | * @param Node $node |
||
63 | * |
||
64 | * @return bool |
||
65 | */ |
||
66 | 35 | protected function isAllowedAndAvailable($node) { |
|
67 | try { |
||
68 | 35 | return $node && $this->isAllowed($node) && $this->isAvailable($node); |
|
69 | 2 | } catch (\Exception $exception) { |
|
70 | 2 | $message = 'The folder is not available: ' . $exception->getMessage(); |
|
71 | 2 | $this->logger->error($message); |
|
72 | |||
73 | 2 | return false; |
|
74 | } |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Returns the node type, either 'dir' or 'file' |
||
79 | * |
||
80 | * If there is a problem, we return an empty string so that the node can be ignored |
||
81 | * |
||
82 | * @param Node $node |
||
83 | * |
||
84 | * @return string |
||
85 | */ |
||
86 | 14 | protected function getNodeType($node) { |
|
87 | try { |
||
88 | 14 | $nodeType = $node->getType(); |
|
89 | 1 | } catch (\Exception $exception) { |
|
90 | 1 | return ''; |
|
91 | } |
||
92 | |||
93 | 13 | return $nodeType; |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * Returns various information about a node |
||
98 | * |
||
99 | * @param Node|File|Folder $node |
||
100 | * |
||
101 | * @return array<string,int|string|bool|array<string,int|string>> |
||
102 | */ |
||
103 | 13 | protected function getNodeData($node) { |
|
119 | |||
120 | /** |
||
121 | * Returns various information about a folder |
||
122 | * |
||
123 | * @param Folder $node |
||
124 | * |
||
125 | * @return array<string,int|string|bool|array<string,int|string>> |
||
126 | */ |
||
127 | 13 | protected function getFolderData($node) { |
|
133 | |||
134 | /** |
||
135 | * Returns the node if it's a folder we have access to |
||
136 | * |
||
137 | * @param Folder $node |
||
138 | * @param string $nodeType |
||
139 | * |
||
140 | * @return array|Folder |
||
141 | */ |
||
142 | 14 | protected function getAllowedSubFolder($node, $nodeType) { |
|
152 | |||
153 | /** |
||
154 | * Determines if we've reached the root folder |
||
155 | * |
||
156 | * @param Folder $folder |
||
157 | * @param int $level |
||
158 | * |
||
159 | * @return bool |
||
160 | */ |
||
161 | 5 | protected function isRootFolder($folder, $level) { |
|
162 | 5 | $isRootFolder = false; |
|
163 | 5 | $rootFolder = $this->environment->getVirtualRootFolder(); |
|
164 | 5 | if ($folder->getPath() === $rootFolder->getPath()) { |
|
165 | 5 | $isRootFolder = true; |
|
166 | } |
||
167 | 5 | $virtualRootFolder = $this->environment->getPathFromVirtualRoot($folder); |
|
168 | 5 | if (empty($virtualRootFolder)) { |
|
169 | 5 | $this->virtualRootLevel = $level; |
|
170 | } |
||
171 | |||
172 | 5 | return $isRootFolder; |
|
173 | } |
||
174 | |||
175 | /** |
||
176 | * Throws an exception if this problem occurs in the current folder, otherwise just ignores the |
||
177 | * sub-folder |
||
178 | * |
||
179 | * @param int $subDepth |
||
180 | * @param \Exception $exception |
||
181 | * |
||
182 | * @return array |
||
183 | * @throws NotFoundServiceException |
||
184 | */ |
||
185 | 4 | private function recoverFromGetNodesError($subDepth, $exception) { |
|
186 | 4 | if ($subDepth === 0) { |
|
187 | 2 | throw new NotFoundServiceException($exception->getMessage()); |
|
188 | } |
||
189 | |||
190 | 2 | return []; |
|
191 | } |
||
192 | |||
193 | /** |
||
194 | * Determines if we can consider the node mounted locally or if it's been authorised to be |
||
195 | * scanned |
||
196 | * |
||
197 | * @param Node $node |
||
198 | * |
||
199 | * @return bool |
||
200 | */ |
||
201 | 34 | private function isAllowed($node) { |
|
202 | 34 | $allowed = true; |
|
203 | 34 | if ($this->isExternalShare($node)) { |
|
204 | 6 | $allowed = $this->isExternalShareAllowed(); |
|
205 | } |
||
206 | |||
207 | 33 | if ($node->isMounted()) { |
|
208 | 3 | $mount = $node->getMountPoint(); |
|
209 | 3 | $allowed = $mount && $mount->getOption('previews', true); |
|
210 | } |
||
211 | |||
212 | 33 | return $allowed; |
|
213 | } |
||
214 | |||
215 | /** |
||
216 | * Determines if the node is available, as in readable |
||
217 | * |
||
218 | * @todo Test to see by how much using file_exists slows things down |
||
219 | * |
||
220 | * @param Node $node |
||
221 | * |
||
222 | * @return bool |
||
223 | */ |
||
224 | 31 | private function isAvailable($node) { |
|
225 | 31 | return $node->isReadable(); |
|
226 | } |
||
227 | |||
228 | /** |
||
229 | * Determines if the user has allowed the use of external shares |
||
230 | * |
||
231 | * @return bool |
||
232 | */ |
||
233 | 6 | private function isExternalShareAllowed() { |
|
239 | |||
240 | /** |
||
241 | * Determines if the node is a share which is hosted externally |
||
242 | * |
||
243 | * |
||
244 | * @param Node $node |
||
245 | * |
||
246 | * @return bool |
||
247 | */ |
||
248 | 34 | private function isExternalShare($node) { |
|
257 | |||
258 | /** |
||
259 | * Returns what we known about the owner of a node |
||
260 | * |
||
261 | * @param Node $node |
||
262 | * |
||
263 | * @return null|array<string,int|string> |
||
264 | */ |
||
265 | 13 | private function getOwnerData($node) { |
|
277 | |||
278 | /** |
||
279 | * Returns an array containing information about a node |
||
280 | * |
||
281 | * @param string $imagePath |
||
282 | * @param int $nodeId |
||
283 | * @param int $mTime |
||
284 | * @param string $etag |
||
285 | * @param int $size |
||
286 | * @param bool $sharedWithUser |
||
287 | * @param array <string,int|string> $ownerData |
||
288 | * @param int $permissions |
||
289 | * |
||
290 | * @return array |
||
291 | */ |
||
292 | 13 | private function formatNodeData( |
|
306 | |||
307 | } |
||
308 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.