summaryrefslogtreecommitdiffstats
path: root/apps/files_sharing/lib/Controller/ShareInfoController.php
blob: d926721f06c5e7f4df9da0e65f3318e062f4726c (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
<?php

namespace OCA\Files_Sharing\Controller;

use OCA\Files_External\NotFoundException;
use OCP\AppFramework\ApiController;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\Constants;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\Node;
use OCP\ILogger;
use OCP\IRequest;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager;

class ShareInfoController extends ApiController {

	/** @var IManager */
	private $shareManager;

	/** @var ILogger */
	private $logger;

	public function __construct($appName,
								IRequest $request,
								IManager $shareManager,
								ILogger $logger) {
		parent::__construct($appName, $request);

		$this->shareManager = $shareManager;
		$this->logger = $logger;
	}

	/**
	 * @PublicPage
	 * @NoCSRFRequired
	 *
	 * @param string $t
	 * @param null $password
	 * @param null $dir
	 * @return JSONResponse
	 */
	public function info($t, $password = null, $dir = null) {
		$this->logger->error('HERE!');
		try {
			$share = $this->shareManager->getShareByToken($t);
		} catch (ShareNotFound $e) {
			return new JSONResponse([], Http::STATUS_NOT_FOUND);
		}

		if ($share->getPassword() && !$this->shareManager->checkPassword($share, $password)) {
			return new JSONResponse([], Http::STATUS_FORBIDDEN);
		}

		if (!($share->getPermissions() & Constants::PERMISSION_READ)) {
			return new JSONResponse([], Http::STATUS_FORBIDDEN);
		}

		// TODO FIX!!!
		$isWritable = $share->getPermissions() & (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_CREATE);
		if (!$isWritable) {
			// FIXME: should not add storage wrappers outside of preSetup, need to find a better way
			$previousLog = \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(false);
			\OC\Files\Filesystem::addStorageWrapper('readonly', function ($mountPoint, $storage) {
				return new \OC\Files\Storage\Wrapper\PermissionsMask(array('storage' => $storage, 'mask' => \OCP\Constants::PERMISSION_READ + \OCP\Constants::PERMISSION_SHARE));
			});
			\OC\Files\Filesystem::logWarningWhenAddingStorageWrapper($previousLog);
		}

		$node = $share->getNode();

		if ($dir !== null && $node instanceof Folder) {
			try {
				$node = $node->get($dir);
			} catch (NotFoundException $e) {

			}
		}

		$result = [
			'data' => $this->parseNode($node),
			'status' => 'success'
		];

		return new JSONResponse($result);
	}

	private function parseNode(Node $node) {
		if ($node instanceof File) {
			return $this->parseFile($node);
		}
		return $this->parseFolder($node);
	}

	private function parseFile(File $file) {
		return $this->format($file);
	}

	private function parseFolder(Folder $folder) {
		$data = $this->format($folder);

		$data['children'] = [];

		$nodes = $folder->getDirectoryListing();
		foreach ($nodes as $node) {
			$data['children'][] = $this->parseNode($node);
		}

		return $data;
	}

	private function format(Node $node) {
		$entry = [];

		$entry['id'] = $node->getId();
		$entry['parentId'] = $node->getParent()->getId();
		$entry['mtime'] = $node->getMTime();

		$entry['name'] = $node->getName();
		$entry['permissions'] = $node->getPermissions();
		$entry['mimetype'] = $node->getMimetype();
		$entry['size'] = $node->getSize();
		$entry['type'] = $node->getType();
		$entry['etag'] = $node->getEtag();

		return $entry;
	}
}