Completed
Push — master ( 0233b7...8a5b0a )
by Thomas
42s
created

Avatar::buildAvatarPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Arthur Schiwon <[email protected]>
4
 * @author Christopher Schäpers <[email protected]>
5
 * @author Lukas Reschke <[email protected]>
6
 * @author Morris Jobke <[email protected]>
7
 * @author Olivier Mehani <[email protected]>
8
 * @author Robin Appelman <[email protected]>
9
 * @author Roeland Jago Douma <[email protected]>
10
 * @author Thomas Müller <[email protected]>
11
 *
12
 * @copyright Copyright (c) 2018, ownCloud GmbH
13
 * @license AGPL-3.0
14
 *
15
 * This code is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License, version 3,
17
 * as published by the Free Software Foundation.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License, version 3,
25
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
26
 *
27
 */
28
29
namespace OC;
30
31
use OC\User\User;
32
use OCP\Files\Folder;
33
use OCP\Files\File;
34
use OCP\Files\NotFoundException;
35
use OCP\Files\NotPermittedException;
36
use OCP\IAvatar;
37
use OCP\IImage;
38
use OCP\IL10N;
39
use OC_Image;
40
use OCP\ILogger;
41
42
/**
43
 * This class gets and sets users avatars.
44
 */
45
46
class Avatar implements IAvatar {
47
	/** @var Folder */
48
	private $folder;
49
	/** @var IL10N */
50
	private $l;
51
	/** @var User */
52
	private $user;
53
	/** @var ILogger  */
54
	private $logger;
55
56
	/**
57
	 * constructor
58
	 *
59
	 * @param Folder $folder The folder where the avatars are
60
	 * @param IL10N $l
61
	 * @param User $user
62
	 * @param ILogger $logger
63
	 */
64
	public function __construct(Folder $folder, IL10N $l, $user, ILogger $logger) {
65
		$this->folder = $folder;
66
		$this->l = $l;
67
		$this->user = $user;
68
		$this->logger = $logger;
69
	}
70
71
	/**
72
	 * @inheritdoc
73
	 */
74
	public function get($size = 64) {
75
		try {
76
			$file = $this->getFile($size);
77
		} catch (NotFoundException $e) {
78
			return false;
79
		}
80
81
		$avatar = new OC_Image();
82
		$avatar->loadFromData($file->getContent());
83
		return $avatar;
84
	}
85
86
	/**
87
	 * Check if an avatar exists for the user
88
	 *
89
	 * @return bool
90
	 */
91
	public function exists() {
92
		return $this->folder->nodeExists('avatar.jpg') || $this->folder->nodeExists('avatar.png');
93
	}
94
95
	/**
96
	 * sets the users avatar
97
	 * @param IImage|resource|string $data An image object, imagedata or path to set a new avatar
98
	 * @throws \Exception if the provided file is not a jpg or png image
99
	 * @throws \Exception if the provided image is not valid
100
	 * @throws NotSquareException if the image is not square
101
	 * @return void
102
	*/
103
	public function set($data) {
104
		if ($data instanceof IImage) {
105
			$img = $data;
106
			$data = $img->data();
107
		} else {
108
			$img = new OC_Image($data);
109
		}
110
		$type = \substr($img->mimeType(), -3);
111
		if ($type === 'peg') {
112
			$type = 'jpg';
113
		}
114
		if ($type !== 'jpg' && $type !== 'png') {
115
			throw new \Exception($this->l->t("Unknown filetype"));
116
		}
117
118
		if (!$img->valid()) {
119
			throw new \Exception($this->l->t("Invalid image"));
120
		}
121
122
		if (!($img->height() === $img->width())) {
123
			throw new NotSquareException($this->l->t("Avatar image is not square"));
124
		}
125
126
		$this->remove();
127
		$this->folder->newFile('avatar.'.$type)->putContent($data);
0 ignored issues
show
Bug introduced by
It seems like $data defined by parameter $data on line 103 can also be of type resource; however, OCP\Files\File::putContent() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
128
		$this->user->triggerChange('avatar');
129
	}
130
131
	/**
132
	 * remove the users avatar
133
	 * @return void
134
	*/
135
	public function remove() {
136
		$regex = '/^avatar\.([0-9]+\.)?(jpg|png)$/';
137
		$avatars = $this->folder->getDirectoryListing();
138
139
		foreach ($avatars as $avatar) {
140
			if (\preg_match($regex, $avatar->getName())) {
141
				$avatar->delete();
142
			}
143
		}
144
		$this->user->triggerChange('avatar');
145
	}
146
147
	/**
148
	 * @inheritdoc
149
	 */
150
	public function getFile($size) {
151
		$ext = $this->getExtension();
152
153
		if ($size === -1) {
154
			$path = 'avatar.' . $ext;
155
		} else {
156
			$path = 'avatar.' . $size . '.' . $ext;
157
		}
158
159
		try {
160
			$file = $this->folder->get($path);
161
		} catch (NotFoundException $e) {
162
			if ($size <= 0) {
163
				throw new NotFoundException;
164
			}
165
166
			$avatar = new OC_Image();
167
			/** @var File $file */
168
			$file = $this->folder->get('avatar.' . $ext);
169
			$avatar->loadFromData($file->getContent());
170
			if ($size !== -1) {
171
				$avatar->resize($size);
172
			}
173
			try {
174
				$file = $this->folder->newFile($path);
175
				$file->putContent($avatar->data());
176
			} catch (NotPermittedException $e) {
177
				$this->logger->error('Failed to save avatar for ' . $this->user->getUID());
178
			}
179
		}
180
181
		return $file;
182
	}
183
184
	/**
185
	 * Get the extension of the avatar. If there is no avatar throw Exception
186
	 *
187
	 * @return string
188
	 * @throws NotFoundException
189
	 */
190
	private function getExtension() {
191
		if ($this->folder->nodeExists('avatar.jpg')) {
192
			return 'jpg';
193
		} elseif ($this->folder->nodeExists('avatar.png')) {
194
			return 'png';
195
		}
196
		throw new NotFoundException;
197
	}
198
}
199