Passed
Push — master ( 4209dc...b3eb0b )
by Julius
16:39 queued 12s
created

ReferenceController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
dl 0
loc 4
rs 10
c 1
b 0
f 0
eloc 3
nc 1
nop 4
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * @copyright Copyright (c) 2022 Julius Härtl <[email protected]>
6
 *
7
 * @author Julius Härtl <[email protected]>
8
 *
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace OC\Core\Controller;
26
27
use OCP\AppFramework\Http\Response;
28
use OCP\Collaboration\Reference\IReferenceManager;
29
use OCP\AppFramework\Controller;
30
use OCP\AppFramework\Http;
31
use OCP\AppFramework\Http\DataDownloadResponse;
32
use OCP\AppFramework\Http\DataResponse;
33
use OCP\Files\AppData\IAppDataFactory;
34
use OCP\Files\NotFoundException;
35
use OCP\Files\NotPermittedException;
36
use OCP\IRequest;
37
38
class ReferenceController extends Controller {
39
	private IReferenceManager $referenceManager;
40
	private IAppDataFactory $appDataFactory;
41
42
	public function __construct(string $appName, IRequest $request, IReferenceManager $referenceManager, IAppDataFactory $appDataFactory) {
43
		parent::__construct($appName, $request);
44
		$this->referenceManager = $referenceManager;
45
		$this->appDataFactory = $appDataFactory;
46
	}
47
48
	/**
49
	 * @PublicPage
50
	 * @NoCSRFRequired
51
	 */
52
	public function preview(string $referenceId): Response {
53
		$reference = $this->referenceManager->getReferenceByCacheKey($referenceId);
54
		if ($reference === null) {
55
			return new DataResponse('', Http::STATUS_NOT_FOUND);
56
		}
57
58
		try {
59
			$appData = $this->appDataFactory->get('core');
60
			$folder = $appData->getFolder('opengraph');
61
			$file = $folder->getFile($referenceId);
62
			return new DataDownloadResponse($file->getContent(), $referenceId, $reference->getImageContentType());
63
		} catch (NotFoundException|NotPermittedException $e) {
64
			return new DataResponse('', Http::STATUS_NOT_FOUND);
65
		}
66
	}
67
}
68