1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2024-12-11 22:24:32 +01:00
OwncloudContactsOfficial/lib/controller/contactphotocontroller.php
2014-03-21 15:19:32 +01:00

231 lines
5.9 KiB
PHP

<?php
/**
* @author Thomas Tanghus
* @copyright 2013-2014 Thomas Tanghus (thomas@tanghus.net)
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace OCA\Contacts\Controller;
use OCA\Contacts\App,
OCA\Contacts\JSONResponse,
OCA\Contacts\ImageResponse,
OCA\Contacts\Utils\Properties,
OCA\Contacts\Utils\TemporaryPhoto,
OCA\Contacts\Controller;
/**
* Controller class For Contacts
*/
class ContactPhotoController extends Controller {
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function getPhoto($maxSize = 170) {
// TODO: Cache resized photo
$params = $this->request->urlParams;
$etag = null;
$addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']);
$contact = $addressBook->getChild($params['contactId']);
$tempPhoto = TemporaryPhoto::create(
$this->server,
TemporaryPhoto::PHOTO_CURRENT,
$contact
);
$image = $tempPhoto->getPhoto();
if($image->valid()) {
$response = new ImageResponse($image);
$lastModified = $contact->lastModified();
// Force refresh if modified within the last minute.
if(!is_null($lastModified)) {
$response->setLastModified(\DateTime::createFromFormat('U', $lastModified) ?: null);
}
if(!is_null($etag)) {
$response->setETag($etag);
}
if ($image->width() > $maxSize || $image->height() > $maxSize) {
$image->resize($maxSize);
}
return $response;
} else {
throw new \Exception(App::$l10n->t('Error getting user photo'));
}
}
/**
* Uploads a photo and saves in oC cache
* @return JSONResponse with data.tmp set to the key in the cache.
*
* @NoAdminRequired
*/
public function uploadPhoto() {
$params = $this->request->urlParams;
$response = new JSONResponse();
$tempPhoto = TemporaryPhoto::create(
$this->server,
TemporaryPhoto::PHOTO_UPLOADED,
$this->request
);
return $response->setParams(array(
'tmp'=>$tempPhoto->getKey(),
'metadata' => array(
'contactId'=> $params['contactId'],
'addressBookId'=> $params['addressBookId'],
'backend'=> $params['backend'],
),
));
}
/**
* Saves the photo from the contact being edited to oC cache
* @return JSONResponse with data.tmp set to the key in the cache.
*
* @NoAdminRequired
* @NoCSRFRequired
*/
public function cacheCurrentPhoto() {
$params = $this->request->urlParams;
$response = new JSONResponse();
$addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']);
$contact = $addressBook->getChild($params['contactId']);
$tempPhoto = TemporaryPhoto::create(
$this->server,
TemporaryPhoto::PHOTO_CURRENT,
$contact
);
return $response->setParams(array(
'tmp'=>$tempPhoto->getKey(),
'metadata' => array(
'contactId'=> $params['contactId'],
'addressBookId'=> $params['addressBookId'],
'backend'=> $params['backend'],
),
));
}
/**
* Saves the photo from ownCloud FS to oC cache
* @return JSONResponse with data.tmp set to the key in the cache.
*
* @NoAdminRequired
* @NoCSRFRequired
*/
public function cacheFileSystemPhoto() {
$params = $this->request->urlParams;
$response = new JSONResponse();
if(!isset($this->request->get['path'])) {
$response->bailOut(App::$l10n->t('No photo path was submitted.'));
}
$tempPhoto = TemporaryPhoto::create(
$this->server,
TemporaryPhoto::PHOTO_FILESYSTEM,
$this->request->get['path']
);
return $response->setParams(array(
'tmp'=>$tempPhoto->getKey(),
'metadata' => array(
'contactId'=> $params['contactId'],
'addressBookId'=> $params['addressBookId'],
'backend'=> $params['backend'],
),
));
}
/**
* Get a photo from the oC cache for cropping.
* @NoAdminRequired
* @NoCSRFRequired
*/
public function getTempPhoto() {
$params = $this->request->urlParams;
$tmpkey = $params['key'];
$tmpPhoto = new TemporaryPhoto($this->server, $tmpkey);
$image = $tmpPhoto->getPhoto();
if($image->valid()) {
$response = new ImageResponse($image);
return $response;
} else {
$response = new JSONResponse();
return $response->bailOut('Error getting temporary photo');
}
}
/**
* Get a photo from the oC and crops it with the suplied geometry.
* @NoAdminRequired
* @NoCSRFRequired
*/
public function cropPhoto() {
$params = $this->request->urlParams;
$x = (isset($this->request->post['x']) && $this->request->post['x']) ? $this->request->post['x'] : 0;
$y = (isset($this->request->post['y']) && $this->request->post['y']) ? $this->request->post['y'] : 0;
$w = (isset($this->request->post['w']) && $this->request->post['w']) ? $this->request->post['w'] : -1;
$h = (isset($this->request->post['h']) && $this->request->post['h']) ? $this->request->post['h'] : -1;
$tmpkey = $params['key'];
$app = new App($this->api->getUserId());
$addressBook = $app->getAddressBook($params['backend'], $params['addressBookId']);
$contact = $addressBook->getChild($params['contactId']);
$response = new JSONResponse();
$tmpPhoto = new TemporaryPhoto($this->server, $tmpkey);
$image = $tmpPhoto->getPhoto();
if(!$image || !$image->valid()) {
return $response->bailOut(App::$l10n->t('Error loading image from cache'));
}
$w = ($w !== -1 ? $w : $image->width());
$h = ($h !== -1 ? $h : $image->height());
$image->crop($x, $y, $w, $h);
if (!$contact->setPhoto($image)) {
$tmpPhoto->remove($tmpkey);
return $response->bailOut(App::$l10n->t('Error getting PHOTO property.'));
}
if(!$contact->save()) {
return $response->bailOut(App::$l10n->t('Error saving contact.'));
}
$thumbnail = Properties::cacheThumbnail(
$params['backend'],
$params['addressBookId'],
$params['contactId'],
$image,
$contact
);
$response->setData(array(
'status' => 'success',
'data' => array(
'id' => $params['contactId'],
'thumbnail' => $thumbnail,
)
));
$tmpPhoto->remove($tmpkey);
return $response;
}
}