CustomerRewardsRESTAPI/Controller/API/ImageController.php
2024-05-14 20:11:04 -05:00

119 lines
3.9 KiB
PHP

<?php
header("Content-Type: application/json");
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST");
require_once PD . "/Model/ImageModel.php";
/**
* Description of ImageController
*
* @author Mike Howard
*/
class ImageController extends BaseController {
public $imageModel;
public $action;
private $imagePayload;
function __construct() {
parent::__construct();
$this->basename = "ImageController";
}
/*
* http://localhost/CustomerRewardsRESTAPI/index.php/image/process/upload
*/
public function processAction() {
$this->strErrorDesc = '';
$this->strErrorHeader = '';
try {
$this->requestMethod = $this->getServerRequestMethod();
$this->imageModel = new ImageModel();
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)
);
}
}
/* Upload the image and store on server as file */
private function uploadImage() {
if ($this->checkRequestType('GET') == 'false') {
return $this->unprocessableRequestResponse();
}
return var_dump(file_get_contents('php://input'));
$this->imagePayload = file_get_contents($_FILES['image']['tmp_name']);
$this->imageModel->fileName = $_FILES['image']['name'];
if(empty($this->imageModel->fileName)){
return $this->notFoundResponse();
}
$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();
}
if (! $this->handleImage()) {
return $this->notFoundResponse();
}
$response = $this->imageModel->insertImage();
return $response;
}
private function handleImage() {
$moved = false;
if(!move_uploaded_file($this->imageModel->tempPath, $this->imageModel->originalPath)) {
$moved = true;
}
return $moved;
}
private function validateImage(){
$validtion = false;
if(in_array($this->imageModel->fileType, $this->imageModel->allowFileType)){
$validtion = true;
}
return $validtion;
}
}