Improve base FileModel class
This commit is contained in:
@@ -16,6 +16,42 @@ use Kanboard\Core\ObjectStorage\ObjectStorageException;
|
|||||||
*/
|
*/
|
||||||
abstract class FileModel extends Base
|
abstract class FileModel extends Base
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Get the table
|
||||||
|
*
|
||||||
|
* @abstract
|
||||||
|
* @access protected
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
abstract protected function getTable();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define the foreign key
|
||||||
|
*
|
||||||
|
* @abstract
|
||||||
|
* @access protected
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
abstract protected function getForeignKey();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the path prefix
|
||||||
|
*
|
||||||
|
* @abstract
|
||||||
|
* @access protected
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
abstract protected function getPathPrefix();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get event name
|
||||||
|
*
|
||||||
|
* @abstract
|
||||||
|
* @access protected
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
abstract protected function getEventName();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get PicoDb query to get all files
|
* Get PicoDb query to get all files
|
||||||
*
|
*
|
||||||
@@ -25,21 +61,21 @@ abstract class FileModel extends Base
|
|||||||
protected function getQuery()
|
protected function getQuery()
|
||||||
{
|
{
|
||||||
return $this->db
|
return $this->db
|
||||||
->table(static::TABLE)
|
->table($this->getTable())
|
||||||
->columns(
|
->columns(
|
||||||
static::TABLE.'.id',
|
$this->getTable().'.id',
|
||||||
static::TABLE.'.name',
|
$this->getTable().'.name',
|
||||||
static::TABLE.'.path',
|
$this->getTable().'.path',
|
||||||
static::TABLE.'.is_image',
|
$this->getTable().'.is_image',
|
||||||
static::TABLE.'.'.static::FOREIGN_KEY,
|
$this->getTable().'.'.$this->getForeignKey(),
|
||||||
static::TABLE.'.date',
|
$this->getTable().'.date',
|
||||||
static::TABLE.'.user_id',
|
$this->getTable().'.user_id',
|
||||||
static::TABLE.'.size',
|
$this->getTable().'.size',
|
||||||
UserModel::TABLE.'.username',
|
UserModel::TABLE.'.username',
|
||||||
UserModel::TABLE.'.name as user_name'
|
UserModel::TABLE.'.name as user_name'
|
||||||
)
|
)
|
||||||
->join(UserModel::TABLE, 'id', 'user_id')
|
->join(UserModel::TABLE, 'id', 'user_id')
|
||||||
->asc(static::TABLE.'.name');
|
->asc($this->getTable().'.name');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -51,7 +87,7 @@ abstract class FileModel extends Base
|
|||||||
*/
|
*/
|
||||||
public function getById($file_id)
|
public function getById($file_id)
|
||||||
{
|
{
|
||||||
return $this->db->table(static::TABLE)->eq('id', $file_id)->findOne();
|
return $this->db->table($this->getTable())->eq('id', $file_id)->findOne();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -63,7 +99,7 @@ abstract class FileModel extends Base
|
|||||||
*/
|
*/
|
||||||
public function getAll($id)
|
public function getAll($id)
|
||||||
{
|
{
|
||||||
return $this->getQuery()->eq(static::FOREIGN_KEY, $id)->findAll();
|
return $this->getQuery()->eq($this->getForeignKey(), $id)->findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -75,7 +111,7 @@ abstract class FileModel extends Base
|
|||||||
*/
|
*/
|
||||||
public function getAllImages($id)
|
public function getAllImages($id)
|
||||||
{
|
{
|
||||||
return $this->getQuery()->eq(static::FOREIGN_KEY, $id)->eq('is_image', 1)->findAll();
|
return $this->getQuery()->eq($this->getForeignKey(), $id)->eq('is_image', 1)->findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -87,7 +123,7 @@ abstract class FileModel extends Base
|
|||||||
*/
|
*/
|
||||||
public function getAllDocuments($id)
|
public function getAllDocuments($id)
|
||||||
{
|
{
|
||||||
return $this->getQuery()->eq(static::FOREIGN_KEY, $id)->eq('is_image', 0)->findAll();
|
return $this->getQuery()->eq($this->getForeignKey(), $id)->eq('is_image', 0)->findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -103,7 +139,7 @@ abstract class FileModel extends Base
|
|||||||
public function create($id, $name, $path, $size)
|
public function create($id, $name, $path, $size)
|
||||||
{
|
{
|
||||||
$values = array(
|
$values = array(
|
||||||
static::FOREIGN_KEY => $id,
|
$this->getForeignKey() => $id,
|
||||||
'name' => substr($name, 0, 255),
|
'name' => substr($name, 0, 255),
|
||||||
'path' => $path,
|
'path' => $path,
|
||||||
'is_image' => $this->isImage($name) ? 1 : 0,
|
'is_image' => $this->isImage($name) ? 1 : 0,
|
||||||
@@ -112,12 +148,12 @@ abstract class FileModel extends Base
|
|||||||
'date' => time(),
|
'date' => time(),
|
||||||
);
|
);
|
||||||
|
|
||||||
$result = $this->db->table(static::TABLE)->insert($values);
|
$result = $this->db->table($this->getTable())->insert($values);
|
||||||
|
|
||||||
if ($result) {
|
if ($result) {
|
||||||
$file_id = (int) $this->db->getLastId();
|
$file_id = (int) $this->db->getLastId();
|
||||||
$event = new FileEvent($values + array('file_id' => $file_id));
|
$event = new FileEvent($values + array('file_id' => $file_id));
|
||||||
$this->dispatcher->dispatch(static::EVENT_CREATE, $event);
|
$this->dispatcher->dispatch($this->getEventName(), $event);
|
||||||
return $file_id;
|
return $file_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,7 +169,7 @@ abstract class FileModel extends Base
|
|||||||
*/
|
*/
|
||||||
public function removeAll($id)
|
public function removeAll($id)
|
||||||
{
|
{
|
||||||
$file_ids = $this->db->table(static::TABLE)->eq(static::FOREIGN_KEY, $id)->asc('id')->findAllByColumn('id');
|
$file_ids = $this->db->table($this->getTable())->eq($this->getForeignKey(), $id)->asc('id')->findAllByColumn('id');
|
||||||
$results = array();
|
$results = array();
|
||||||
|
|
||||||
foreach ($file_ids as $file_id) {
|
foreach ($file_ids as $file_id) {
|
||||||
@@ -160,7 +196,7 @@ abstract class FileModel extends Base
|
|||||||
$this->objectStorage->remove($this->getThumbnailPath($file['path']));
|
$this->objectStorage->remove($this->getThumbnailPath($file['path']));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->db->table(static::TABLE)->eq('id', $file['id'])->remove();
|
return $this->db->table($this->getTable())->eq('id', $file['id'])->remove();
|
||||||
} catch (ObjectStorageException $e) {
|
} catch (ObjectStorageException $e) {
|
||||||
$this->logger->error($e->getMessage());
|
$this->logger->error($e->getMessage());
|
||||||
return false;
|
return false;
|
||||||
@@ -211,7 +247,7 @@ abstract class FileModel extends Base
|
|||||||
*/
|
*/
|
||||||
public function generatePath($id, $filename)
|
public function generatePath($id, $filename)
|
||||||
{
|
{
|
||||||
return static::PATH_PREFIX.DIRECTORY_SEPARATOR.$id.DIRECTORY_SEPARATOR.hash('sha1', $filename.time());
|
return $this->getPathPrefix().DIRECTORY_SEPARATOR.$id.DIRECTORY_SEPARATOR.hash('sha1', $filename.time());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -253,6 +289,7 @@ abstract class FileModel extends Base
|
|||||||
* @access public
|
* @access public
|
||||||
* @param integer $id
|
* @param integer $id
|
||||||
* @param array $file
|
* @param array $file
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function uploadFile($id, array $file)
|
public function uploadFile($id, array $file)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -11,30 +11,64 @@ namespace Kanboard\Model;
|
|||||||
class ProjectFileModel extends FileModel
|
class ProjectFileModel extends FileModel
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* SQL table name
|
* Table name
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
const TABLE = 'project_has_files';
|
const TABLE = 'project_has_files';
|
||||||
|
|
||||||
/**
|
|
||||||
* SQL foreign key
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
const FOREIGN_KEY = 'project_id';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Path prefix
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
const PATH_PREFIX = 'projects';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Events
|
* Events
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
const EVENT_CREATE = 'project.file.create';
|
const EVENT_CREATE = 'project.file.create';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the table
|
||||||
|
*
|
||||||
|
* @abstract
|
||||||
|
* @access protected
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function getTable()
|
||||||
|
{
|
||||||
|
return self::TABLE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define the foreign key
|
||||||
|
*
|
||||||
|
* @abstract
|
||||||
|
* @access protected
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function getForeignKey()
|
||||||
|
{
|
||||||
|
return 'project_id';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define the path prefix
|
||||||
|
*
|
||||||
|
* @abstract
|
||||||
|
* @access protected
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function getPathPrefix()
|
||||||
|
{
|
||||||
|
return 'projects';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get event name
|
||||||
|
*
|
||||||
|
* @abstract
|
||||||
|
* @access protected
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function getEventName()
|
||||||
|
{
|
||||||
|
return self::EVENT_CREATE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,26 +11,12 @@ namespace Kanboard\Model;
|
|||||||
class TaskFileModel extends FileModel
|
class TaskFileModel extends FileModel
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* SQL table name
|
* Table name
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
const TABLE = 'task_has_files';
|
const TABLE = 'task_has_files';
|
||||||
|
|
||||||
/**
|
|
||||||
* SQL foreign key
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
const FOREIGN_KEY = 'task_id';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Path prefix
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
const PATH_PREFIX = 'tasks';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Events
|
* Events
|
||||||
*
|
*
|
||||||
@@ -38,6 +24,54 @@ class TaskFileModel extends FileModel
|
|||||||
*/
|
*/
|
||||||
const EVENT_CREATE = 'task.file.create';
|
const EVENT_CREATE = 'task.file.create';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the table
|
||||||
|
*
|
||||||
|
* @abstract
|
||||||
|
* @access protected
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function getTable()
|
||||||
|
{
|
||||||
|
return self::TABLE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define the foreign key
|
||||||
|
*
|
||||||
|
* @abstract
|
||||||
|
* @access protected
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function getForeignKey()
|
||||||
|
{
|
||||||
|
return 'task_id';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define the path prefix
|
||||||
|
*
|
||||||
|
* @abstract
|
||||||
|
* @access protected
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function getPathPrefix()
|
||||||
|
{
|
||||||
|
return 'tasks';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get event name
|
||||||
|
*
|
||||||
|
* @abstract
|
||||||
|
* @access protected
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function getEventName()
|
||||||
|
{
|
||||||
|
return self::EVENT_CREATE;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle screenshot upload
|
* Handle screenshot upload
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user