Added avatar image upload
This commit is contained in:
111
app/Model/AvatarFile.php
Normal file
111
app/Model/AvatarFile.php
Normal file
@@ -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())));
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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')
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user