From 0b7435b8827081341a331ecdd5546ac25121d87d Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sat, 13 Jun 2015 15:47:48 -0400 Subject: API: new procedure 'removeAllFiles' and contract change for 'createFile' --- app/Api/File.php | 9 +++- app/Controller/File.php | 2 +- app/Model/File.php | 134 +++++++++++++++++------------------------------- app/check_setup.php | 12 +++++ app/common.php | 1 + 5 files changed, 68 insertions(+), 90 deletions(-) (limited to 'app') diff --git a/app/Api/File.php b/app/Api/File.php index 65dff729..5b82179c 100644 --- a/app/Api/File.php +++ b/app/Api/File.php @@ -36,13 +36,18 @@ class File extends Base return ''; } - public function createFile($project_id, $task_id, $filename, $is_image, $blob) + public function createFile($project_id, $task_id, $filename, $blob) { - return $this->file->uploadContent($project_id, $task_id, $filename, $is_image, $blob); + return $this->file->uploadContent($project_id, $task_id, $filename, $blob); } public function removeFile($file_id) { return $this->file->remove($file_id); } + + public function removeAllFiles($task_id) + { + return $this->file->removeAll($task_id); + } } diff --git a/app/Controller/File.php b/app/Controller/File.php index f0367537..f73a9de9 100644 --- a/app/Controller/File.php +++ b/app/Controller/File.php @@ -19,7 +19,7 @@ class File extends Base { $task = $this->getTask(); - if ($this->request->isPost() && $this->file->uploadScreenshot($task['project_id'], $task['id'], $this->request->getValue('screenshot'))) { + if ($this->request->isPost() && $this->file->uploadScreenshot($task['project_id'], $task['id'], $this->request->getValue('screenshot')) !== false) { $this->session->flash(t('Screenshot uploaded successfully.')); 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 @@ -226,24 +209,6 @@ class File extends Base return $project_id.DIRECTORY_SEPARATOR.$task_id.DIRECTORY_SEPARATOR.hash('sha1', $filename.time()); } - /** - * 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 * @@ -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) ); } diff --git a/app/check_setup.php b/app/check_setup.php index 065b8e10..624b6b34 100644 --- a/app/check_setup.php +++ b/app/check_setup.php @@ -38,3 +38,15 @@ if (! is_writable('data')) { if (ini_get('arg_separator.output') === '&') { ini_set('arg_separator.output', '&'); } + +// Prepare folder for uploaded files +if (! is_dir(FILES_DIR)) { + if (! mkdir(FILES_DIR, 0755, true)) { + die('Unable to create the upload directory: "'.FILES_DIR.'"'); + } +} + +// Check permissions for files folder +if (! is_writable(FILES_DIR)) { + die('The directory "'.FILES_DIR.'" must be writeable by your webserver user'); +} diff --git a/app/common.php b/app/common.php index f4485267..d1659018 100644 --- a/app/common.php +++ b/app/common.php @@ -21,6 +21,7 @@ if (file_exists('config.php')) { } require __DIR__.'/constants.php'; +require __DIR__.'/check_setup.php'; $container = new Pimple\Container; $container->register(new ServiceProvider\LoggingProvider); -- cgit v1.2.3