1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) 2018 Joas Schilling <[email protected]> |
5
|
|
|
* |
6
|
|
|
* @license GNU AGPL version 3 or any later version |
7
|
|
|
* |
8
|
|
|
* This program is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU Affero General Public License as |
10
|
|
|
* published by the Free Software Foundation, either version 3 of the |
11
|
|
|
* License, or (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU Affero General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU Affero General Public License |
19
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace OCA\Files\Collaboration\Resources; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
use OCP\Collaboration\Resources\IProvider; |
27
|
|
|
use OCP\Collaboration\Resources\IResource; |
28
|
|
|
use OCP\Collaboration\Resources\ResourceException; |
29
|
|
|
use OCP\Files\IRootFolder; |
30
|
|
|
use OCP\Files\Node; |
31
|
|
|
use OCP\IPreview; |
32
|
|
|
use OCP\IURLGenerator; |
33
|
|
|
use OCP\IUser; |
34
|
|
|
|
35
|
|
|
class ResourceProvider implements IProvider { |
36
|
|
|
|
37
|
|
|
public const RESOURCE_TYPE = 'file'; |
38
|
|
|
|
39
|
|
|
/** @var IRootFolder */ |
40
|
|
|
protected $rootFolder; |
41
|
|
|
/** @var IPreview */ |
42
|
|
|
private $preview; |
43
|
|
|
/** @var IURLGenerator */ |
44
|
|
|
private $urlGenerator; |
45
|
|
|
|
46
|
|
|
/** @var array */ |
47
|
|
|
protected $nodes = []; |
48
|
|
|
|
49
|
|
|
public function __construct(IRootFolder $rootFolder, |
50
|
|
|
IPreview $preview, |
51
|
|
|
IURLGenerator $urlGenerator) { |
52
|
|
|
$this->rootFolder = $rootFolder; |
53
|
|
|
$this->preview = $preview; |
54
|
|
|
$this->urlGenerator = $urlGenerator; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function getNode(IResource $resource): ?Node { |
58
|
|
|
if (isset($this->nodes[(int) $resource->getId()])) { |
59
|
|
|
return $this->nodes[(int) $resource->getId()]; |
60
|
|
|
} |
61
|
|
|
$nodes = $this->rootFolder->getById((int) $resource->getId()); |
62
|
|
|
if (!empty($nodes)) { |
63
|
|
|
$this->nodes[(int) $resource->getId()] = array_shift($nodes); |
64
|
|
|
return $this->nodes[(int) $resource->getId()]; |
65
|
|
|
} |
66
|
|
|
return null; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param IResource $resource |
71
|
|
|
* @return array |
72
|
|
|
* @since 16.0.0 |
73
|
|
|
*/ |
74
|
|
|
public function getResourceRichObject(IResource $resource): array { |
75
|
|
|
if (isset($this->nodes[(int) $resource->getId()])) { |
76
|
|
|
$node = $this->nodes[(int) $resource->getId()]->getPath(); |
77
|
|
|
} else { |
78
|
|
|
$node = $this->getNode($resource); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ($node instanceof Node) { |
82
|
|
|
$link = $this->urlGenerator->linkToRouteAbsolute( |
83
|
|
|
'files.viewcontroller.showFile', |
84
|
|
|
['fileid' => $resource->getId()] |
85
|
|
|
); |
86
|
|
|
return [ |
87
|
|
|
'type' => 'file', |
88
|
|
|
'id' => $resource->getId(), |
89
|
|
|
'name' => $node->getName(), |
90
|
|
|
'path' => $node->getInternalPath(), |
91
|
|
|
'link' => $link, |
92
|
|
|
'mimetype' => $node->getMimetype(), |
93
|
|
|
'preview-available' => $this->preview->isAvailable($node), |
94
|
|
|
]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
throw new ResourceException('File not found'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Can a user/guest access the collection |
102
|
|
|
* |
103
|
|
|
* @param IResource $resource |
104
|
|
|
* @param IUser $user |
105
|
|
|
* @return bool |
106
|
|
|
* @since 16.0.0 |
107
|
|
|
*/ |
108
|
|
|
public function canAccessResource(IResource $resource, IUser $user = null): bool { |
109
|
|
|
if (!$user instanceof IUser) { |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$userFolder = $this->rootFolder->getUserFolder($user->getUID()); |
114
|
|
|
$nodes = $userFolder->getById((int) $resource->getId()); |
115
|
|
|
|
116
|
|
|
if (!empty($nodes)) { |
117
|
|
|
$this->nodes[(int) $resource->getId()] = array_shift($nodes); |
118
|
|
|
return true; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return false; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get the resource type of the provider |
126
|
|
|
* |
127
|
|
|
* @return string |
128
|
|
|
* @since 16.0.0 |
129
|
|
|
*/ |
130
|
|
|
public function getType(): string { |
131
|
|
|
return self::RESOURCE_TYPE; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|