-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathTargetController.php
More file actions
79 lines (69 loc) · 1.92 KB
/
TargetController.php
File metadata and controls
79 lines (69 loc) · 1.92 KB
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
<?php
/**
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Richdocuments\Controller;
use OC\User\NoUserException;
use OCA\Richdocuments\Service\FileTargetService;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataDisplayResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\Response;
use OCP\Files\File;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IRequest;
class TargetController extends \OCP\AppFramework\OCSController {
public function __construct(
string $appName,
IRequest $request,
private FileTargetService $fileTargetService,
private IRootFolder $rootFolder,
private ?string $userId,
) {
parent::__construct($appName, $request);
}
/**
* @NoAdminRequired
*/
public function getTargets(string $path): DataResponse {
try {
$file = $this->getFile($path);
return new DataResponse($this->fileTargetService->getFileTargets($file));
} catch (NotFoundException) {
}
return new DataResponse('File not found', Http::STATUS_NOT_FOUND);
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function getPreview(string $path, string $target): Response {
try {
$file = $this->getFile($path);
return new DataDisplayResponse(
$this->fileTargetService->getTargetPreview($file, $target),
Http::STATUS_OK,
['Content-Type' => 'image/png']
);
} catch (NotFoundException) {
return new DataResponse('File not found', Http::STATUS_NOT_FOUND);
}
}
/**
* @throws NotFoundException
*/
private function getFile(string $path): File {
try {
$file = $this->rootFolder->getUserFolder($this->userId)->get($path);
if (!$file instanceof File) {
throw new NotFoundException();
}
return $file;
} catch (NotFoundException|NotPermittedException|NoUserException) {
throw new NotFoundException();
}
}
}