From 97207ea5367d927ee82474686fba054562686ef7 Mon Sep 17 00:00:00 2001 From: sctn4elk Date: Thu, 9 May 2024 16:40:42 -0500 Subject: [PATCH] Begin work on image processing --- Controller/API/CustomerController.php | 6 +- Controller/API/ImageController.php | 169 ++++++++++++++++++++++++++ Model/ImageModel.php | 61 ++++++++++ index.php | 5 + 4 files changed, 238 insertions(+), 3 deletions(-) create mode 100644 Controller/API/ImageController.php create mode 100644 Model/ImageModel.php diff --git a/Controller/API/CustomerController.php b/Controller/API/CustomerController.php index 1310ebf..354e085 100644 --- a/Controller/API/CustomerController.php +++ b/Controller/API/CustomerController.php @@ -12,7 +12,7 @@ header("Content-Type: application/json"); * http://localhost:8000/index.php/customer/process/insert?name=Mike%20Howard&email=sctn4elk@msn.com&phone=208-841-4159&birthday=05/07/1965&loyalty=1&city=Winnsboro&state=TX&zip=75494 * http://localhost:8000/index.php/customer/process/select?limit=20 */ -class CustomerController extends BaseController{ +class CustomerController extends BaseController { /** * "/customer/list" Endpoint - Get list of users */ @@ -49,7 +49,7 @@ class CustomerController extends BaseController{ case "delete": $response = $this->deleteCustomer(); break; - + default: $response = (object) ['Result' => 'Default']; $this->strErrorDesc = 'Controller Method not supported for processAction: ' . $this->action; @@ -182,7 +182,7 @@ class CustomerController extends BaseController{ } return $response; } - + private function checkRequestType($request) { $response = 'false'; diff --git a/Controller/API/ImageController.php b/Controller/API/ImageController.php new file mode 100644 index 0000000..45814fd --- /dev/null +++ b/Controller/API/ImageController.php @@ -0,0 +1,169 @@ +imageModel = new ImageModel(); + $this->requestMethod = $this->getServerRequestMethod(); + } + + /* + * http://localhost/CustomerRewardsRESTAPI/index.php/image/process/upload + */ + public function processAction() { + $this->strErrorDesc = ''; + $this->strErrorHeader = ''; + try { + switch($this->action) { + case "upload": + $response = $this->uploadImage(); + break; + + case "update": + $response = $this->updateImage(); + break; + + case "delete": + $response = $this->deleteImage(); + break; + + default: + $response = (object) ['Result' => 'Default']; + $this->strErrorDesc = 'Controller Method not supported for processAction: ' . $this->action; + $this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity'; + break; + } + + $responseData = json_encode($response); + } catch (Error $e) { + $this->strErrorDesc = $e->getMessage().' Something went wrong in processAction! Please contact support.'; + $this->strErrorHeader = 'HTTP/1.1 500 Internal Server Error'; + } + + // send output + if (!$this->strErrorDesc) { + $this->sendOutput( + $responseData, + array('Content-Type: application/json', 'HTTP/1.1 200 OK') + ); + } else { + $this->sendOutput(json_encode(array('error' => $this->strErrorDesc)), + array('Content-Type: application/json', $this->strErrorHeader) + ); + } + } + + private function uploadImage(){ + if ($this->checkRequestType('POST') == 'false') { + $this->strErrorDesc = 'Request Method not supported for processAction'; + $this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request'; + return; + } + // reads the raw POST data and returns it as a string. + $jsonPayload = file_get_contents('php://input'); + $input = json_decode($jsonPayload, TRUE); + if (! $this->validatePerson($input)) { + return $this->unprocessableEntityResponse(); + } + + //remove customer_id field so it doesn't break + unset($input['customer_id']); + + $response = $this->customerModel->insertCustomer($input); + return $response; + } + + 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 insert() { + header("Access-Control-Allow-Origin: *"); + header("Access-Control-Allow-Methods: POST"); + header("Content-Type: application/json"); + + if ($_SERVER['REQUEST_METHOD'] === 'POST') { + if (isset($_FILES['image'])) { + $uploadImage = $this->processImage(); + $success = false; + + if (!$uploadImage['error']) { + // table name for admin profiles + $query = "INSERT INTO " . $this->imageTable; + $query .= " (filename, filepath) VALUES (?,?)"; + $stmt = $this->conn->prepare($query); + + $stmt->bind_param("ss", $uploadImage['filename'], $uploadImage['filepath']); + + if ($stmt->execute()) { + $success = true; + $stmt->close(); + } + } + + $data = [ + 'Errormsg' => $uploadImage['msg'] ?? '', + 'success' => $success + ]; + + return json_encode($data); + } + } + } + + private function notFoundResponse() + { + $response['status_code_header'] = 'HTTP/1.1 404 Entity Not Found'; + $response['body'] = null; + $this->strErrorDesc = 'Request Entity Not Found'; + $this->strErrorHeader = 'HTTP/1.1 422 Entity Not Found'; + return $response; + } +} diff --git a/Model/ImageModel.php b/Model/ImageModel.php new file mode 100644 index 0000000..e4daf9c --- /dev/null +++ b/Model/ImageModel.php @@ -0,0 +1,61 @@ +params[$name] = $value; + } + + /* + * @assert ('name') == 'value' + */ + public function __get($name) + { + //echo "Getting '$name'\n"; + if (array_key_exists($name, $this->params)) { + return $this->params[$name]; + } + + $trace = debug_backtrace(); + trigger_error( + 'Undefined property via __get(): ' . $name . + ' in ' . $trace[0]['file'] . + ' on line ' . $trace[0]['line'], + E_USER_NOTICE); + return null; + } + + /* + * @assert ('name') == 'true' + * @assert ('test') == 'false' + */ + public function __isset($name) + { + //echo "Is '$name' set?\n"; + return isset($this->params[$name]); + } + + public function __unset($name) + { + //echo "Unsetting '$name'\n"; + unset($this->params[$name]); + } + + +} diff --git a/index.php b/index.php index 9e488bb..b80eb17 100644 --- a/index.php +++ b/index.php @@ -50,6 +50,11 @@ switch($uri[$uri_pos + 1]) { $objFeedController = new CustomerController(); break; + case "image": + require PROJECT_ROOT_PATH . "/Controller/Api/ImageController.php"; + $objFeedController = new ImageController(); + break; + default: header("HTTP/1.1 404 Module Not Found"); exit();