|
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) { |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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
|
|
|
|