From 964d99bd80b6d592399787b45626b36d9d8ac2ba Mon Sep 17 00:00:00 2001 From: Diego Najar Date: Sun, 23 Aug 2020 18:59:56 +0200 Subject: [PATCH] API, include upload files for a page --- bl-plugins/api/plugin.php | 50 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/bl-plugins/api/plugin.php b/bl-plugins/api/plugin.php index c8bbfc40..ec5dc4c4 100644 --- a/bl-plugins/api/plugin.php +++ b/bl-plugins/api/plugin.php @@ -199,6 +199,11 @@ class pluginAPI extends Plugin { $pageKey = $parameters[1]; $data = $this->getFiles($pageKey); } + // (POST) /api/files/ + elseif ( ($method==='POST') && ($parameters[0]==='files') && !empty($parameters[1]) ) { + $pageKey = $parameters[1]; + $data = $this->uploadFile($pageKey); + } else { $this->response(401, 'Unauthorized', array('message'=>'Access denied or invalid endpoint.')); } @@ -711,4 +716,49 @@ class pluginAPI extends Plugin { 'data'=>$files ); } + + /* + | Upload a file to a particular page + | Returns the file URL + | + | @inputs array + | @inputs['uuid'] string Page UUID + | @_FILE array https://www.php.net/manual/en/reserved.variables.files.php + | + | @return array + */ + private function uploadFile($pageKey) + { + if (!isset($_FILES['file'])) { + return array( + 'status'=>'1', + 'message'=>'File not sent.' + ); + } + + if ($_FILES['file']['error'] != 0) { + return array( + 'status'=>'1', + 'message'=>'Error uploading the file.' + ); + } + + $filename = $_FILES['file']['name']; + $absoluteURL = DOMAIN_UPLOADS_PAGES.$pageKey.DS.$filename; + $absolutePath = PATH_UPLOADS_PAGES.$pageKey.DS.$filename; + if (Filesystem::mv($_FILES['file']['tmp_name'], $absolutePath)) { + return array( + 'status'=>'0', + 'message'=>'File uploaded.', + 'filename'=>$filename, + 'absolutePath'=>$absolutePath, + 'absoluteURL'=>$absoluteURL + ); + } + + return array( + 'status'=>'1', + 'message'=>'Error moving the file to the final path.' + ); + } } \ No newline at end of file