From 9460fac030dccd5b0a0d7485220e1e833d4b6ab6 Mon Sep 17 00:00:00 2001 From: sctn4elk Date: Wed, 29 May 2024 01:59:54 -0500 Subject: [PATCH] Work on storing image to database --- Controller/API/BaseController.php | 4 +- Controller/API/ImageController.php | 66 ++++++++++++++++++++++++++---- Include/bootstrap.php | 4 +- Model/CustomerModel.php | 5 +-- Model/Database.php | 47 ++++++++++++++++----- Model/ImageModel.php | 8 +++- 6 files changed, 108 insertions(+), 26 deletions(-) diff --git a/Controller/API/BaseController.php b/Controller/API/BaseController.php index 517470a..5d0efd6 100644 --- a/Controller/API/BaseController.php +++ b/Controller/API/BaseController.php @@ -47,12 +47,12 @@ class BaseController { */ public function unprocessableRequestResponse($msg) { - $response['status_code_header'] = 'HTTP/1.1 422 Unprocessable Request'; + $response['status_code_header'] = 'HTTP/1.1 405 Method Not Supported'; $response['body'] = json_encode([ 'error' => 'Invalid request' ]); $this->strErrorDesc = 'Request Method not supported for processAction'; - $this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request'; + $this->strErrorHeader = 'HTTP/1.1 405 Method Not Supported'; $this->strErrorMessage = $msg; return $response; } diff --git a/Controller/API/ImageController.php b/Controller/API/ImageController.php index 3347c7d..3522f97 100644 --- a/Controller/API/ImageController.php +++ b/Controller/API/ImageController.php @@ -75,13 +75,21 @@ class ImageController extends BaseController { return $this->unprocessableRequestResponse(); } - // reads the raw POST data and returns it as a string. - $jsonPayload = file_get_contents('php://input'); - $input = json_decode($jsonPayload, TRUE); - + /* + * array(1) { + ["MemberImageModel"]=> + string(240) "{"CustomerID":1,"ImagePath":"C:\\DEV\\CustomerRewardsAdminPortal\\Resources\\Images\\headshot.jpg","ImageName":"headshot.jpg","ImageType":"image/jpg","ImageBlob":{"Headers":[{"Key":"Content-Type","Value":["image/jpg"]}]},"SourceImage":null}" +} + */ + // reads the raw POST data + $jsonPayload = filter_input_array(INPUT_POST); + $input = json_decode($jsonPayload['MemberImageModel'], TRUE); + //get the member data - $this->memberId = $input['CustomerID']; - $this->imageBlob = $input['ImageBlob']; + $this->imageModel->memberId = $input['CustomerID']; + $this->imageModel->imagePath = $input['ImagePath']; + $this->imageModel->imageName = $input['ImageName']; + $this->imageModel->imageType = $input['ImageType']; //get the file data $this->imagePayload = $_FILES['image']; @@ -95,6 +103,12 @@ class ImageController extends BaseController { $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 (!is_readable($this->imageModel->tempPath)) { + return $this->notFoundResponse(); + } + + $this->imageModel->imageBlob = base64_encode(file_get_contents($this->imageModel->tempPath)); if (! $this->validateImage()) { return $this->unprocessableEntityResponse(); @@ -104,7 +118,7 @@ class ImageController extends BaseController { return $this->notFoundResponse(); } - $this->imageModel->insertImage(); + $response = $this->imageModel->insertImage(); return $response; } @@ -116,6 +130,44 @@ class ImageController extends BaseController { return $moved; } + private function transferImage() { + $src = $this->imageModel->uploadTo; + $dest = "/server/location/upload/" . $this->imageModel->fileName; + $check = file_put_contents($dest, file_get_contents($src)); + if($check != false){ + $check = true; + } + + /* Transfer between web servers + if ( isset($_FILES['uploadedfile']) ) { + $filename = $_FILES['uploadedfile']['tmp_name']; + $handle = fopen($filename, "r"); + $data = fread($handle, filesize($filename)); + $POST_DATA = array( + 'file' => base64_encode($data) + ); + + $curl = curl_init(); + curl_setopt($curl, CURLOPT_URL, 'http://extserver.com/handle.php'); + curl_setopt($curl, CURLOPT_TIMEOUT, 30); + curl_setopt($curl, CURLOPT_POST, 1); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($curl, CURLOPT_POSTFIELDS, $POST_DATA); + $response = curl_exec($curl); + curl_close ($curl); + } + */ + + /* Separate file running on a different web server + $encoded_file = $_POST['file']; + $decoded_file = base64_decode($encoded_file); + //Now you can copy the uploaded file to your server. + file_put_contents('subins', $decoded_file); + */ + + return $check; + } + private function validateImage(){ $validtion = false; if(in_array($this->imageModel->fileType, $this->imageModel->allowFileType)){ diff --git a/Include/bootstrap.php b/Include/bootstrap.php index da5021b..ddb97d7 100644 --- a/Include/bootstrap.php +++ b/Include/bootstrap.php @@ -6,8 +6,8 @@ */ define("PROJECT_ROOT_PATH", __DIR__ . "/../"); -define('PD', 'D:\Programs\UniformServer\UniServerZ\www\CustomerRewardsRESTAPI'); -//define('PD', 'C:\DEV\UniServerZ\www\CustomerRewardsRESTAPI'); +//define('PD', 'D:\Programs\UniformServer\UniServerZ\www\CustomerRewardsRESTAPI'); +define('PD', 'C:\DEV\UniServerZ\www\CustomerRewardsRESTAPI'); // include main configuration file require_once PROJECT_ROOT_PATH . "/include/config.php"; diff --git a/Model/CustomerModel.php b/Model/CustomerModel.php index 4867ef4..1bc8783 100644 --- a/Model/CustomerModel.php +++ b/Model/CustomerModel.php @@ -1,4 +1,6 @@ connection->prepare($query); - if($stmt === false) { - throw New Exception("Unable to prepare the statement: " . $query); + try + { + if($this->connection == null) + { + $this->connection = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE_NAME); } - + /*$stmt = $this->connection->prepare($query); + if($stmt === false) { + $msg = "Unable to prepare the statement: " . $query; + return $this->unprocessableQueryResponse($msg); + throw New Exception($msg); + }*/ + //return var_dump($query); //$stmt->bind_param("ss", $params['filename'], $params['filepath']); - - $result = $stmt->execute(); + $result = $this->connection->execute_query($query); + //$result = $stmt->execute(); if($result === false) { - throw New Exception("Unable to execute the statement: " . $query); + $msg = "Unable to execute the statement: " . $query; + return $this->unprocessableQueryResponse($msg); + throw New Exception($msg); } $rowCount = $this->connection->affected_rows; if($rowCount < 1) { - throw New Exception("Statement did not return any rows: " . $query); + $msg = "Statement did not return any rows: " . $query; + return $this->unprocessableQueryResponse($msg); + throw New Exception($msg); } $stmt->close(); return $rowCount; } catch(Exception $e) { - throw New Exception( $e->getMessage() ); + $msg = $e->getMessage(); + return $this->unprocessableQueryResponse($msg); + throw New Exception($msg); } - return false; + return $this->unprocessableQueryResponse("Unknown error"); + } + + private function unprocessableQueryResponse($msg) + { + $response['status_code_header'] = 'HTTP/1.1 422 Unprocessable Query'; + $response['body'] = json_encode([ + 'error' => 'Invalid input' + ]); + $this->strErrorDesc = 'Unprocessable Payload'; + $this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Payload'; + $this->strErrorMessage = $msg; + return $response; } } diff --git a/Model/ImageModel.php b/Model/ImageModel.php index 54e2db8..5f36289 100644 --- a/Model/ImageModel.php +++ b/Model/ImageModel.php @@ -20,6 +20,9 @@ class ImageModel extends Database { use ModelTraits; public $memberId; + public $imagePath; + public $imageName; + public $imageType; public $imageBlob; public $uploadTo; @@ -39,6 +42,9 @@ class ImageModel extends Database { /* Process the uploaded image and store in database */ public function insertImage() { + //$escaped_string = mysql_real_escape_string($this->imageBlob); + $escaped_string = addslashes($this->imageBlob); + //return var_dump($escaped_string); //$sql = "INSERT INTO ImageStore(ImageId,Image) VALUES('$this->image_id','" . mysql_escape_string(file_get_contents($tmp_image)) . "')"; //$fp=addslashes(file_get_contents($_FILES['image']['tmp_name'])); //will store the image to fp @@ -56,7 +62,7 @@ class ImageModel extends Database { SET blob_col=LOAD_FILE('/tmp/picture') WHERE id=1; */ - $query .= "CALL insert_loyalty_member_image_proc (" . $this->memberId . ", " . $this->imageBlob . ")"; + $query = "CALL insert_loyalty_member_image_proc (" . $this->memberId . ", '" . $escaped_string . "');"; //return var_dump($query);