ShowImageController update to add comments and remove unnecessary code

This commit is contained in:
Duncan Sommerville 2015-02-09 12:15:19 -05:00
parent 2cf0f7da7a
commit a1436bfebb

View file

@ -1,25 +1,23 @@
<?php
/**
*
* Controller class for handling Show-related functionality.
*
* Controller class for handling ShowImage-related functionality.
* Changelog:
* 16/09/2014 : v1.0 Created class skeleton, added image upload functionality
* 18/09/2014 : v1.1 Changed auth references to static calls
*
* 06/02/2015 : v1.2 Changed endpoints to be more RESTful, changed classname to
* better reflect functionality
* 09/02/2015 : v1.2.1 Added more comments
* @author sourcefabric
* @version 1.1
*
* @version 1.2.1
*/
$filepath = realpath(__DIR__);
require_once($filepath."/../helpers/RestAuth.php");
require_once($filepath . "/../helpers/RestAuth.php");
class Rest_ShowImageController extends Zend_Rest_Controller
{
public function init()
{
class Rest_ShowImageController extends Zend_Rest_Controller {
public function init() {
// Remove layout dependencies
$this->view->layout()->disableLayout();
// Remove reliance on .phtml files to render requests
@ -38,9 +36,11 @@ class Rest_ShowImageController extends Zend_Rest_Controller
Logging::info("PUT action received");
}
/**
* RESTful POST endpoint; used when uploading show images
*/
public function postAction() {
if (!RestAuth::verifyAuth(true, true))
{
if (!RestAuth::verifyAuth(true, true)) {
$this->getResponse()
->setHttpResponseCode(401)
->appendBody("Authentication failed");
@ -57,7 +57,7 @@ class Rest_ShowImageController extends Zend_Rest_Controller
}
try {
$path = $this->processUploadedImage($showId, $_FILES["file"]["tmp_name"], $_FILES["file"]["name"]);
$path = $this->processUploadedImage($showId, $_FILES["file"]["tmp_name"]);
} catch (Exception $e) {
$this->getResponse()
->setHttpResponseCode(500)
@ -85,9 +85,11 @@ class Rest_ShowImageController extends Zend_Rest_Controller
->setHttpResponseCode(201);
}
/**
* RESTful DELETE endpoint; used when deleting show images
*/
public function deleteAction() {
if (!RestAuth::verifyAuth(true, true))
{
if (!RestAuth::verifyAuth(true, true)) {
$this->getResponse()
->setHttpResponseCode(401)
->appendBody("Authentication failed");
@ -132,41 +134,26 @@ class Rest_ShowImageController extends Zend_Rest_Controller
->setHttpResponseCode(201);
}
public function uploadImageAction()
{
}
public function deleteImageAction()
{
}
/**
* Verify and process an uploaded image file, copying it into
* .../stor/imported/:owner-id/show-images/:show-id/ to differentiate between
* individual users and shows
*
* @param unknown $tempFilePath
* - temporary filepath assigned to the upload generally of the form /tmp/:tmp_name
* @param unknown
* - $originalFilename the file name at time of upload
* @param int $showId the ID of the show we're adding the image to
* @param string $tempFilePath temporary filepath assigned to the upload generally of the form /tmp/:tmp_name
*
* @throws Exception
* - when a file with an unsupported file extension is uploaded or an
* error occurs in copyFileToStor
* @return string the path to the new location for the file
*/
private function processUploadedImage($showId, $tempFilePath, $originalFilename)
{
private function processUploadedImage($showId, $tempFilePath) {
$ownerId = RestAuth::getOwnerId();
$CC_CONFIG = Config::getConfig();
$apiKey = $CC_CONFIG["apiKey"][0];
$tempFileName = basename($tempFilePath);
//Only accept files with a file extension that we support.
$fileExtension = $this->getFileExtension($originalFilename, $tempFilePath);
$fileExtension = $this->getFileExtension($tempFilePath);
if (!in_array(strtolower($fileExtension), explode(",", "jpg,png,gif,jpeg")))
{
if (!in_array(strtolower($fileExtension), explode(",", "jpg,png,gif,jpeg"))) {
@unlink($tempFilePath);
throw new Exception("Bad file extension.");
}
@ -184,16 +171,28 @@ class Rest_ShowImageController extends Zend_Rest_Controller
return $importedStorageDirectory;
}
private function getFileExtension($originalFileName, $tempFilePath)
{
/**
* Check the MIME type of an uploaded file to determine what extension it should have
*
* @param $tempFilePath the file path to the uploaded file in /tmp
*
* @return string the file extension for the new file based on its MIME type
*/
private function getFileExtension($tempFilePath) {
// Don't trust the extension - get the MIME-type instead
$fileInfo = finfo_open();
$mime = finfo_file($fileInfo, $tempFilePath, FILEINFO_MIME_TYPE);
return $this->getExtensionFromMime($mime);
}
private function getExtensionFromMime($mime)
{
/**
* Use a hardcoded list of accepted MIME types to return a file extension
*
* @param $mime the MIME type of the file
*
* @return string the file extension based on the given MIME type
*/
private function getExtensionFromMime($mime) {
$extensions = array(
'image/jpeg' => 'jpg',
'image/png' => 'png',
@ -203,8 +202,20 @@ class Rest_ShowImageController extends Zend_Rest_Controller
return $extensions[$mime];
}
private function copyFileToStor($tempFilePath, $importedStorageDirectory, $fileExtension)
{
/**
* Copy a given file in /tmp to the user's stor directory
*
* @param string $tempFilePath the path to the file in /tmp
* @param string $importedStorageDirectory the path to the new location for the file
* @param string $fileExtension the file's extension based on its MIME type
*
* @return string the new full path to the file in stor
* @throws Exception if either the storage directory does not exist and cannot be
* created, the storage directory does not have write permissions
* enabled, or the user's hard drive does not have enough space to
* store the file
*/
private function copyFileToStor($tempFilePath, $importedStorageDirectory, $fileExtension) {
$image_file = $tempFilePath;
// check if show image dir exists and if not, create one
@ -218,7 +229,7 @@ class Rest_ShowImageController extends Zend_Rest_Controller
Logging::info("Warning: couldn't change permissions of $image_file to 0644");
}
$newFileName = substr($tempFilePath, strrpos($tempFilePath, "/")).".".$fileExtension;
$newFileName = substr($tempFilePath, strrpos($tempFilePath, "/")) . "." . $fileExtension;
// Did all the checks for real, now trying to copy
$image_stor = Application_Common_OsPath::join($importedStorageDirectory, $newFileName);
@ -233,14 +244,21 @@ class Rest_ShowImageController extends Zend_Rest_Controller
unlink($image_file); //remove the file after failed rename
throw new Exception("The file was not uploaded, this error can occur if the computer "
."hard drive does not have enough disk space or the stor "
."directory does not have correct write permissions.");
. "hard drive does not have enough disk space or the stor "
. "directory does not have correct write permissions.");
}
return $image_stor;
}
// Should this be an endpoint instead?
/**
* Delete any images belonging to the show with the given ID
*
* @param int $showId the ID of the show we're deleting images from
*
* @return bool true if the images were successfully deleted, otherwise false
*/
public static function deleteShowImagesFromStor($showId) {
$ownerId = RestAuth::getOwnerId();
@ -260,7 +278,7 @@ class Rest_ShowImageController extends Zend_Rest_Controller
// from a note @ http://php.net/manual/en/function.rmdir.php
private static function delTree($dir) {
$files = array_diff(scandir($dir), array('.','..'));
$files = array_diff(scandir($dir), array('.', '..'));
foreach ($files as $file) {
(is_dir("$dir/$file")) ? delTree("$dir/$file") : unlink("$dir/$file");
}
@ -269,12 +287,10 @@ class Rest_ShowImageController extends Zend_Rest_Controller
/**
* Fetch the id parameter from the request.
*
* @return boolean|unknown false if the show id wasn't
* @return boolean|int false if the show id wasn't
* provided, otherwise returns the id
*/
private function getShowId()
{
private function getShowId() {
if (!$id = $this->_getParam('id', false)) {
$resp = $this->getResponse();
$resp->setHttpResponseCode(400);