|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2018, Roeland Jago Douma <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @author Roeland Jago Douma <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* @license GNU AGPL version 3 or any later version |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
12
|
|
|
* License, or (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU Affero General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace OCA\Richdocuments\Controller; |
|
25
|
|
|
|
|
26
|
|
|
use OCA\Richdocuments\Db\AssetMapper; |
|
27
|
|
|
use OCA\Richdocuments\Service\UserScopeService; |
|
28
|
|
|
use OCP\AppFramework\Controller; |
|
29
|
|
|
use OCP\AppFramework\Db\DoesNotExistException; |
|
30
|
|
|
use OCP\AppFramework\Http; |
|
31
|
|
|
use OCP\AppFramework\Http\DataResponse; |
|
32
|
|
|
use OCP\AppFramework\Http\FileDisplayResponse; |
|
33
|
|
|
use OCP\AppFramework\Http\JSONResponse; |
|
34
|
|
|
use OCP\Files\File; |
|
35
|
|
|
use OCP\Files\IRootFolder; |
|
36
|
|
|
use OCP\Files\NotFoundException; |
|
37
|
|
|
use OCP\IRequest; |
|
38
|
|
|
use OCP\IURLGenerator; |
|
39
|
|
|
|
|
40
|
|
|
class AssetsController extends Controller { |
|
41
|
|
|
|
|
42
|
|
|
/** @var AssetMapper */ |
|
43
|
|
|
private $assetMapper; |
|
44
|
|
|
|
|
45
|
|
|
/** @var IRootFolder */ |
|
46
|
|
|
private $rootFolder; |
|
47
|
|
|
|
|
48
|
|
|
/** @var string */ |
|
49
|
|
|
private $userId; |
|
50
|
|
|
|
|
51
|
|
|
/** @var IURLGenerator */ |
|
52
|
|
|
private $urlGenerator; |
|
53
|
|
|
|
|
54
|
|
|
public function __construct($appName, |
|
55
|
|
|
IRequest $request, |
|
56
|
|
|
AssetMapper $assetMapper, |
|
57
|
|
|
IRootFolder $rootFolder, |
|
58
|
|
|
$userId, |
|
59
|
|
|
UserScopeService $userScopeService, |
|
60
|
|
|
IURLGenerator $urlGenerator) { |
|
61
|
|
|
parent::__construct($appName, $request); |
|
62
|
|
|
|
|
63
|
|
|
$this->assetMapper = $assetMapper; |
|
64
|
|
|
$this->rootFolder = $rootFolder; |
|
65
|
|
|
$this->userId = $userId; |
|
66
|
|
|
$this->userScopeService = $userScopeService; |
|
|
|
|
|
|
67
|
|
|
$this->urlGenerator = $urlGenerator; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @NoAdminRequired |
|
72
|
|
|
* @NoCSRFRequired |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $path |
|
75
|
|
|
* @return JSONResponse |
|
76
|
|
|
*/ |
|
77
|
|
|
public function create($path) { |
|
78
|
|
|
$userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
79
|
|
|
|
|
80
|
|
|
try { |
|
81
|
|
|
$node = $userFolder->get($path); |
|
82
|
|
|
} catch (NotFoundException $e) { |
|
83
|
|
|
return new JSONResponse([], Http::STATUS_NOT_FOUND); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$asset = $this->assetMapper->newAsset($this->userId, $node->getId()); |
|
87
|
|
|
|
|
88
|
|
|
return new JSONResponse([ |
|
89
|
|
|
'url' => $this->urlGenerator->linkToRouteAbsolute('richdocuments.assets.get', [ |
|
90
|
|
|
'token' => $asset->getToken(), |
|
91
|
|
|
]) |
|
92
|
|
|
]); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @PublicPage |
|
97
|
|
|
* @NoCSRFRequired |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $token |
|
100
|
|
|
* @return Http\Response |
|
101
|
|
|
*/ |
|
102
|
|
|
public function get($token) { |
|
103
|
|
|
try { |
|
104
|
|
|
$asset = $this->assetMapper->getAssetByToken($token); |
|
105
|
|
|
} catch (DoesNotExistException $e) { |
|
106
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
// Do not clear the asset on HEAD requests |
|
110
|
|
|
if ($this->request->getMethod() === 'GET') { |
|
111
|
|
|
// Clear the assets so we can only fetch it once |
|
112
|
|
|
$this->assetMapper->delete($asset); |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
$this->userScopeService->setUserScope($asset->getUid()); |
|
117
|
|
|
$userFolder = $this->rootFolder->getUserFolder($asset->getUid()); |
|
118
|
|
|
$nodes = $userFolder->getById($asset->getFileid()); |
|
119
|
|
|
|
|
120
|
|
|
if ($nodes === []) { |
|
121
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$node = array_pop($nodes); |
|
125
|
|
|
if (!($node instanceof File)) { |
|
126
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return new FileDisplayResponse($node, Http::STATUS_OK, [ |
|
130
|
|
|
'Content-Type' => $node->getMimeType() |
|
131
|
|
|
]); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: