Welcome to mirror list, hosted at ThFree Co, Russian Federation.

filesservice.php « service - github.com/nextcloud/gallery.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 021b43cb069e222dd32850781aac24a42bbcc695 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
/**
 * ownCloud - gallery
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Olivier Paroz <owncloud@interfasys.ch>
 *
 * @copyright Olivier Paroz 2014-2015
 */

namespace OCA\Gallery\Service;

use OCP\Files\Folder;
use OCP\Files\Node;

/**
 * Contains various methods to retrieve information from the filesystem
 *
 * @package OCA\Gallery\Service
 */
abstract class FilesService extends Service {
	/**
	 * @var int
	 */
	protected $virtualRootLevel = null;

	/**
	 * @var string[]
	 */
	protected $features;

	/**
	 * Retrieves all files and sub-folders contained in a folder
	 *
	 * If we can't find anything in the current folder, we throw an exception as there is no point
	 * in doing any more work, but if we're looking at a sub-folder, we return an empty array so
	 * that it can be simply ignored
	 *
	 * @param Folder $folder
	 * @param int $subDepth
	 *
	 * @return array
	 */
	protected function getNodes($folder, $subDepth) {
		try {
			$nodes = $folder->getDirectoryListing();
		} catch (\Exception $exception) {
			$nodes = $this->recoverFromGetNodesError($subDepth, $exception);
		}

		return $nodes;
	}

	/**
	 * Determines if the files are hosted locally (shared or not) and can be used by the preview
	 * system
	 *
	 * isMounted() doesn't include externally hosted shares, so we need to exclude those from the
	 * non-mounted nodes
	 *
	 * @param Node $node
	 *
	 * @return bool
	 */
	protected function isAllowedAndAvailable($node) {
		try {
			return $node && $this->isAllowed($node) && $this->isAvailable($node);
		} catch (\Exception $exception) {
			$message = 'The folder is not available: ' . $exception->getMessage();
			$this->logger->error($message);

			return false;
		}
	}

	/**
	 * Returns the node type, either 'dir' or 'file'
	 *
	 * If there is a problem, we return an empty string so that the node can be ignored
	 *
	 * @param Node $node
	 *
	 * @return string
	 */
	protected function getNodeType($node) {
		try {
			$nodeType = $node->getType();
		} catch (\Exception $exception) {
			return '';
		}

		return $nodeType;
	}

	/**
	 * Returns the node if it's a folder we have access to
	 *
	 * @param Folder $node
	 * @param string $nodeType
	 *
	 * @return array|Folder
	 */
	protected function getAllowedSubFolder($node, $nodeType) {
		if ($nodeType === 'dir') {
			/** @var Folder $node */
			if (!$node->nodeExists('.nomedia')) {
				return [$node];
			}
		}

		return [];
	}

	/**
	 * Determines if we've reached the root folder
	 *
	 * @param Folder $folder
	 * @param int $level
	 *
	 * @return bool
	 */
	protected function isRootFolder($folder, $level) {
		$isRootFolder = false;
		$rootFolder = $this->environment->getVirtualRootFolder();
		if ($folder->getPath() === $rootFolder->getPath()) {
			$isRootFolder = true;
		}
		$virtualRootFolder = $this->environment->getPathFromVirtualRoot($folder);
		if (empty($virtualRootFolder)) {
			$this->virtualRootLevel = $level;
		}

		return $isRootFolder;
	}

	/**
	 * Throws an exception if this problem occurs in the current folder, otherwise just ignores the
	 * sub-folder
	 *
	 * @param int $subDepth
	 * @param \Exception $exception
	 *
	 * @return array
	 * @throws NotFoundServiceException
	 */
	private function recoverFromGetNodesError($subDepth, $exception) {
		if ($subDepth === 0) {
			throw new NotFoundServiceException($exception->getMessage());
		}

		return [];
	}

	/**
	 * Determines if we can consider the node mounted locally or if it's been authorised to be
	 * scanned
	 *
	 * @param Node $node
	 *
	 * @return bool
	 */
	private function isAllowed($node) {
		$allowed = true;
		if ($this->isExternalShare($node)) {
			$allowed = $this->isExternalShareAllowed();
		}

		if ($node->isMounted()) {
			$mount = $node->getMountPoint();
			$allowed = $mount && $mount->getOption('previews', true);
		}

		return $allowed;
	}

	/**
	 * Determines if the node is available, as in readable
	 *
	 * @todo Test to see by how much using file_exists slows things down
	 *
	 * @param Node $node
	 *
	 * @return bool
	 */
	private function isAvailable($node) {
		return $node->isReadable();
	}

	/**
	 * Determines if the user has allowed the use of external shares
	 *
	 * @return bool
	 */
	private function isExternalShareAllowed() {
		$rootFolder = $this->environment->getVirtualRootFolder();
		if ($this->isExternalShare($rootFolder) || in_array('external_shares', $this->features)) {
			return true;
		}

		return false;
	}

	/**
	 * Determines if the node is a share which is hosted externally
	 *
	 *
	 * @param Node $node
	 *
	 * @return bool
	 */
	private function isExternalShare($node) {
		$sid = explode(
			':',
			$node->getStorage()
				 ->getId()
		);

		return ($sid[0] === 'shared' && $sid[2][0] !== '/');
	}

}