Completed
Pull Request — stable8.2 (#40)
by Olivier
61:07
created

FilesService::getNodeData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 16
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 11
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * ownCloud - galleryplus
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Olivier Paroz <[email protected]>
9
 *
10
 * @copyright Olivier Paroz 2014-2015
11
 */
12
13
namespace OCA\GalleryPlus\Service;
14
15
use OCP\Files\File;
16
use OCP\Files\Folder;
17
use OCP\Files\Node;
18
19
/**
20
 * Contains various methods to retrieve information from the filesystem
21
 *
22
 * @package OCA\GalleryPlus\Service
23
 */
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
	protected function getNodes($folder, $subDepth) {
46 14
		try {
47
			$nodes = $folder->getDirectoryListing();
48 14
		} catch (\Exception $exception) {
49 14
			$nodes = $this->recoverFromGetNodesError($subDepth, $exception);
50 2
		}
51
52
		return $nodes;
53 13
	}
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
	protected function isAllowedAndAvailable($node) {
67 33
		try {
68
			return $node && $this->isAllowed($node) && $this->isAvailable($node);
69 33
		} catch (\Exception $exception) {
70 2
			$message = 'The folder is not available: ' . $exception->getMessage();
71 2
			$this->logger->error($message);
72 2
73
			return false;
74 2
		}
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
	protected function getNodeType($node) {
87 12
		try {
88
			$nodeType = $node->getType();
89 12
		} catch (\Exception $exception) {
90 12
			return '';
91 1
		}
92
93
		return $nodeType;
94 11
	}
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
	protected function getNodeData($node) {
104
		$imagePath = $this->environment->getPathFromVirtualRoot($node);
105 12
		$nodeId = $node->getId();
106 12
		$mTime = $node->getMTime();
107
		$etag = $node->getEtag();
108 11
		$size = $node->getSize();
109 10
		$sharedWithUser = $node->isShared();
110
		$ownerData = $this->getOwnerData($node);
111 1
		$permissions = $node->getPermissions();
112
113 12
		//$this->logger->debug("Image path : {var1}", ['var1' => $imagePath]);
114
115
		return $this->formatNodeData(
116
			$imagePath, $nodeId, $mTime, $etag, $size, $sharedWithUser, $ownerData, $permissions
117
		);
118
	}
119
120
	/**
121
	 * Returns various information about a folder
122
	 *
123
	 * @param Folder $node
124 5
	 *
125 5
	 * @return array<string,int|string|bool|array<string,int|string>>
126 5
	 */
127 5
	protected function getFolderData($node) {
128 5
		$folderData = $this->getNodeData($node);
129
		$folderData['freespace'] = $node->getFreeSpace();
130 5
131 5
		return $folderData;
132 5
	}
133
134
	/**
135 5
	 * 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
	protected function getAllowedSubFolder($node, $nodeType) {
143
		if ($nodeType === 'dir') {
144
			/** @var Folder $node */
145
			if (!$node->nodeExists($this->ignoreAlbum)) {
146
				return [$node];
147
			}
148 4
		}
149 4
150 2
		return [];
151
	}
152
153 2
	/**
154
	 * Determines if we've reached the root folder
155
	 *
156
	 * @param Folder $folder
157
	 * @param int $level
158
	 *
159
	 * @return bool
160
	 */
161
	protected function isRootFolder($folder, $level) {
162
		$isRootFolder = false;
163
		$rootFolder = $this->environment->getVirtualRootFolder();
164 32
		if ($folder->getPath() === $rootFolder->getPath()) {
165 32
			$isRootFolder = true;
166 32
		}
167 6
		$virtualRootFolder = $this->environment->getPathFromVirtualRoot($folder);
168 5
		if (empty($virtualRootFolder)) {
169
			$this->virtualRootLevel = $level;
170 31
		}
171 3
172 3
		return $isRootFolder;
173 3
	}
174
175 31
	/**
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
	private function recoverFromGetNodesError($subDepth, $exception) {
186
		if ($subDepth === 0) {
187 29
			throw new NotFoundServiceException($exception->getMessage());
188 29
		}
189
190
		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 6
	 *
197 6
	 * @param Node $node
198 6
	 *
199 4
	 * @return bool
200
	 */
201
	private function isAllowed($node) {
202 1
		$allowed = true;
203
		if ($this->isExternalShare($node)) {
204
			$allowed = $this->isExternalShareAllowed();
205
		}
206
207
		if ($node->isMounted()) {
208
			$mount = $node->getMountPoint();
209
			$allowed = $mount && $mount->getOption('previews', true);
210
		}
211
212
		return $allowed;
213 32
	}
214 32
215 32
	/**
216 32
	 * Determines if the node is available, as in readable
217 32
	 *
218 32
	 * @todo Test to see by how much using file_exists slows things down
219
	 *
220 32
	 * @param Node $node
221
	 *
222
	 * @return bool
223
	 */
224
	private function isAvailable($node) {
225
		return $node->isReadable();
226
	}
227
228
	/**
229
	 * Determines if the user has allowed the use of external shares
230
	 *
231
	 * @return bool
232
	 */
233
	private function isExternalShareAllowed() {
234
		$rootFolder = $this->environment->getVirtualRootFolder();
235
236
		return ($this->isExternalShare($rootFolder)
0 ignored issues
show
Bug introduced by
It seems like $rootFolder defined by $this->environment->getVirtualRootFolder() on line 234 can also be of type null; however, OCA\GalleryPlus\Service\...vice::isExternalShare() does only seem to accept object<OCP\Files\Node>, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
237
				|| in_array('external_shares', $this->features));
238
	}
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
	private function isExternalShare($node) {
249
		$sid = explode(
250
			':',
251
			$node->getStorage()
252
				 ->getId()
253
		);
254
255
		return ($sid[0] === 'shared' && $sid[2][0] !== '/');
256
	}
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>
0 ignored issues
show
Documentation introduced by
Should the return type not be array? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
264
	 */
265
	private function getOwnerData($node) {
266
		// On 8.2 we don't have getOwner()
267
		//$owner = $node->getOwner();
268
		$storage = $node->getStorage();
269
		$owner = \OC::$server->getUserManager()
270
							 ->get($storage->getOwner($node->getInternalPath()));
271
		$ownerData = [];
272
		if ($owner) {
273
			$ownerData = [
274
				'uid'         => $owner->getUID(),
275
				'displayname' => $owner->getDisplayName()
276
			];
277
		}
278
279
		return $ownerData;
280
	}
281
282
	/**
283
	 * Returns an array containing information about a node
284
	 *
285
	 * @param string $imagePath
286
	 * @param int $nodeId
287
	 * @param int $mTime
288
	 * @param string $etag
289
	 * @param int $size
290
	 * @param bool $sharedWithUser
291
	 * @param array <string,int|string> $ownerData
292
	 * @param int $permissions
293
	 *
294
	 * @return array
295
	 */
296
	private function formatNodeData(
297
		$imagePath, $nodeId, $mTime, $etag, $size, $sharedWithUser, $ownerData, $permissions
298
	) {
299
		return [
300
			'path'           => $imagePath,
301
			'nodeid'         => $nodeId,
302
			'mtime'          => $mTime,
303
			'etag'           => $etag,
304
			'size'           => $size,
305
			'sharedwithuser' => $sharedWithUser,
306
			'owner'          => $ownerData,
307
			'permissions'    => $permissions
308
		];
309
	}
310
311
}
312