1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2024-12-01 13:24:10 +01:00
OwncloudContactsOfficial/lib/controller/contactphotocontroller.php

242 lines
6.1 KiB
PHP
Raw Normal View History

<?php
/**
* @author Thomas Tanghus
2014-01-26 00:40:22 +01:00
* @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,
OCP\IRequest,
OCP\ICache;
/**
* Controller class For Contacts
*/
2013-09-17 18:46:59 +02:00
class ContactPhotoController extends Controller {
2014-04-08 01:19:30 +02:00
/**
* @var \OCP\ICache
*/
2014-04-08 01:32:53 +02:00
protected $cache;
2014-04-08 01:19:30 +02:00
public function __construct($appName, IRequest $request, App $app, ICache $cache) {
2014-04-08 01:19:30 +02:00
parent::__construct($appName, $request, $app);
$this->cache = $cache;
}
/**
2013-09-24 13:39:45 +02:00
* @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']);
2014-03-20 21:53:13 +01:00
$tempPhoto = TemporaryPhoto::create(
$this->cache,
2014-03-20 21:53:13 +01:00
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 {
2014-03-20 21:53:13 +01:00
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.
*
2013-09-24 13:39:45 +02:00
* @NoAdminRequired
*/
public function uploadPhoto() {
$params = $this->request->urlParams;
2014-03-20 16:00:22 +01:00
$response = new JSONResponse();
2014-03-20 21:53:13 +01:00
$tempPhoto = TemporaryPhoto::create(
$this->cache,
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.
*
2013-09-24 13:39:45 +02:00
* @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']);
2014-03-20 21:53:13 +01:00
$tempPhoto = TemporaryPhoto::create(
$this->cache,
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.
*
2013-09-24 13:39:45 +02:00
* @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.'));
}
2014-03-20 21:53:13 +01:00
$tempPhoto = TemporaryPhoto::create(
$this->cache,
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.
2013-09-24 13:39:45 +02:00
* @NoAdminRequired
* @NoCSRFRequired
*/
public function getTempPhoto() {
$params = $this->request->urlParams;
$tmpkey = $params['key'];
$tmpPhoto = new TemporaryPhoto($this->cache, $tmpkey);
2014-03-20 21:53:13 +01:00
$image = $tmpPhoto->getPhoto();
if($image->valid()) {
$response = new ImageResponse($image);
return $response;
} else {
$response = new JSONResponse();
2013-10-17 02:10:34 +02:00
return $response->bailOut('Error getting temporary photo');
}
}
/**
* Get a photo from the oC and crops it with the suplied geometry.
2013-09-24 13:39:45 +02:00
* @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'];
2014-04-07 23:35:57 +02:00
$addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']);
$contact = $addressBook->getChild($params['contactId']);
$response = new JSONResponse();
$tmpPhoto = new TemporaryPhoto($this->cache, $tmpkey);
2014-03-20 21:53:13 +01:00
$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);
2014-03-20 21:53:13 +01:00
if (!$contact->setPhoto($image)) {
$tmpPhoto->remove($tmpkey);
return $response->bailOut(App::$l10n->t('Error getting PHOTO property.'));
}
2014-03-20 21:53:13 +01:00
if(!$contact->save()) {
2013-10-17 02:10:34 +02:00
return $response->bailOut(App::$l10n->t('Error saving contact.'));
}
$thumbnail = Properties::cacheThumbnail(
$params['backend'],
$params['addressBookId'],
$params['contactId'],
2014-03-21 15:18:31 +01:00
$image,
$contact
);
2013-10-22 17:10:23 +02:00
$response->setData(array(
'status' => 'success',
'data' => array(
'id' => $params['contactId'],
'thumbnail' => $thumbnail,
)
));
2014-03-20 21:53:13 +01:00
$tmpPhoto->remove($tmpkey);
return $response;
}
}