diff options
Diffstat (limited to 'app/Controller')
-rw-r--r-- | app/Controller/AvatarFile.php | 55 | ||||
-rw-r--r-- | app/Controller/User.php | 37 |
2 files changed, 92 insertions, 0 deletions
diff --git a/app/Controller/AvatarFile.php b/app/Controller/AvatarFile.php new file mode 100644 index 00000000..f8298e16 --- /dev/null +++ b/app/Controller/AvatarFile.php @@ -0,0 +1,55 @@ +<?php + +namespace Kanboard\Controller; + +use Kanboard\Core\ObjectStorage\ObjectStorageException; +use Kanboard\Core\Thumbnail; + +/** + * Avatar File Controller + * + * @package controller + * @author Frederic Guillot + */ +class AvatarFile extends Base +{ + /** + * Show Avatar image and send aggressive caching headers + */ + public function show() + { + $user_id = $this->request->getIntegerParam('user_id'); + $size = $this->request->getStringParam('size', 48); + $filename = $this->avatarFile->getFilename($user_id); + $etag = md5($filename.$size); + + $this->response->cache(365 * 86400, $etag); + $this->response->contentType('image/jpeg'); + + if ($this->request->getHeader('If-None-Match') !== '"'.$etag.'"') { + $this->render($filename, $size); + } else { + $this->response->status(304); + } + } + + /** + * Render thumbnail from object storage + * + * @access private + * @param string $filename + * @param integer $size + */ + private function render($filename, $size) + { + try { + $blob = $this->objectStorage->get($filename); + + Thumbnail::createFromString($blob) + ->resize($size, $size) + ->toOutput(); + } catch (ObjectStorageException $e) { + $this->logger->error($e->getMessage()); + } + } +} diff --git a/app/Controller/User.php b/app/Controller/User.php index f7d7d2e0..8c02ef7f 100644 --- a/app/Controller/User.php +++ b/app/Controller/User.php @@ -405,4 +405,41 @@ class User extends Base 'user' => $user, ))); } + + /** + * Display avatar page + */ + public function avatar() + { + $user = $this->getUser(); + + $this->response->html($this->helper->layout->user('user/avatar', array( + 'user' => $user, + ))); + } + + /** + * Upload Avatar + */ + public function uploadAvatar() + { + $user = $this->getUser(); + + if (! $this->avatarFile->uploadFile($user['id'], $this->request->getFileInfo('avatar'))) { + $this->flash->failure(t('Unable to upload the file.')); + } + + $this->response->redirect($this->helper->url->to('user', 'avatar', array('user_id' => $user['id']))); + } + + /** + * Remove Avatar image + */ + public function removeAvatar() + { + $this->checkCSRFParam(); + $user = $this->getUser(); + $this->avatarFile->remove($user['id']); + $this->response->redirect($this->helper->url->to('user', 'avatar', array('user_id' => $user['id']))); + } } |