summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorFrederic Guillot <fred@kanboard.net>2015-05-24 20:28:54 -0400
committerFrederic Guillot <fred@kanboard.net>2015-05-24 20:28:54 -0400
commit00c2e5c80ee4b6c5e5234e5b6a333bb19edd9b76 (patch)
treed6ad2135d614de68adf2fb6a8e8d702785e9ce0a /app
parent3eb5501ca0cfc11e774514a4169c718d1e54854d (diff)
Add file procedures to the API
Diffstat (limited to 'app')
-rw-r--r--app/Api/File.php48
-rw-r--r--app/Model/File.php35
2 files changed, 82 insertions, 1 deletions
diff --git a/app/Api/File.php b/app/Api/File.php
new file mode 100644
index 00000000..11c48404
--- /dev/null
+++ b/app/Api/File.php
@@ -0,0 +1,48 @@
+<?php
+
+namespace Api;
+
+/**
+ * File API controller
+ *
+ * @package api
+ * @author Frederic Guillot
+ */
+class File extends Base
+{
+ public function getFile($file_id)
+ {
+ return $this->file->getById($file_id);
+ }
+
+ public function getAllFiles($task_id)
+ {
+ return $this->file->getAll($task_id);
+ }
+
+ public function downloadFile($file_id)
+ {
+ $file = $this->file->getById($file_id);
+
+ if (! empty($file)) {
+
+ $filename = FILES_DIR.$file['path'];
+
+ if (file_exists($filename)) {
+ return base64_encode(file_get_contents($filename));
+ }
+ }
+
+ return '';
+ }
+
+ public function createFile($project_id, $task_id, $filename, $is_image, &$blob)
+ {
+ return $this->file->uploadContent($project_id, $task_id, $filename, $is_image, $blob);
+ }
+
+ public function removeFile($file_id)
+ {
+ return $this->file->remove($file_id);
+ }
+}
diff --git a/app/Model/File.php b/app/Model/File.php
index cb1e4792..16bf079e 100644
--- a/app/Model/File.php
+++ b/app/Model/File.php
@@ -96,7 +96,7 @@ class File extends Base
'path' => $path,
'is_image' => $is_image ? '1' : '0',
'size' => $size,
- 'user_id' => $this->userSession->getId(),
+ 'user_id' => $this->userSession->getId() ?: 0,
'date' => time(),
));
}
@@ -320,6 +320,39 @@ class File extends Base
}
/**
+ * Handle file upload (base64 encoded content)
+ *
+ * @access public
+ * @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
+ */
+ public function uploadContent($project_id, $task_id, $filename, $is_image, &$blob)
+ {
+ $data = base64_decode($blob);
+
+ if (empty($data)) {
+ return false;
+ }
+
+ $destination_filename = $this->generatePath($project_id, $task_id, $filename);
+
+ @mkdir(FILES_DIR.dirname($destination_filename), 0755, true);
+ @file_put_contents(FILES_DIR.$destination_filename, $data);
+
+ return $this->create(
+ $task_id,
+ $filename,
+ $destination_filename,
+ $is_image,
+ strlen($data)
+ );
+ }
+
+ /**
* Generate a jpeg thumbnail from an image (output directly the image)
*
* @access public