Completed
Push — master ( 06fa48...914bb2 )
by Lukas
24s
created

FileDisplayResponse::callback()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 6
rs 9.4285
1
<?php
2
/**
3
 * @copyright 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
namespace OCP\AppFramework\Http;
24
25
use OCP\AppFramework\Http;
26
use OCP\Files\File;
27
28
/**
29
 * Class FileDisplayResponse
30
 *
31
 * @package OCP\AppFramework\Http
32
 * @since 9.2.0
33
 */
34
class FileDisplayResponse extends Response implements ICallbackResponse {
35
36
	/** @var File */
37
	private $file;
38
39
	/**
40
	 * FileDisplayResponse constructor.
41
	 *
42
	 * @param File $file
43
	 * @param int $statusCode
44
	 * @param array $headers
45
	 * @since 9.2.0
46
	 */
47
	public function __construct(File $file, $statusCode=Http::STATUS_OK,
48
								$headers=[]) {
49
		$this->file = $file;
50
		$this->setStatus($statusCode);
51
		$this->setHeaders(array_merge($this->getHeaders(), $headers));
52
		$this->addHeader('Content-Disposition', 'inline; filename="' . rawurldecode($file->getName()) . '"');
53
54
		$this->setETag($file->getEtag());
55
		$lastModified = new \DateTime();
56
		$lastModified->setTimestamp($file->getMTime());
57
		$this->setLastModified($lastModified);
58
	}
59
60
	/**
61
	 * @param IOutput $output
62
	 * @since 9.2.0
63
	 */
64
	public function callback(IOutput $output) {
65
		if ($output->getHttpResponseCode() !== Http::STATUS_NOT_MODIFIED) {
66
			$output->setHeader('Content-Length: ' . $this->file->getSize());
67
			$output->setOutput($this->file->getContent());
68
		}
69
	}
70
}
71