diff options
author | Frederic Guillot <fred@kanboard.net> | 2016-03-26 14:43:41 -0400 |
---|---|---|
committer | Frederic Guillot <fred@kanboard.net> | 2016-03-26 14:43:41 -0400 |
commit | 820c929ab38273c80d0930e2e6140dd7676ba4df (patch) | |
tree | 3944047e41d14077f7e5e8c0d50856b360a22567 /app/Model/AvatarFile.php | |
parent | e71f37238c82f01cd02d6d7343b42328d9603d14 (diff) |
Added avatar image upload
Diffstat (limited to 'app/Model/AvatarFile.php')
-rw-r--r-- | app/Model/AvatarFile.php | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/app/Model/AvatarFile.php b/app/Model/AvatarFile.php new file mode 100644 index 00000000..52d07962 --- /dev/null +++ b/app/Model/AvatarFile.php @@ -0,0 +1,111 @@ +<?php + +namespace Kanboard\Model; + +use Exception; + +/** + * Avatar File + * + * @package model + * @author Frederic Guillot + */ +class AvatarFile extends Base +{ + /** + * Path prefix + * + * @var string + */ + const PATH_PREFIX = 'avatars'; + + /** + * Get image filename + * + * @access public + * @param integer $user_id + * @return string + */ + public function getFilename($user_id) + { + return $this->db->table(User::TABLE)->eq('id', $user_id)->findOneColumn('avatar_path'); + } + + /** + * Add avatar in the user profile + * + * @access public + * @param integer $user_id Foreign key + * @param string $path Path on the disk + * @return bool + */ + public function create($user_id, $path) + { + $result = $this->db->table(User::TABLE)->eq('id', $user_id)->update(array( + 'avatar_path' => $path, + )); + + $this->userSession->refresh($user_id); + + return $result; + } + + /** + * Remove avatar from the user profile + * + * @access public + * @param integer $user_id Foreign key + * @return bool + */ + public function remove($user_id) + { + try { + $this->objectStorage->remove($this->getFilename($user_id)); + $result = $this->db->table(User::TABLE)->eq('id', $user_id)->update(array('avatar_path' => '')); + $this->userSession->refresh($user_id); + return $result; + } catch (Exception $e) { + $this->logger->error($e->getMessage()); + return false; + } + } + + /** + * Upload avatar image + * + * @access public + * @param integer $user_id + * @param array $file + */ + public function uploadFile($user_id, array $file) + { + try { + if ($file['error'] == UPLOAD_ERR_OK && $file['size'] > 0) { + $destination_filename = $this->generatePath($user_id, $file['name']); + $this->objectStorage->moveUploadedFile($file['tmp_name'], $destination_filename); + $this->create($user_id, $destination_filename); + } else { + throw new Exception('File not uploaded: '.var_export($file['error'], true)); + } + + } catch (Exception $e) { + $this->logger->error($e->getMessage()); + return false; + } + + return true; + } + + /** + * Generate the path for a new filename + * + * @access public + * @param integer $user_id + * @param string $filename + * @return string + */ + public function generatePath($user_id, $filename) + { + return implode(DIRECTORY_SEPARATOR, array(self::PATH_PREFIX, $user_id, hash('sha1', $filename.time()))); + } +} |