mirror of
https://github.com/sctn4elk/CustomerRewardsRESTAPI.git
synced 2025-01-10 00:04:28 -06:00
Work on storing image to database
This commit is contained in:
parent
35c165fbf9
commit
9460fac030
|
@ -47,12 +47,12 @@ class BaseController {
|
||||||
*/
|
*/
|
||||||
public function unprocessableRequestResponse($msg)
|
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([
|
$response['body'] = json_encode([
|
||||||
'error' => 'Invalid request'
|
'error' => 'Invalid request'
|
||||||
]);
|
]);
|
||||||
$this->strErrorDesc = 'Request Method not supported for processAction';
|
$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;
|
$this->strErrorMessage = $msg;
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
|
@ -75,13 +75,21 @@ class ImageController extends BaseController {
|
||||||
return $this->unprocessableRequestResponse();
|
return $this->unprocessableRequestResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
// reads the raw POST data and returns it as a string.
|
/*
|
||||||
$jsonPayload = file_get_contents('php://input');
|
* array(1) {
|
||||||
$input = json_decode($jsonPayload, TRUE);
|
["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
|
//get the member data
|
||||||
$this->memberId = $input['CustomerID'];
|
$this->imageModel->memberId = $input['CustomerID'];
|
||||||
$this->imageBlob = $input['ImageBlob'];
|
$this->imageModel->imagePath = $input['ImagePath'];
|
||||||
|
$this->imageModel->imageName = $input['ImageName'];
|
||||||
|
$this->imageModel->imageType = $input['ImageType'];
|
||||||
|
|
||||||
//get the file data
|
//get the file data
|
||||||
$this->imagePayload = $_FILES['image'];
|
$this->imagePayload = $_FILES['image'];
|
||||||
|
@ -95,6 +103,12 @@ class ImageController extends BaseController {
|
||||||
$this->imageModel->basename = basename($this->imageModel->fileName);
|
$this->imageModel->basename = basename($this->imageModel->fileName);
|
||||||
$this->imageModel->originalPath = $this->imageModel->uploadTo.$this->imageModel->basename;
|
$this->imageModel->originalPath = $this->imageModel->uploadTo.$this->imageModel->basename;
|
||||||
$this->imageModel->fileType = pathinfo($this->imageModel->originalPath, PATHINFO_EXTENSION);
|
$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()) {
|
if (! $this->validateImage()) {
|
||||||
return $this->unprocessableEntityResponse();
|
return $this->unprocessableEntityResponse();
|
||||||
|
@ -104,7 +118,7 @@ class ImageController extends BaseController {
|
||||||
return $this->notFoundResponse();
|
return $this->notFoundResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->imageModel->insertImage();
|
$response = $this->imageModel->insertImage();
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -116,6 +130,44 @@ class ImageController extends BaseController {
|
||||||
return $moved;
|
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, '<span style="color: red;">http://extserver.com/handle.php</span>');
|
||||||
|
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('<span style="color: red;">subins</span>', $decoded_file);
|
||||||
|
*/
|
||||||
|
|
||||||
|
return $check;
|
||||||
|
}
|
||||||
|
|
||||||
private function validateImage(){
|
private function validateImage(){
|
||||||
$validtion = false;
|
$validtion = false;
|
||||||
if(in_array($this->imageModel->fileType, $this->imageModel->allowFileType)){
|
if(in_array($this->imageModel->fileType, $this->imageModel->allowFileType)){
|
||||||
|
|
|
@ -6,8 +6,8 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
define("PROJECT_ROOT_PATH", __DIR__ . "/../");
|
define("PROJECT_ROOT_PATH", __DIR__ . "/../");
|
||||||
define('PD', 'D:\Programs\UniformServer\UniServerZ\www\CustomerRewardsRESTAPI');
|
//define('PD', 'D:\Programs\UniformServer\UniServerZ\www\CustomerRewardsRESTAPI');
|
||||||
//define('PD', 'C:\DEV\UniServerZ\www\CustomerRewardsRESTAPI');
|
define('PD', 'C:\DEV\UniServerZ\www\CustomerRewardsRESTAPI');
|
||||||
|
|
||||||
// include main configuration file
|
// include main configuration file
|
||||||
require_once PROJECT_ROOT_PATH . "/include/config.php";
|
require_once PROJECT_ROOT_PATH . "/include/config.php";
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
require_once PD . "/Model/Database.php";
|
||||||
|
require_once PD . "/Model/ModelTraits.php";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Description of CustomerModel
|
* Description of CustomerModel
|
||||||
|
@ -23,9 +25,6 @@
|
||||||
* address_zip varchar(10)
|
* address_zip varchar(10)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
require_once PD . "/Model/Database.php";
|
|
||||||
require_once PD . "/Model/ModelTraits.php";
|
|
||||||
|
|
||||||
class CustomerModel extends Database {
|
class CustomerModel extends Database {
|
||||||
use ModelTraits;
|
use ModelTraits;
|
||||||
|
|
||||||
|
|
|
@ -71,30 +71,55 @@ class Database {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function processImage($query = "") {
|
public function processImage($query = "") {
|
||||||
try {
|
try
|
||||||
$stmt = $this->connection->prepare($query);
|
{
|
||||||
if($stmt === false) {
|
if($this->connection == null)
|
||||||
throw New Exception("Unable to prepare the statement: " . $query);
|
{
|
||||||
|
$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']);
|
//$stmt->bind_param("ss", $params['filename'], $params['filepath']);
|
||||||
|
$result = $this->connection->execute_query($query);
|
||||||
$result = $stmt->execute();
|
//$result = $stmt->execute();
|
||||||
if($result === false) {
|
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;
|
$rowCount = $this->connection->affected_rows;
|
||||||
if($rowCount < 1)
|
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();
|
$stmt->close();
|
||||||
return $rowCount;
|
return $rowCount;
|
||||||
} catch(Exception $e) {
|
} 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,9 @@ class ImageModel extends Database {
|
||||||
use ModelTraits;
|
use ModelTraits;
|
||||||
|
|
||||||
public $memberId;
|
public $memberId;
|
||||||
|
public $imagePath;
|
||||||
|
public $imageName;
|
||||||
|
public $imageType;
|
||||||
public $imageBlob;
|
public $imageBlob;
|
||||||
|
|
||||||
public $uploadTo;
|
public $uploadTo;
|
||||||
|
@ -39,6 +42,9 @@ class ImageModel extends Database {
|
||||||
/* Process the uploaded image and store in database */
|
/* Process the uploaded image and store in database */
|
||||||
public function insertImage()
|
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)) . "')";
|
//$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
|
//$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')
|
SET blob_col=LOAD_FILE('/tmp/picture')
|
||||||
WHERE id=1;
|
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);
|
//return var_dump($query);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user