CustomerRewardsRESTAPI/Controller/API/ImageController.php

127 lines
4.1 KiB
PHP

<?php
header("Content-Type: multipart/form-data");
header("Access-Control-Allow-Origin: *");
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('POST') == 'false') {
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);
//get the member data
$this->memberId = $input['CustomerID'];
$this->imageBlob = $input['ImageBlob'];
//get the file data
$this->imagePayload = $_FILES['image'];
$this->imageModel->fileName = $this->imagePayload['name'];
if(empty($this->imageModel->fileName)){
return $this->notFoundResponse();
}
$this->imageModel->tempPath = $this->imagePayload['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();
}
$this->imageModel->insertImage();
return $response;
}
private function handleImage() {
$moved = false;
if(move_uploaded_file($this->imageModel->tempPath, $this->imageModel->uploadTo . $this->imageModel->fileName)) {
$moved = true;
}
return $moved;
}
private function validateImage(){
$validtion = false;
if(in_array($this->imageModel->fileType, $this->imageModel->allowFileType)){
$validtion = true;
}
return $validtion;
}
}