Issues (263)

lib/Service/SearchFolderService.php (2 issues)

1
<?php
2
/**
3
 * Nextcloud - Gallery
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 2017
11
 */
12
13
namespace OCA\Gallery\Service;
14
15
use OCP\Files\Folder;
0 ignored issues
show
The type OCP\Files\Folder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
17
use OCA\Gallery\Environment\NotFoundEnvException;
18
19
/**
20
 * Looks for the folder to use, based on the request made by the client
21
 *
22
 * This is to make sure we were not:
23
 *    * given a file
24
 *    * given a folder name with a typo
25
 *
26
 * @package OCA\Gallery\Service
27
 */
28
class SearchFolderService extends FilesService {
29
	/**
30
	 * @var int
31
	 */
32
	protected $virtualRootLevel = null;
33
34
	/**
35
	 * This returns what we think is the current folder node based on a given path
36
	 *
37
	 * @param string $location
38
	 * @param string[] $features
39
	 *
40
	 * @return array <string,Folder,bool>
41
	 */
42
	public function getCurrentFolder($location, $features) {
43
		$this->features = $features;
44
45
		return $this->findFolder($location);
46
	}
47
48
	/**
49
	 * This returns the current folder node based on a path
50
	 *
51
	 * If the path leads to a file, we'll return the node of the containing folder
52
	 *
53
	 * If we can't find anything, we try with the parent folder, up to the root or until we reach
54
	 * our recursive limit
55
	 *
56
	 * @param string $location
57
	 * @param int $depth
58
	 *
59
	 * @return array <string,Folder,bool>
60
	 */
61
	private function findFolder($location, $depth = 0) {
62
		$node = null;
0 ignored issues
show
The assignment to $node is dead and can be removed.
Loading history...
63
		$location = $this->validateLocation($location, $depth);
64
		try {
65
			$node = $this->environment->getNodeFromVirtualRoot($location);
66
			if ($node->getType() === 'file') {
67
				$node = $node->getParent();
68
			}
69
		} catch (NotFoundEnvException $exception) {
70
			// There might be a typo in the file or folder name
71
			$folder = pathinfo($location, PATHINFO_DIRNAME);
72
			$depth++;
73
74
			return $this->findFolder($folder, $depth);
75
		}
76
		$path = $this->environment->getPathFromVirtualRoot($node);
77
78
		return $this->sendFolder($path, $node);
79
	}
80
81
	/**
82
	 * Makes sure we don't go too far up before giving up
83
	 *
84
	 * @param string $location
85
	 * @param int $depth
86
	 *
87
	 * @return string
88
	 */
89
	private function validateLocation($location, $depth) {
90
		if ($depth === 4) {
91
			// We can't find anything, so we decide to return data for the root folder
92
			$location = '';
93
		}
94
95
		return $location;
96
	}
97
98
	/**
99
	 * Makes sure that the folder is not empty, does meet our requirements in terms of location and
100
	 * returns details about it
101
	 *
102
	 * @param string $path
103
	 * @param Folder $node
104
	 *
105
	 * @return array <string,Folder,bool>
106
	 * @throws ForbiddenServiceException|NotFoundServiceException
107
	 */
108
	private function sendFolder($path, $node) {
109
		if (is_null($node)) {
110
			// Something very wrong has just happened
111
			throw new NotFoundServiceException('Oh Nooooes!');
112
		} elseif (!$this->isAllowedAndAvailable($node)) {
113
			throw new ForbiddenServiceException(
114
				'The owner has placed a restriction or the storage location is unavailable'
115
			);
116
		}
117
118
		return [$path, $node];
119
	}
120
121
}
122