From ab48a09f0d674b703467975b376c5ac7352670ae Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sat, 28 May 2016 11:31:54 -0400 Subject: Rename controllers --- app/Controller/AvatarFileController.php | 94 +++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 app/Controller/AvatarFileController.php (limited to 'app/Controller/AvatarFileController.php') diff --git a/app/Controller/AvatarFileController.php b/app/Controller/AvatarFileController.php new file mode 100644 index 00000000..0e2ed1bb --- /dev/null +++ b/app/Controller/AvatarFileController.php @@ -0,0 +1,94 @@ +getUser(); + + $this->response->html($this->helper->layout->user('avatar_file/show', array( + 'user' => $user, + ))); + } + + /** + * Upload Avatar + */ + public function upload() + { + $user = $this->getUser(); + + if (! $this->avatarFile->uploadImageFile($user['id'], $this->request->getFileInfo('avatar'))) { + $this->flash->failure(t('Unable to upload the file.')); + } + + $this->response->redirect($this->helper->url->to('AvatarFileController', 'show', array('user_id' => $user['id']))); + } + + /** + * Remove Avatar image + */ + public function remove() + { + $this->checkCSRFParam(); + $user = $this->getUser(); + $this->avatarFile->remove($user['id']); + $this->userSession->refresh($user['id']); + $this->response->redirect($this->helper->url->to('AvatarFileController', 'show', array('user_id' => $user['id']))); + } + + /** + * Show Avatar image (public) + */ + public function image() + { + $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->withCache(365 * 86400, $etag); + $this->response->withContentType('image/jpeg'); + + if ($this->request->getHeader('If-None-Match') !== '"'.$etag.'"') { + $this->response->send(); + $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()); + } + } +} -- cgit v1.2.3