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;
}
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';

View File

@ -1,6 +1,8 @@
<?php
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
*
@ -77,7 +79,6 @@ class CustomerController extends BaseController {
return;
}
require_once PROJECT_ROOT_PATH . "/Model/CustomerModel.php";
$this->customerModel = new CustomerModel();
//return var_dump($this->customerModel);
$this->arrQueryStringParams = $this->getQueryStringParams();

View File

@ -1,5 +1,10 @@
<?php
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
@ -10,6 +15,7 @@ class ImageController extends BaseController {
public $imageModel;
public $action;
private $imagePayload;
function __construct() {
parent::__construct();
@ -24,7 +30,8 @@ class ImageController extends BaseController {
$this->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;
}

View File

@ -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()
{

View File

@ -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 {

View File

@ -1,10 +1,5 @@
<?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
*
@ -19,10 +14,28 @@
* 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 {
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);
$keys = array_keys($inputModel);