Completed
Pull Request — master (#195)
by John
02:42
created

DisplayController::showPdfViewer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 14
cp 0
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright 2014 Lukas Reschke [email protected]
7
 *
8
 * @author Lukas Reschke <[email protected]>
9
 * @author John Molakvoæ <[email protected]>
10
 *
11
 * @license GNU AGPL version 3 or any later version
12
 *
13
 * This program is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License as
15
 * published by the Free Software Foundation, either version 3 of the
16
 * License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License
24
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25
 *
26
 */
27
28
namespace OCA\Files_PDFViewer\Controller;
29
30
use OCA\Files_PDFViewer\AppInfo\Application;
31
use OCP\AppFramework\Controller;
32
use OCP\AppFramework\Http\ContentSecurityPolicy;
33
use OCP\AppFramework\Http\StreamResponse;
34
use OCP\AppFramework\Http\TemplateResponse;
35
use OCP\IRequest;
36
use OCP\IURLGenerator;
37
38
class DisplayController extends Controller {
39
40
	/** @var IURLGenerator */
41
	private $urlGenerator;
42
43
	/**
44
	 * @param IRequest $request
45
	 * @param IURLGenerator $urlGenerator
46
	 */
47
	public function __construct(IRequest $request,
48
								IURLGenerator $urlGenerator) {
49
		parent::__construct(Application::APP_ID, $request);
50
		$this->urlGenerator = $urlGenerator;
51
	}
52
53
	/**
54
	 * @PublicPage
55
	 * @NoCSRFRequired
56
	 *
57
	 * @param bool $minmode
58
	 * @return TemplateResponse
59
	 */
60
	public function showPdfViewer(bool $minmode = false): TemplateResponse {
61
		$params = [
62
			'urlGenerator' => $this->urlGenerator,
63
			'minmode' => $minmode
64
		];
65
		$response = new TemplateResponse(Application::APP_ID, 'viewer', $params, 'blank');
66
67
		$policy = new ContentSecurityPolicy();
68
		$policy->addAllowedChildSrcDomain('\'self\'');
69
		$policy->addAllowedFontDomain('data:');
70
		$policy->addAllowedImageDomain('*');
71
		$policy->allowEvalScript(false);
72
		$response->setContentSecurityPolicy($policy);
73
74
		return $response;
75
	}
76
77
	/**
78
	 * @NoAdminRequired
79
	 * @NoCSRFRequired
80
	 *
81
	 * @return StreamResponse
82
	 */
83
	public function serviceWorker(): StreamResponse {
84
		$response = new StreamResponse(__DIR__.'/../../js/files_pdfviewer-worker.js');
85
		$response->setHeaders(['Content-Type' => 'application/javascript']);
86
87
		$policy = new ContentSecurityPolicy();
88
		$policy->addAllowedWorkerSrcDomain("'self'");
89
		$policy->addAllowedScriptDomain("'self'");
90
		$policy->addAllowedConnectDomain("'self'");
91
		$response->setContentSecurityPolicy($policy);
92
93
		return $response;
94
	}
95
}
96