Completed
Push — stable8.2 ( 09e830...ae9bfd )
by Olivier
12:09
created

ImageResponse::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * ownCloud - galleryplus
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Olivier Paroz <[email protected]>
9
 *
10
 * @copyright Olivier Paroz 2014-2015
11
 */
12
13
namespace OCA\GalleryPlus\Http;
14
15
use OCP\AppFramework\Http\Response;
16
use OCP\AppFramework\Http;
17
18
/**
19
 * A renderer for images
20
 *
21
 * @package OCA\GalleryPlus\Http
22
 */
23
class ImageResponse extends Response {
24
25
	/**
26
	 * @var \OC_Image|string
27
	 */
28
	private $preview;
29
30
	/**
31
	 * Constructor
32
	 *
33
	 * @param array $image image meta data
34
	 * @param int $statusCode the HTTP status code, defaults to 200
35
	 */
36 18
	public function __construct(array $image, $statusCode = Http::STATUS_OK) {
37 18
		$name = $image['name'];
38 18
		$this->preview = $image['preview'];
39
40 18
		$this->setStatus($statusCode);
41 18
		$this->addHeader('Content-type', $image['mimetype'] . '; charset=utf-8');
42 18
		$this->addHeader('Content-Disposition',
43 18
						 'attachment; filename*=UTF-8\'\'' . rawurlencode($name) . '; filename="'
44 18
						 . rawurlencode($name) . '"'
45
		);
46 18
	}
47
48
	/**
49
	 * Returns the rendered image
50
	 *
51
	 * @return string the file
52
	 */
53 13
	public function render() {
54 13
		if ($this->preview instanceof \OC_Image) {
55
			// Uses imagepng() to output the image
56 3
			return $this->preview->data();
57
		} else {
58 10
			return $this->preview;
59
		}
60
	}
61
62
}
63