Continued work on image upload

This commit is contained in:
sctn4elk 2024-05-14 11:38:57 -05:00
parent 3531f71a0c
commit cf1fb1bc63
6 changed files with 69 additions and 91 deletions

View File

@ -35,6 +35,17 @@ class BaseController {
return $response; 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() public function unprocessableEntityResponse()
{ {
$response['status_code_header'] = 'HTTP/1.1 422 Unprocessable Payload'; $response['status_code_header'] = 'HTTP/1.1 422 Unprocessable Payload';

View File

@ -1,6 +1,8 @@
<?php <?php
header("Content-Type: application/json"); header("Content-Type: application/json");
//require_once PROJECT_ROOT_PATH . "/Model/CustomerModel.php"; define('PD', 'D:\DEV\Git Repository\CustomerRewardsRESTAPI');
require_once PD . "/Model/CustomerModel.php";
/** /**
* Description of CustomerController * Description of CustomerController
* *
@ -77,7 +79,6 @@ class CustomerController extends BaseController {
return; return;
} }
require_once PROJECT_ROOT_PATH . "/Model/CustomerModel.php";
$this->customerModel = new CustomerModel(); $this->customerModel = new CustomerModel();
//return var_dump($this->customerModel); //return var_dump($this->customerModel);
$this->arrQueryStringParams = $this->getQueryStringParams(); $this->arrQueryStringParams = $this->getQueryStringParams();

View File

@ -1,5 +1,10 @@
<?php <?php
header("Content-Type: application/json"); header("Content-Type: application/json");
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST");
define('PD', 'D:\DEV\Git Repository\CustomerRewardsRESTAPI');
require_once PD . "/Model/ImageModel.php";
/** /**
* Description of ImageController * Description of ImageController
@ -10,6 +15,7 @@ class ImageController extends BaseController {
public $imageModel; public $imageModel;
public $action; public $action;
private $imagePayload;
function __construct() { function __construct() {
parent::__construct(); parent::__construct();
@ -24,7 +30,8 @@ class ImageController extends BaseController {
$this->strErrorHeader = ''; $this->strErrorHeader = '';
try { try {
$this->requestMethod = $this->getServerRequestMethod(); $this->requestMethod = $this->getServerRequestMethod();
$this->imageModel = new ImageModel();
switch($this->action) { switch($this->action) {
case "upload": case "upload":
$response = $this->uploadImage(); $response = $this->uploadImage();
@ -64,85 +71,47 @@ class ImageController extends BaseController {
} }
} }
/* Upload the image and store on server as file */
private function uploadImage(){ private function uploadImage(){
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST");
header("Content-Type: application/json");
if ($this->checkRequestType('POST') == 'false') { if ($this->checkRequestType('POST') == 'false') {
$this->strErrorDesc = 'Request Method not supported for processAction'; return $this->unprocessableRequestResponse();
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request';
return;
} }
$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. $this->imageModel->tempPath = $_FILES["image"]["tmp_name"];
//base64_decode($_POST['content']); $this->imageModel->basename = basename($this->imageModel->fileName);
$imagePayload = file_get_contents($_FILES['your_image_name']['tmp_name']); $this->imageModel->originalPath = $this->imageModel->uploadTo.$this->imageModel->basename;
if (! $this->validateImage($imagePayload)) { $this->imageModel->fileType = pathinfo($this->imageModel->originalPath, PATHINFO_EXTENSION);
if (! $this->validateImage()) {
return $this->unprocessableEntityResponse(); return $this->unprocessableEntityResponse();
} }
$response = $this->insertImage(); if (! $this->handleImage()) {
return $this->notFoundResponse();
}
$response = $this->imageModel->insertImage();
return $response; return $response;
} }
private function insertImage() { private function handleImage() {
if (isset($_FILES['image'])) { $moved = false;
$uploadImage = $this->processImage(); if(!move_uploaded_file($this->imageModel->tempPath, $this->imageModel->originalPath)) {
$success = false; $moved = true;
if (!$uploadImage['error']) {
}
} }
return $moved;
} }
private function processImage() { private function validateImage(){
$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){
$validtion = false; $validtion = false;
if($imageData['customer_name_first'] != null){ if(in_array($this->imageModel->fileType, $this->imageModel->allowFileType)){
$validtion = true; $validtion = true;
if($imageData['customer_name_last'] == null) {
$validtion = false;
}
} }
return $validtion; return $validtion;
} }

View File

@ -25,10 +25,10 @@
define('PD', 'D:\DEV\Git Repository\CustomerRewardsRESTAPI'); define('PD', 'D:\DEV\Git Repository\CustomerRewardsRESTAPI');
//require_once PROJECT_ROOT_PATH . "/Model/Database.php"; //require_once PROJECT_ROOT_PATH . "/Model/Database.php";
require_once PD . "/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 { class CustomerModel extends Database {
//use ModelTraits; use ModelTraits;
public function findAllCustomers() public function findAllCustomers()
{ {

View File

@ -6,6 +6,7 @@
*/ */
class Database { class Database {
protected $connection = null; protected $connection = null;
public function __construct() public function __construct()
{ {
try { try {
@ -41,23 +42,6 @@ class Database {
return false; 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 = "") public function processStatement($query = "")
{ {
try { try {

View File

@ -1,10 +1,5 @@
<?php <?php
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
*/
/** /**
* Description of ImageModel * Description of ImageModel
* *
@ -19,10 +14,28 @@
* loyalty_value_blob blob * loyalty_value_blob blob
*/ */
define('PD', 'D:\DEV\Git Repository\CustomerRewardsRESTAPI');
require_once PD . "/Model/Database.php";
require_once PD . "/Model/ModelTraits.php";
class ImageModel extends Database { class ImageModel extends Database {
use ModelTraits; use ModelTraits;
public function insertImage($inputModel) public $uploadTo;
public $allowFileType;
public $fileName;
public $tempPath;
public $basename;
public $originalPath;
public $fileType;
function __construct() {
$this->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); //return var_dump($jsonPayLoad);
$keys = array_keys($inputModel); $keys = array_keys($inputModel);