Completed
Push — master ( 8bf574...c0bbae )
by Lukas
28:01 queued 15:22
created

PreviewController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 6
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, 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 OC\Core\Controller;
25
26
use OCP\AppFramework\Controller;
27
use OCP\AppFramework\Utility\ITimeFactory;
28
use OCP\Files\File;
29
use OCP\AppFramework\Http;
30
use OCP\AppFramework\Http\DataResponse;
31
use OCP\AppFramework\Http\FileDisplayResponse;
32
use OCP\Files\IRootFolder;
33
use OCP\Files\NotFoundException;
34
use OCP\IPreview;
35
use OCP\IRequest;
36
37
class PreviewController extends Controller {
38
39
	/** @var string */
40
	private $userId;
41
42
	/** @var IRootFolder */
43
	private $root;
44
45
	/** @var IPreview */
46
	private $preview;
47
48
	/** @var ITimeFactory */
49
	private $timeFactory;
50
51
	/**
52
	 * PreviewController constructor.
53
	 *
54
	 * @param string $appName
55
	 * @param IRequest $request
56
	 * @param IPreview $preview
57
	 * @param IRootFolder $root
58
	 * @param string $userId
59
	 */
60
	public function __construct($appName,
61
								IRequest $request,
62
								IPreview $preview,
63
								IRootFolder $root,
64
								$userId,
65
								ITimeFactory $timeFactory
66
	) {
67
		parent::__construct($appName, $request);
68
69
		$this->preview = $preview;
70
		$this->root = $root;
71
		$this->userId = $userId;
72
		$this->timeFactory = $timeFactory;
73
	}
74
75
	/**
76
	 * @NoAdminRequired
77
	 * @NoCSRFRequired
78
	 *
79
	 * @param string $file
80
	 * @param int $x
81
	 * @param int $y
82
	 * @param bool $a
83
	 * @param bool $forceIcon
84
	 * @param string $mode
85
	 * @return DataResponse|Http\FileDisplayResponse
86
	 */
87
	public function getPreview(
88
		$file = '',
89
		$x = 32,
90
		$y = 32,
91
		$a = false,
92
		$forceIcon = true,
93
		$mode = 'fill') {
94
95 View Code Duplication
		if ($file === '' || $x === 0 || $y === 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
			return new DataResponse([], Http::STATUS_BAD_REQUEST);
97
		}
98
99
		try {
100
			$userFolder = $this->root->getUserFolder($this->userId);
101
			$file = $userFolder->get($file);
102
		} catch (NotFoundException $e) {
103
			return new DataResponse([], Http::STATUS_NOT_FOUND);
104
		}
105
106
		if (!($file instanceof File) || (!$forceIcon && !$this->preview->isAvailable($file))) {
107
			return new DataResponse([], Http::STATUS_NOT_FOUND);
108
		} else if (!$file->isReadable()) {
109
			return new DataResponse([], Http::STATUS_FORBIDDEN);
110
		}
111
112
		try {
113
			$f = $this->preview->getPreview($file, $x, $y, !$a, $mode);
114
			$response = new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
115
116
			// Let cache this!
117
			$response->addHeader('Pragma', 'public');
118
119
			// Cache previews for 24H
120
			$response->cacheFor(3600 * 24);
121
			$expires = new \DateTime();
122
			$expires->setTimestamp($this->timeFactory->getTime());
123
			$expires->add(new \DateInterval('P1D'));
124
			$response->addHeader('Expires', $expires->format(\DateTime::RFC2822));
125
126
			return $response;
127
		} catch (NotFoundException $e) {
128
			return new DataResponse([], Http::STATUS_NOT_FOUND);
129
		}
130
131
	}
132
}
133