Completed
Push — master ( 626d03...c1030a )
by Morris
27:45 queued 15:55
created

AvatarNode::getContentType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Thomas Müller <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2016, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
23
namespace OCA\DAV\Avatars;
24
25
26
use OCP\IAvatar;
27
use Sabre\DAV\File;
28
29
class AvatarNode extends File {
30
	private $ext;
31
	private $size;
32
	private $avatar;
33
34
	/**
35
	 * AvatarNode constructor.
36
	 *
37
	 * @param integer $size
38
	 * @param string $ext
39
	 * @param IAvatar $avatar
40
	 */
41
	public function __construct($size, $ext, $avatar) {
42
		$this->size = $size;
43
		$this->ext = $ext;
44
		$this->avatar = $avatar;
45
	}
46
47
	/**
48
	 * Returns the name of the node.
49
	 *
50
	 * This is used to generate the url.
51
	 *
52
	 * @return string
53
	 */
54
	public function getName() {
55
		return "$this->size.$this->ext";
56
	}
57
58
	public function get() {
59
		$image = $this->avatar->get($this->size);
60
		$res = $image->resource();
61
62
		ob_start();
63
		if ($this->ext === 'png') {
64
			imagepng($res);
65
		} else {
66
			imagejpeg($res);
67
		}
68
69
		return ob_get_clean();
70
	}
71
72
	/**
73
	 * Returns the mime-type for a file
74
	 *
75
	 * If null is returned, we'll assume application/octet-stream
76
	 *
77
	 * @return string|null
78
	 */
79
	public function getContentType() {
80
		if ($this->ext === 'png') {
81
			return 'image/png';
82
		}
83
		return 'image/jpeg';
84
	}
85
86
	public function getETag() {
87
		return $this->avatar->getFile($this->size)->getEtag();
88
	}
89
90
	public function getLastModified() {
91
		$timestamp = $this->avatar->getFile($this->size)->getMTime();
92
		if (!empty($timestamp)) {
93
			return (int)$timestamp;
94
		}
95
		return $timestamp;
96
97
	}
98
}
99