summaryrefslogtreecommitdiff
path: root/app/Model
diff options
context:
space:
mode:
Diffstat (limited to 'app/Model')
-rw-r--r--app/Model/AvatarFile.php111
-rw-r--r--app/Model/Comment.php3
-rw-r--r--app/Model/File.php19
-rw-r--r--app/Model/ProjectActivity.php6
-rw-r--r--app/Model/TaskFinder.php1
-rw-r--r--app/Model/User.php10
6 files changed, 133 insertions, 17 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())));
+ }
+}
diff --git a/app/Model/Comment.php b/app/Model/Comment.php
index 6eb4a1e5..f7ac4eaa 100644
--- a/app/Model/Comment.php
+++ b/app/Model/Comment.php
@@ -48,7 +48,8 @@ class Comment extends Base
self::TABLE.'.comment',
User::TABLE.'.username',
User::TABLE.'.name',
- User::TABLE.'.email'
+ User::TABLE.'.email',
+ User::TABLE.'.avatar_path'
)
->join(User::TABLE, 'id', 'user_id')
->orderBy(self::TABLE.'.date_creation', $sorting)
diff --git a/app/Model/File.php b/app/Model/File.php
index 03ea691d..5e77060c 100644
--- a/app/Model/File.php
+++ b/app/Model/File.php
@@ -3,6 +3,7 @@
namespace Kanboard\Model;
use Exception;
+use Kanboard\Core\Thumbnail;
use Kanboard\Event\FileEvent;
use Kanboard\Core\Tool;
use Kanboard\Core\ObjectStorage\ObjectStorageException;
@@ -315,15 +316,15 @@ abstract class File extends Base
*/
public function generateThumbnailFromData($destination_filename, &$data)
{
- $temp_filename = tempnam(sys_get_temp_dir(), 'datafile');
+ $blob = Thumbnail::createFromString($data)
+ ->resize()
+ ->toString();
- file_put_contents($temp_filename, $data);
- $this->generateThumbnailFromFile($temp_filename, $destination_filename);
- unlink($temp_filename);
+ $this->objectStorage->put($this->getThumbnailPath($destination_filename), $blob);
}
/**
- * Generate thumbnail from a blob
+ * Generate thumbnail from a local file
*
* @access public
* @param string $uploaded_filename
@@ -331,8 +332,10 @@ abstract class File extends Base
*/
public function generateThumbnailFromFile($uploaded_filename, $destination_filename)
{
- $thumbnail_filename = tempnam(sys_get_temp_dir(), 'thumbnail');
- Tool::generateThumbnail($uploaded_filename, $thumbnail_filename);
- $this->objectStorage->moveFile($thumbnail_filename, $this->getThumbnailPath($destination_filename));
+ $blob = Thumbnail::createFromFile($uploaded_filename)
+ ->resize()
+ ->toString();
+
+ $this->objectStorage->put($this->getThumbnailPath($destination_filename), $blob);
}
}
diff --git a/app/Model/ProjectActivity.php b/app/Model/ProjectActivity.php
index 74df26a1..d399d5c6 100644
--- a/app/Model/ProjectActivity.php
+++ b/app/Model/ProjectActivity.php
@@ -88,7 +88,8 @@ class ProjectActivity extends Base
self::TABLE.'.*',
User::TABLE.'.username AS author_username',
User::TABLE.'.name AS author_name',
- User::TABLE.'.email'
+ User::TABLE.'.email',
+ User::TABLE.'.avatar_path'
)
->in('project_id', $project_ids)
->join(User::TABLE, 'id', 'creator_id')
@@ -117,7 +118,8 @@ class ProjectActivity extends Base
self::TABLE.'.*',
User::TABLE.'.username AS author_username',
User::TABLE.'.name AS author_name',
- User::TABLE.'.email'
+ User::TABLE.'.email',
+ User::TABLE.'.avatar_path'
)
->eq('task_id', $task_id)
->join(User::TABLE, 'id', 'creator_id')
diff --git a/app/Model/TaskFinder.php b/app/Model/TaskFinder.php
index d67372cc..7bca2284 100644
--- a/app/Model/TaskFinder.php
+++ b/app/Model/TaskFinder.php
@@ -128,6 +128,7 @@ class TaskFinder extends Base
User::TABLE.'.username AS assignee_username',
User::TABLE.'.name AS assignee_name',
User::TABLE.'.email AS assignee_email',
+ User::TABLE.'.avatar_path AS assignee_avatar_path',
Category::TABLE.'.name AS category_name',
Category::TABLE.'.description AS category_description',
Column::TABLE.'.title AS column_name',
diff --git a/app/Model/User.php b/app/Model/User.php
index 0e11422b..b093d55f 100644
--- a/app/Model/User.php
+++ b/app/Model/User.php
@@ -283,12 +283,7 @@ class User extends Base
{
$this->prepare($values);
$result = $this->db->table(self::TABLE)->eq('id', $values['id'])->update($values);
-
- // If the user is connected refresh his session
- if ($this->userSession->getId() == $values['id']) {
- $this->userSession->initialize($this->getById($this->userSession->getId()));
- }
-
+ $this->userSession->refresh($values['id']);
return $result;
}
@@ -327,6 +322,9 @@ class User extends Base
{
return $this->db->transaction(function (Database $db) use ($user_id) {
+ // Remove Avatar
+ $this->avatarFile->remove($user_id);
+
// All assigned tasks are now unassigned (no foreign key)
if (! $db->table(Task::TABLE)->eq('owner_id', $user_id)->update(array('owner_id' => 0))) {
return false;