From cf1fb1bc636850dd194784940cffa5c88319deac Mon Sep 17 00:00:00 2001 From: sctn4elk Date: Tue, 14 May 2024 11:38:57 -0500 Subject: [PATCH] Continued work on image upload --- Controller/API/BaseController.php | 11 +++ Controller/API/CustomerController.php | 5 +- Controller/API/ImageController.php | 97 +++++++++------------------ Model/CustomerModel.php | 4 +- Model/Database.php | 18 +---- Model/ImageModel.php | 25 +++++-- 6 files changed, 69 insertions(+), 91 deletions(-) diff --git a/Controller/API/BaseController.php b/Controller/API/BaseController.php index afc5968..daaf941 100644 --- a/Controller/API/BaseController.php +++ b/Controller/API/BaseController.php @@ -35,6 +35,17 @@ class BaseController { return $response; } + public function unprocessableRequestResponse() + { + $response['status_code_header'] = 'HTTP/1.1 422 Unprocessable Request'; + $response['body'] = json_encode([ + 'error' => 'Invalid request' + ]); + $this->strErrorDesc = 'Request Method not supported for processAction'; + $this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request'; + return $response; + } + public function unprocessableEntityResponse() { $response['status_code_header'] = 'HTTP/1.1 422 Unprocessable Payload'; diff --git a/Controller/API/CustomerController.php b/Controller/API/CustomerController.php index fc0fab5..2a124e5 100644 --- a/Controller/API/CustomerController.php +++ b/Controller/API/CustomerController.php @@ -1,6 +1,8 @@ customerModel = new CustomerModel(); //return var_dump($this->customerModel); $this->arrQueryStringParams = $this->getQueryStringParams(); diff --git a/Controller/API/ImageController.php b/Controller/API/ImageController.php index 828e64e..7a2b695 100644 --- a/Controller/API/ImageController.php +++ b/Controller/API/ImageController.php @@ -1,5 +1,10 @@ strErrorHeader = ''; try { $this->requestMethod = $this->getServerRequestMethod(); - + $this->imageModel = new ImageModel(); + switch($this->action) { case "upload": $response = $this->uploadImage(); @@ -64,85 +71,47 @@ class ImageController extends BaseController { } } + /* Upload the image and store on server as file */ private function uploadImage(){ - header("Access-Control-Allow-Origin: *"); - header("Access-Control-Allow-Methods: POST"); - header("Content-Type: application/json"); - if ($this->checkRequestType('POST') == 'false') { - $this->strErrorDesc = 'Request Method not supported for processAction'; - $this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request'; - return; + return $this->unprocessableRequestResponse(); } - $this->imageModel = new ImageModel(); + $this->imagePayload = file_get_contents($_FILES['image']['tmp_name']); + $this->imageModel->fileName = $_FILES['image']['name']; + if(empty($this->imageModel->fileName)){ + return $this->notFoundResponse(); + } - // reads the raw POST data and returns it as a string. - //base64_decode($_POST['content']); - $imagePayload = file_get_contents($_FILES['your_image_name']['tmp_name']); - if (! $this->validateImage($imagePayload)) { + $this->imageModel->tempPath = $_FILES["image"]["tmp_name"]; + $this->imageModel->basename = basename($this->imageModel->fileName); + $this->imageModel->originalPath = $this->imageModel->uploadTo.$this->imageModel->basename; + $this->imageModel->fileType = pathinfo($this->imageModel->originalPath, PATHINFO_EXTENSION); + + if (! $this->validateImage()) { return $this->unprocessableEntityResponse(); } - $response = $this->insertImage(); + if (! $this->handleImage()) { + return $this->notFoundResponse(); + } + + $response = $this->imageModel->insertImage(); return $response; } - private function insertImage() { - if (isset($_FILES['image'])) { - $uploadImage = $this->processImage(); - $success = false; - - if (!$uploadImage['error']) { - - } + private function handleImage() { + $moved = false; + if(!move_uploaded_file($this->imageModel->tempPath, $this->imageModel->originalPath)) { + $moved = true; } + return $moved; } - private function processImage() { - $error = false; - $msg = null; - - $uploadTo = "public/images/"; - $allowFileType = array('jpg','png','jpeg'); - $fileName = $_FILES['image']['name']; - $tempPath = $_FILES["image"]["tmp_name"]; - - $basename = basename($fileName); - $originalPath = $uploadTo.$basename; - $fileType = pathinfo($originalPath, PATHINFO_EXTENSION); - - if(!empty($fileName)){ - if(in_array($fileType, $allowFileType)){ - if(!move_uploaded_file($tempPath, $originalPath)){ - $msg = 'Image Not uploaded ! try again'; - $error = true; - } - } else { - $msg = 'Image type is not allowed'; - $error = true; - } - } else { - $msg = 'Image is required'; - $error = true; - } - - $imageInfo = [ - "error" => $error, - "msg" => $msg, - "filename" => $fileName, - "filepath" => $originalPath - ]; - return $imageInfo; - } - - private function validateImage($imageData){ + private function validateImage(){ $validtion = false; - if($imageData['customer_name_first'] != null){ + if(in_array($this->imageModel->fileType, $this->imageModel->allowFileType)){ $validtion = true; - if($imageData['customer_name_last'] == null) { - $validtion = false; - } } return $validtion; } diff --git a/Model/CustomerModel.php b/Model/CustomerModel.php index 5c2e028..fa03b56 100644 --- a/Model/CustomerModel.php +++ b/Model/CustomerModel.php @@ -25,10 +25,10 @@ define('PD', 'D:\DEV\Git Repository\CustomerRewardsRESTAPI'); //require_once PROJECT_ROOT_PATH . "/Model/Database.php"; require_once PD . "/Model/Database.php"; -//require_once PD . "/Model/ModelTraits.php"; +require_once PD . "/Model/ModelTraits.php"; class CustomerModel extends Database { - //use ModelTraits; + use ModelTraits; public function findAllCustomers() { diff --git a/Model/Database.php b/Model/Database.php index 1dfa3fa..a807a3c 100644 --- a/Model/Database.php +++ b/Model/Database.php @@ -6,6 +6,7 @@ */ class Database { protected $connection = null; + public function __construct() { try { @@ -41,23 +42,6 @@ class Database { return false; } - /*private function executeQuery($query = "" , $params = []) - { - try { - $stmt = $this->connection->prepare( $query ); - if($stmt === false) { - throw New Exception("Unable to do prepared statement: " . $query); - } - if( $params ) { - $stmt->bind_param($params[0], $params[1]); - } - $stmt->execute(); - return $stmt; - } catch(Exception $e) { - throw New Exception( $e->getMessage()); - } - }*/ - public function processStatement($query = "") { try { diff --git a/Model/ImageModel.php b/Model/ImageModel.php index a7b3709..0035bd2 100644 --- a/Model/ImageModel.php +++ b/Model/ImageModel.php @@ -1,10 +1,5 @@ uploadTo = "public/images/"; + $this->allowFileType = array('jpg','png','jpeg','gif'); + } + + /* Process the uploaded image and store in database */ + public function insertImage() { //return var_dump($jsonPayLoad); $keys = array_keys($inputModel);