Added delete images endpoint to ShowController

This commit is contained in:
Duncan Sommerville 2014-09-18 16:30:03 -04:00
parent ee92f41a8d
commit 28824219f8
2 changed files with 74 additions and 10 deletions

View File

@ -48,5 +48,18 @@ class Rest_Bootstrap extends Zend_Application_Module_Bootstrap
) )
); );
$router->addRoute('upload-image', $uploadImageRoute); $router->addRoute('upload-image', $uploadImageRoute);
$deleteImageRoute = new Zend_Controller_Router_Route(
'rest/show/:id/delete-image',
array(
'controller' => 'show',
'action' => 'delete-image',
'module' => 'rest'
),
array(
'id' => '\d+'
)
);
$router->addRoute('delete-image', $deleteImageRoute);
} }
} }

View File

@ -13,7 +13,7 @@
* *
*/ */
$filepath = realpath(dirname(__FILE__)); $filepath = realpath(__DIR__);
require_once($filepath."/../helpers/RestAuth.php"); require_once($filepath."/../helpers/RestAuth.php");
class Rest_ShowController extends Zend_Rest_Controller class Rest_ShowController extends Zend_Rest_Controller
@ -55,20 +55,24 @@ class Rest_ShowController extends Zend_Rest_Controller
{ {
if (!RestAuth::verifyAuth(true, true)) if (!RestAuth::verifyAuth(true, true))
{ {
Logging::info("Authentication failed"); $this->getResponse()
->setHttpResponseCode(401)
->appendBody("Authentication failed");
return; return;
} }
$showId = $this->getShowId(); $showId = $this->getShowId();
if (!$showId) { if (!$showId) {
Logging::info("No show id provided"); $this->getResponse()
->setHttpResponseCode(400)
->appendBody("No show ID provided");
return; return;
} }
if (Application_Model_Systemstatus::isDiskOverQuota()) { if (Application_Model_Systemstatus::isDiskOverQuota()) {
$this->getResponse() $this->getResponse()
->setHttpResponseCode(400) ->setHttpResponseCode(413)
->appendBody("ERROR: Disk Quota reached."); ->appendBody("ERROR: Disk Quota reached.");
return; return;
} }
@ -77,7 +81,7 @@ class Rest_ShowController extends Zend_Rest_Controller
$path = $this->processUploadedImage($showId, $_FILES["file"]["tmp_name"], $_FILES["file"]["name"]); $path = $this->processUploadedImage($showId, $_FILES["file"]["tmp_name"], $_FILES["file"]["name"]);
} catch (Exception $e) { } catch (Exception $e) {
$this->getResponse() $this->getResponse()
->setHttpResponseCode(400) ->setHttpResponseCode(500)
->appendBody("Error processing image: " . $e->getMessage()); ->appendBody("Error processing image: " . $e->getMessage());
} }
@ -88,14 +92,13 @@ class Rest_ShowController extends Zend_Rest_Controller
$con->beginTransaction(); $con->beginTransaction();
$show->setDbImagePath($path); $show->setDbImagePath($path);
// TODO set linked show image paths
$show->save(); $show->save();
$con->commit(); $con->commit();
} catch (Exception $e) { } catch (Exception $e) {
$con->rollBack(); $con->rollBack();
$this->getResponse() $this->getResponse()
->setHttpResponseCode(400) ->setHttpResponseCode(500)
->appendBody("Couldn't add show image: " . $e->getMessage()); ->appendBody("Couldn't add show image: " . $e->getMessage());
} }
@ -103,6 +106,54 @@ class Rest_ShowController extends Zend_Rest_Controller
->setHttpResponseCode(201); ->setHttpResponseCode(201);
} }
public function deleteImageAction()
{
if (!RestAuth::verifyAuth(true, true))
{
$this->getResponse()
->setHttpResponseCode(401)
->appendBody("Authentication failed");
return;
}
$showId = $this->getShowId();
if (!$showId) {
$this->getResponse()
->setHttpResponseCode(400)
->appendBody("No show ID provided");
return;
}
try {
Rest_ShowController::deleteShowImagesFromStor($showId);
} catch (Exception $e) {
$this->getResponse()
->setHttpResponseCode(500)
->appendBody("Error processing image: " . $e->getMessage());
}
$show = CcShowQuery::create()->findPk($showId);
try {
$con = Propel::getConnection();
$con->beginTransaction();
$show->setDbImagePath(null);
$show->save();
$con->commit();
} catch (Exception $e) {
$con->rollBack();
$this->getResponse()
->setHttpResponseCode(500)
->appendBody("Couldn't remove show image: " . $e->getMessage());
}
$this->getResponse()
->setHttpResponseCode(201);
}
/** /**
* Verify and process an uploaded image file, copying it first into .../stor/organize/ * Verify and process an uploaded image file, copying it first into .../stor/organize/
* and then to RabbitMq to process. Processed image files end up in * and then to RabbitMq to process. Processed image files end up in
@ -124,7 +175,7 @@ class Rest_ShowController extends Zend_Rest_Controller
$tempFileName = basename($tempFilePath); $tempFileName = basename($tempFilePath);
//Only accept files with a file extension that we support. //Only accept files with a file extension that we support.
$fileExtension = $this->getMimeExtension($originalFileName, $tempFilePath); $fileExtension = $this->getFileExtension($originalFilename, $tempFilePath);
if (!in_array(strtolower($fileExtension), explode(",", "jpg,png,gif,jpeg"))) if (!in_array(strtolower($fileExtension), explode(",", "jpg,png,gif,jpeg")))
{ {
@ -145,7 +196,7 @@ class Rest_ShowController extends Zend_Rest_Controller
return $importedStorageDirectory; return $importedStorageDirectory;
} }
private function getMimeExtension($originalFileName, $tempFilePath) private function getFileExtension($originalFileName, $tempFilePath)
{ {
// Don't trust the extension - get the MIME-type instead // Don't trust the extension - get the MIME-type instead
$fileInfo = finfo_open(); $fileInfo = finfo_open();
@ -201,7 +252,7 @@ class Rest_ShowController extends Zend_Rest_Controller
return $image_stor; return $image_stor;
} }
// Should this be a POST endpoint instead? // Should this be an endpoint instead?
public static function deleteShowImagesFromStor($showId) { public static function deleteShowImagesFromStor($showId) {
$ownerId = RestAuth::getOwnerId(); $ownerId = RestAuth::getOwnerId();