1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2024-11-29 11:24:11 +01:00

More contact photo refactoring

This commit is contained in:
Thomas Tanghus 2014-03-20 16:00:22 +01:00
parent 1a7a7cbc80
commit eb7c143718
5 changed files with 91 additions and 70 deletions

View File

@ -76,11 +76,10 @@ class ContactPhotoController extends Controller {
* @return JSONResponse with data.tmp set to the key in the cache.
*
* @NoAdminRequired
* @NoCSRFRequired
*/
public function uploadPhoto() {
$params = $this->request->urlParams;
$response = new JSONResponse();
$tempPhoto = TemporaryPhoto::get(
$this->server,
@ -88,8 +87,6 @@ class ContactPhotoController extends Controller {
$this->request
);
$response = new JSONResponse();
return $response->setParams(array(
'tmp'=>$tempPhoto->getKey(),
'metadata' => array(
@ -139,7 +136,6 @@ class ContactPhotoController extends Controller {
*/
public function cacheFileSystemPhoto() {
$params = $this->request->urlParams;
$maxSize = isset($this->request->get['maxSize']) ? $this->request->get['maxSize'] : 400;
$response = new JSONResponse();
if(!isset($this->request->get['path'])) {

View File

@ -35,6 +35,10 @@ use OCA\Contacts\Controller,
*/
class Http extends Middleware {
public function __construct() {
\OCP\Util::writeLog('contacts', __METHOD__, \OCP\Util::DEBUG);
}
/**
* If an Exception is being caught, return a JSON error response with
* a suitable status code
@ -45,6 +49,7 @@ class Http extends Middleware {
* @return Response a Response object
*/
public function afterException($controller, $methodName, \Exception $exception) {
\OCP\Util::writeLog('contacts', __METHOD__.' method: '.$methodName, \OCP\Util::DEBUG);
// If there's no proper status code associated, set it to 500.
$response = new JSONResponse();
if($exception->getCode() < 100) {

View File

@ -22,6 +22,8 @@
namespace OCA\Contacts\Utils;
//use OCP\Image;
/**
* This class is used for getting a contact photo for cropping.
*/
@ -39,37 +41,37 @@ abstract class TemporaryPhoto {
protected $server;
/**
* @var OCP\Image
*/
* @var OCP\Image
*/
protected $image;
/**
* Cache key for temporary storage.
*
* @var string
*/
* Cache key for temporary storage.
*
* @var string
*/
protected $key;
/**
* Whether normalizePhoto() has already been called.
*
* @var bool
*/
* Whether normalizePhoto() has already been called.
*
* @var bool
*/
protected $normalized;
/**
* Whether the photo is cached.
*
* @var bool
*/
* Whether the photo is cached.
*
* @var bool
*/
protected $cached;
/**
* Photo loader classes.
*
* @var array
*/
/**
* Photo loader classes.
*
* @var array
*/
static public $classMap = array(
'OCA\\Contacts\\Utils\\TemporaryPhoto\\Contact',
'OCA\\Contacts\\Utils\\TemporaryPhoto\\FileSystem',
@ -77,20 +79,20 @@ abstract class TemporaryPhoto {
);
/**
* Always call parents ctor:
* parent::__construct($server);
*/
* Always call parents ctor:
* parent::__construct($server);
*/
public function __construct(\OCP\IServerContainer $server) {
$this->server = $server;
}
/**
* Returns an instance of a subclass of this class
*
* @param \OCP\IServerContainer $server
* @param int $type One of the pre-defined types.
* @param mixed $data Whatever data is needed to load the photo.
*/
* Returns an instance of a subclass of this class
*
* @param \OCP\IServerContainer $server
* @param int $type One of the pre-defined types.
* @param mixed $data Whatever data is needed to load the photo.
*/
public static function get(\OCP\IServerContainer $server, $type, $data) {
if (isset(self::$classMap[$type])) {
return new self::$classMap[$type]($server, $data);
@ -101,53 +103,53 @@ abstract class TemporaryPhoto {
}
/**
* Do what's needed to get the image from storage
* depending on the type.
* After this method is called $this->image must hold an
* instance of \OCP\Image.
*/
* Do what's needed to get the image from storage
* depending on the type.
* After this method is called $this->image must hold an
* instance of \OCP\Image.
*/
protected abstract function processImage();
/**
* Whether this image is valied
*
* @return bool
*/
* Whether this image is valied
*
* @return bool
*/
public function isValid() {
return (($this->image instanceof \OCP\Image) && $this->image->valid());
return (($this->image instanceof Image) && $this->image->valid());
}
/**
* Get the key to the cache image.
*
* @return string
*/
* Get the key to the cache image.
*
* @return string
*/
public function getKey() {
$this->cachePhoto();
return $this->key;
}
/**
* Get normalized image.
*
* @return \OCP\Image
*/
* Get normalized image.
*
* @return \OCP\Image
*/
public function getPhoto() {
$this->normalizePhoto();
return $this->image;
}
/**
* Save image data to cache and return the key
*
* @return string
*/
* Save image data to cache and return the key
*
* @return string
*/
private function cachePhoto() {
if ($this->cached) {
return;
}
if (!$this->image instanceof \OCP\Image) {
if (!$this->image instanceof Image) {
$this->processImage();
}
$this->normalizePhoto();
@ -158,8 +160,8 @@ abstract class TemporaryPhoto {
}
/**
* Resize and rotate the image if needed.
*/
* Resize and rotate the image if needed.
*/
private function normalizePhoto() {
if($this->normalized) {

View File

@ -23,7 +23,10 @@
namespace OCA\Contacts\Utils\TemporaryPhoto;
use OCA\Contacts\Contact as ContactObject,
OCA\Contacts\Utils\TemporaryPhoto as AbstractTemporaryPhoto;
OCA\Contacts\Utils\TemporaryPhoto as AbstractTemporaryPhoto,
OCP\IRequest,
OCP\AppFramework\Http;
/**
* This class loads an image from the virtual file system.
@ -37,9 +40,9 @@ class Uploaded extends AbstractTemporaryPhoto {
*/
protected $input;
public function __construct(\OCP\IServerContainer $server, \OCP\IRequest $request) {
public function __construct(\OCP\IServerContainer $server, IRequest $request) {
\OCP\Util::writeLog('contacts', __METHOD__, \OCP\Util::DEBUG);
if (!$request instanceOf \OCP\IRequest) {
if (!$request instanceOf IRequest) {
throw new \Exception(
__METHOD__ . ' Second argument must be an instance of \\OCP\\IRequest'
);
@ -54,10 +57,32 @@ class Uploaded extends AbstractTemporaryPhoto {
* Load the image.
*/
protected function processImage() {
// If image has already been read return
if ($this->image instanceOf \OCP\Image) {
return;
}
$this->image = new \OCP\Image();
\OCP\Util::writeLog('contacts', __METHOD__ . ', Content-Type: ' . $this->request->getHeader('Content-Type'), \OCP\Util::DEBUG);
\OCP\Util::writeLog('contacts', __METHOD__ . ', Content-Length: ' . $this->request->getHeader('Content-Length'), \OCP\Util::DEBUG);
if (substr($this->request->getHeader('Content-Type'), 0, 6) !== 'image/') {
throw new \Exception(
'Only images can be used as contact photo',
Http::STATUS_UNSUPPORTED_MEDIA_TYPE
);
}
$maxSize = \OCP\Util::maxUploadFilesize('/');
if ($this->request->getHeader('Content-Length') > $maxSize) {
throw new \Exception(
sprintf(
'The size of the file exceeds the maximum allowed %s',
\OCP\Util::humanFileSize($maxSize)
),
Http::STATUS_REQUEST_ENTITY_TOO_LARGE
);
}
$this->image->loadFromFileHandle($this->request->put);
}

View File

@ -117,10 +117,10 @@
</div>
</div>
</div>
<form class="float" id="file_upload_form" action="<?php print_unescaped(OCP\Util::linkTo('contacts', 'ajax/uploadphoto.php')); ?>" method="put" target="file_upload_target">
<form class="float" name="file_upload_form" id="file_upload_form" method="PUT" target="file_upload_target">
<input type="hidden" name="requesttoken" value="<?php p($_['requesttoken']) ?>">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php p($_['uploadMaxFilesize']) ?>" id="max_upload">
<input type="hidden" class="max_human_file_size" value="(max <?php p($_['uploadMaxHumanFilesize']); ?>)">
<input type="hidden" name="max_human_file_size" value="<?php p($_['uploadMaxHumanFilesize']); ?>">
<input id="contactphoto_fileupload" type="file" accept="image/*" name="imagefile" />
</form>
<iframe name="file_upload_target" id='file_upload_target' src=""></iframe>
@ -131,13 +131,7 @@
class="coords"
method="post"
enctype="multipart/form-data"
target="crop_target"
action="{action}"
>
<input type="hidden" id="contactid" name="contactid" value="{contactid}" />
<input type="hidden" id="addressbookid" name="addressbookid" value="{addressbookid}" />
<input type="hidden" id="backend" name="backend" value="{backend}" />
<input type="hidden" id="tmpkey" name="tmpkey" value="{tmpkey}" />
<fieldset id="coords">
<input type="hidden" id="x" name="x" value="" />
<input type="hidden" id="y" name="y" value="" />
@ -145,7 +139,6 @@
<input type="hidden" id="h" name="h" value="" />
</fieldset>
</form>
<iframe name="crop_target" id="crop_target" src=""></iframe>
</script>
<script id="addGroupTemplate" type="text/template">