diff options
author | Frederic Guillot <fred@kanboard.net> | 2015-06-13 15:47:48 -0400 |
---|---|---|
committer | Frederic Guillot <fred@kanboard.net> | 2015-06-13 15:47:48 -0400 |
commit | 0b7435b8827081341a331ecdd5546ac25121d87d (patch) | |
tree | e439735e077ceb0b4f77266b1f3e9fcd337e55e8 /app/Model/File.php | |
parent | 41610150238a67471d79caa5bcb2ace1dd4578d1 (diff) |
API: new procedure 'removeAllFiles' and contract change for 'createFile'
Diffstat (limited to 'app/Model/File.php')
-rw-r--r-- | app/Model/File.php | 134 |
1 files changed, 47 insertions, 87 deletions
diff --git a/app/Model/File.php b/app/Model/File.php index 1f62a55e..38b34cd3 100644 --- a/app/Model/File.php +++ b/app/Model/File.php @@ -49,7 +49,8 @@ class File extends Base { $file = $this->getbyId($file_id); - if (! empty($file) && @unlink(FILES_DIR.$file['path'])) { + if (! empty($file)) { + @unlink(FILES_DIR.$file['path']); return $this->db->table(self::TABLE)->eq('id', $file_id)->remove(); } @@ -66,10 +67,13 @@ class File extends Base public function removeAll($task_id) { $files = $this->getAll($task_id); + $results = array(); foreach ($files as $file) { - $this->remove($file['id']); + $results[] = $this->remove($file['id']); } + + return ! in_array(false, $results, true); } /** @@ -79,36 +83,41 @@ class File extends Base * @param integer $task_id Task id * @param string $name Filename * @param string $path Path on the disk - * @param bool $is_image Image or not * @param integer $size File size - * @return bool + * @return bool|integer */ - public function create($task_id, $name, $path, $is_image, $size) + public function create($task_id, $name, $path, $size) { - $this->container['dispatcher']->dispatch( - self::EVENT_CREATE, - new FileEvent(array('task_id' => $task_id, 'name' => $name)) - ); - - return $this->db->table(self::TABLE)->save(array( + $result = $this->db->table(self::TABLE)->save(array( 'task_id' => $task_id, 'name' => substr($name, 0, 255), 'path' => $path, - 'is_image' => $is_image ? '1' : '0', + 'is_image' => $this->isImage($name) ? 1 : 0, 'size' => $size, 'user_id' => $this->userSession->getId() ?: 0, 'date' => time(), )); + + if ($result) { + + $this->container['dispatcher']->dispatch( + self::EVENT_CREATE, + new FileEvent(array('task_id' => $task_id, 'name' => $name)) + ); + + return (int) $this->db->getConnection()->getLastId(); + } + + return false; } /** - * Get all files for a given task + * Get PicoDb query to get all files * * @access public - * @param integer $task_id Task id - * @return array + * @return \PicoDb\Table */ - public function getAll($task_id) + public function getQuery() { return $this->db ->table(self::TABLE) @@ -125,9 +134,19 @@ class File extends Base User::TABLE.'.name as user_name' ) ->join(User::TABLE, 'id', 'user_id') - ->eq('task_id', $task_id) - ->asc(self::TABLE.'.name') - ->findAll(); + ->asc(self::TABLE.'.name'); + } + + /** + * Get all files for a given task + * + * @access public + * @param integer $task_id Task id + * @return array + */ + public function getAll($task_id) + { + return $this->getQuery()->eq('task_id', $task_id)->findAll(); } /** @@ -139,25 +158,7 @@ class File extends Base */ public function getAllImages($task_id) { - return $this->db - ->table(self::TABLE) - ->columns( - self::TABLE.'.id', - self::TABLE.'.name', - self::TABLE.'.path', - self::TABLE.'.is_image', - self::TABLE.'.task_id', - self::TABLE.'.date', - self::TABLE.'.user_id', - self::TABLE.'.size', - User::TABLE.'.username', - User::TABLE.'.name as user_name' - ) - ->join(User::TABLE, 'id', 'user_id') - ->eq('task_id', $task_id) - ->eq('is_image', 1) - ->asc(self::TABLE.'.name') - ->findAll(); + return $this->getQuery()->eq('task_id', $task_id)->eq('is_image', 1)->findAll(); } /** @@ -169,29 +170,11 @@ class File extends Base */ public function getAllDocuments($task_id) { - return $this->db - ->table(self::TABLE) - ->columns( - self::TABLE.'.id', - self::TABLE.'.name', - self::TABLE.'.path', - self::TABLE.'.is_image', - self::TABLE.'.task_id', - self::TABLE.'.date', - self::TABLE.'.user_id', - self::TABLE.'.size', - User::TABLE.'.username', - User::TABLE.'.name as user_name' - ) - ->join(User::TABLE, 'id', 'user_id') - ->eq('task_id', $task_id) - ->eq('is_image', 0) - ->asc(self::TABLE.'.name') - ->findAll(); + return $this->getQuery()->eq('task_id', $task_id)->eq('is_image', 0)->findAll(); } /** - * Check if a filename is an image + * Check if a filename is an image (file types that can be shown as thumbnail) * * @access public * @param string $filename Filename @@ -227,24 +210,6 @@ class File extends Base } /** - * Check if the base directory is created correctly - * - * @access public - */ - public function setup() - { - if (! is_dir(FILES_DIR)) { - if (! mkdir(FILES_DIR, 0755, true)) { - die('Unable to create the upload directory: "'.FILES_DIR.'"'); - } - } - - if (! is_writable(FILES_DIR)) { - die('The directory "'.FILES_DIR.'" must be writeable by your webserver user'); - } - } - - /** * Handle file upload * * @access public @@ -255,8 +220,7 @@ class File extends Base */ public function upload($project_id, $task_id, $form_name) { - $this->setup(); - $result = array(); + $results = array(); if (! empty($_FILES[$form_name])) { @@ -272,11 +236,10 @@ class File extends Base if (@move_uploaded_file($uploaded_filename, FILES_DIR.$destination_filename)) { - $result[] = $this->create( + $results[] = $this->create( $task_id, $original_filename, $destination_filename, - $this->isImage($original_filename), $_FILES[$form_name]['size'][$key] ); } @@ -284,7 +247,7 @@ class File extends Base } } - return count(array_unique($result)) === 1; + return ! in_array(false, $results, true); } /** @@ -294,7 +257,7 @@ class File extends Base * @param integer $project_id Project id * @param integer $task_id Task id * @param string $blob Base64 encoded image - * @return bool + * @return bool|integer */ public function uploadScreenshot($project_id, $task_id, $blob) { @@ -314,7 +277,6 @@ class File extends Base $task_id, $original_filename, $destination_filename, - true, strlen($data) ); } @@ -326,11 +288,10 @@ class File extends Base * @param integer $project_id Project id * @param integer $task_id Task id * @param string $filename Filename - * @param bool $is_image Is image file? * @param string $blob Base64 encoded image - * @return bool + * @return bool|integer */ - public function uploadContent($project_id, $task_id, $filename, $is_image, &$blob) + public function uploadContent($project_id, $task_id, $filename, $blob) { $data = base64_decode($blob); @@ -347,7 +308,6 @@ class File extends Base $task_id, $filename, $destination_filename, - $is_image, strlen($data) ); } |