summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorFrederic Guillot <fred@kanboard.net>2015-06-13 15:47:48 -0400
committerFrederic Guillot <fred@kanboard.net>2015-06-13 15:47:48 -0400
commit0b7435b8827081341a331ecdd5546ac25121d87d (patch)
treee439735e077ceb0b4f77266b1f3e9fcd337e55e8 /app
parent41610150238a67471d79caa5bcb2ace1dd4578d1 (diff)
API: new procedure 'removeAllFiles' and contract change for 'createFile'
Diffstat (limited to 'app')
-rw-r--r--app/Api/File.php9
-rw-r--r--app/Controller/File.php2
-rw-r--r--app/Model/File.php134
-rw-r--r--app/check_setup.php12
-rw-r--r--app/common.php1
5 files changed, 68 insertions, 90 deletions
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
@@ -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)
);
}
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') === '&amp;') {
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);