Completed
Push — master ( c9612b...1c88e5 )
by Lukas
15:34
created

ShareInfoController::info()   D

Complexity

Conditions 9
Paths 9

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 18
c 1
b 0
f 0
nc 9
nop 3
dl 0
loc 32
rs 4.909
1
<?php
2
3
namespace OCA\Files_Sharing\Controller;
4
5
use OCA\Files_External\NotFoundException;
6
use OCP\AppFramework\ApiController;
7
use OCP\AppFramework\Http;
8
use OCP\AppFramework\Http\JSONResponse;
9
use OCP\Constants;
10
use OCP\Files\File;
11
use OCP\Files\Folder;
12
use OCP\Files\Node;
13
use OCP\ILogger;
14
use OCP\IRequest;
15
use OCP\Share\Exceptions\ShareNotFound;
16
use OCP\Share\IManager;
17
18
class ShareInfoController extends ApiController {
19
20
	/** @var IManager */
21
	private $shareManager;
22
23
	/**
24
	 * ShareInfoController constructor.
25
	 *
26
	 * @param string $appName
27
	 * @param IRequest $request
28
	 * @param IManager $shareManager
29
	 */
30
	public function __construct($appName,
31
								IRequest $request,
32
								IManager $shareManager) {
33
		parent::__construct($appName, $request);
34
35
		$this->shareManager = $shareManager;
36
	}
37
38
	/**
39
	 * @PublicPage
40
	 * @NoCSRFRequired
41
	 *
42
	 * @param string $t
43
	 * @param null $password
44
	 * @param null $dir
45
	 * @return JSONResponse
46
	 * @throws ShareNotFound
47
	 */
48
	public function info($t, $password = null, $dir = null) {
49
		try {
50
			$share = $this->shareManager->getShareByToken($t);
51
		} catch (ShareNotFound $e) {
52
			return new JSONResponse([], Http::STATUS_NOT_FOUND);
53
		}
54
55
		if ($share->getPassword() && !$this->shareManager->checkPassword($share, $password)) {
56
			return new JSONResponse([], Http::STATUS_FORBIDDEN);
57
		}
58
59
		if (!($share->getPermissions() & Constants::PERMISSION_READ)) {
60
			return new JSONResponse([], Http::STATUS_FORBIDDEN);
61
		}
62
63
		$isWritable = $share->getPermissions() & (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_CREATE);
64
		if (!$isWritable) {
65
			$this->addROWrapper();
66
		}
67
68
		$node = $share->getNode();
69
70
		if ($dir !== null && $node instanceof Folder) {
71
			try {
72
				$node = $node->get($dir);
73
			} catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
74
75
			}
76
		}
77
78
		return new JSONResponse($this->parseNode($node));
79
	}
80
81
	private function parseNode(Node $node) {
82
		if ($node instanceof File) {
83
			return $this->parseFile($node);
84
		}
85
		return $this->parseFolder($node);
0 ignored issues
show
Compatibility introduced by
$node of type object<OCP\Files\Node> is not a sub-type of object<OCP\Files\Folder>. It seems like you assume a child interface of the interface OCP\Files\Node to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
86
	}
87
88
	private function parseFile(File $file) {
89
		return $this->format($file);
90
	}
91
92
	private function parseFolder(Folder $folder) {
93
		$data = $this->format($folder);
94
95
		$data['children'] = [];
96
97
		$nodes = $folder->getDirectoryListing();
98
		foreach ($nodes as $node) {
99
			$data['children'][] = $this->parseNode($node);
100
		}
101
102
		return $data;
103
	}
104
105
	private function format(Node $node) {
106
		$entry = [];
107
108
		$entry['id'] = $node->getId();
109
		$entry['parentId'] = $node->getParent()->getId();
110
		$entry['mtime'] = $node->getMTime();
111
112
		$entry['name'] = $node->getName();
113
		$entry['permissions'] = $node->getPermissions();
114
		$entry['mimetype'] = $node->getMimetype();
115
		$entry['size'] = $node->getSize();
116
		$entry['type'] = $node->getType();
117
		$entry['etag'] = $node->getEtag();
118
119
		return $entry;
120
	}
121
122
	protected function addROWrapper() {
123
		// FIXME: should not add storage wrappers outside of preSetup, need to find a better way
124
		$previousLog = \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(false);
125
		\OC\Files\Filesystem::addStorageWrapper('readonly', function ($mountPoint, $storage) {
126
			return new \OC\Files\Storage\Wrapper\PermissionsMask(array('storage' => $storage, 'mask' => \OCP\Constants::PERMISSION_READ + \OCP\Constants::PERMISSION_SHARE));
127
		});
128
		\OC\Files\Filesystem::logWarningWhenAddingStorageWrapper($previousLog);
129
	}
130
}
131