Blame view

sources/apps/contacts/lib/controller/contactphotocontroller.php 5.8 KB
d1bafeea1   Kload   [fix] Upgrade to ...
1
2
3
  <?php
  /**
   * @author Thomas Tanghus
6d9380f96   Cédric Dupont   Update sources OC...
4
   * @copyright 2013-2014 Thomas Tanghus (thomas@tanghus.net)
d1bafeea1   Kload   [fix] Upgrade to ...
5
6
7
8
9
10
11
12
13
14
15
   * 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,
6d9380f96   Cédric Dupont   Update sources OC...
16
17
18
19
  	OCA\Contacts\Utils\TemporaryPhoto,
  	OCA\Contacts\Controller,
  	OCP\IRequest,
  	OCP\ICache;
d1bafeea1   Kload   [fix] Upgrade to ...
20
21
22
23
24
25
26
  
  /**
   * Controller class For Contacts
   */
  class ContactPhotoController extends Controller {
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
27
28
29
30
31
32
33
34
35
36
  	 * @var \OCP\ICache
  	 */
  	protected $cache;
  
  	public function __construct($appName, IRequest $request, App $app, ICache $cache) {
  		parent::__construct($appName, $request, $app);
  		$this->cache = $cache;
  	}
  
  	/**
d1bafeea1   Kload   [fix] Upgrade to ...
37
38
39
40
41
42
43
  	 * @NoAdminRequired
  	 * @NoCSRFRequired
  	 */
  	public function getPhoto($maxSize = 170) {
  		// TODO: Cache resized photo
  		$params = $this->request->urlParams;
  		$etag = null;
d1bafeea1   Kload   [fix] Upgrade to ...
44
45
  		$addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']);
  		$contact = $addressBook->getChild($params['contactId']);
6d9380f96   Cédric Dupont   Update sources OC...
46
47
48
49
50
  		$tempPhoto = TemporaryPhoto::create(
  			$this->cache,
  			TemporaryPhoto::PHOTO_CURRENT,
  			$contact
  		);
d1bafeea1   Kload   [fix] Upgrade to ...
51

6d9380f96   Cédric Dupont   Update sources OC...
52
53
  		if($tempPhoto->isValid()) {
  			$image = $tempPhoto->getPhoto();
d1bafeea1   Kload   [fix] Upgrade to ...
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  			$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 {
6d9380f96   Cédric Dupont   Update sources OC...
68
  			return array("data"=> array("FN"=> $contact->getDisplayName()));
d1bafeea1   Kload   [fix] Upgrade to ...
69
70
71
72
73
74
75
76
  		}
  	}
  
  	/**
  	 * Uploads a photo and saves in oC cache
  	 * @return JSONResponse with data.tmp set to the key in the cache.
  	 *
  	 * @NoAdminRequired
d1bafeea1   Kload   [fix] Upgrade to ...
77
78
79
  	 */
  	public function uploadPhoto() {
  		$params = $this->request->urlParams;
d1bafeea1   Kload   [fix] Upgrade to ...
80
  		$response = new JSONResponse();
6d9380f96   Cédric Dupont   Update sources OC...
81
82
83
84
85
  		$tempPhoto = TemporaryPhoto::create(
  			$this->cache,
  			TemporaryPhoto::PHOTO_UPLOADED,
  			$this->request
  		);
d1bafeea1   Kload   [fix] Upgrade to ...
86

6d9380f96   Cédric Dupont   Update sources OC...
87
88
89
90
91
92
93
  		return $response->setParams(array(
  			'tmp'=>$tempPhoto->getKey(),
  			'metadata' => array(
  				'contactId'=> $params['contactId'],
  				'addressBookId'=> $params['addressBookId'],
  				'backend'=> $params['backend'],
  			),
d1bafeea1   Kload   [fix] Upgrade to ...
94
  		));
d1bafeea1   Kload   [fix] Upgrade to ...
95
96
97
98
99
100
101
102
103
104
105
106
  	}
  
  	/**
  	 * 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();
d1bafeea1   Kload   [fix] Upgrade to ...
107

6d9380f96   Cédric Dupont   Update sources OC...
108
109
  		$addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']);
  		$contact = $addressBook->getChild($params['contactId']);
d1bafeea1   Kload   [fix] Upgrade to ...
110

6d9380f96   Cédric Dupont   Update sources OC...
111
112
113
114
115
  		$tempPhoto = TemporaryPhoto::create(
  			$this->cache,
  			TemporaryPhoto::PHOTO_CURRENT,
  			$contact
  		);
d1bafeea1   Kload   [fix] Upgrade to ...
116

6d9380f96   Cédric Dupont   Update sources OC...
117
118
  		return $response->setParams(array(
  			'tmp'=>$tempPhoto->getKey(),
d1bafeea1   Kload   [fix] Upgrade to ...
119
120
121
122
123
124
  			'metadata' => array(
  				'contactId'=> $params['contactId'],
  				'addressBookId'=> $params['addressBookId'],
  				'backend'=> $params['backend'],
  			),
  		));
d1bafeea1   Kload   [fix] Upgrade to ...
125
126
127
128
129
130
131
132
133
134
135
  	}
  
  	/**
  	 * 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;
d1bafeea1   Kload   [fix] Upgrade to ...
136
137
138
139
140
  		$response = new JSONResponse();
  
  		if(!isset($this->request->get['path'])) {
  			$response->bailOut(App::$l10n->t('No photo path was submitted.'));
  		}
6d9380f96   Cédric Dupont   Update sources OC...
141
142
143
144
145
  		$tempPhoto = TemporaryPhoto::create(
  			$this->cache,
  			TemporaryPhoto::PHOTO_FILESYSTEM,
  			$this->request->get['path']
  		);
d1bafeea1   Kload   [fix] Upgrade to ...
146

6d9380f96   Cédric Dupont   Update sources OC...
147
148
  		return $response->setParams(array(
  			'tmp'=>$tempPhoto->getKey(),
d1bafeea1   Kload   [fix] Upgrade to ...
149
150
151
152
153
154
  			'metadata' => array(
  				'contactId'=> $params['contactId'],
  				'addressBookId'=> $params['addressBookId'],
  				'backend'=> $params['backend'],
  			),
  		));
d1bafeea1   Kload   [fix] Upgrade to ...
155
156
157
158
159
160
161
162
163
164
  	}
  
  	/**
  	 * Get a photo from the oC cache for cropping.
  	 * @NoAdminRequired
  	 * @NoCSRFRequired
  	 */
  	public function getTempPhoto() {
  		$params = $this->request->urlParams;
  		$tmpkey = $params['key'];
6d9380f96   Cédric Dupont   Update sources OC...
165
166
  		$tmpPhoto = new TemporaryPhoto($this->cache, $tmpkey);
  		$image = $tmpPhoto->getPhoto();
d1bafeea1   Kload   [fix] Upgrade to ...
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
  		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;
6d9380f96   Cédric Dupont   Update sources OC...
183
184
185
186
  		$x = $this->params('x', 0);
  		$y = $this->params('y', 0);
  		$w = $this->params('w', -1);
  		$h = $this->params('h', -1);
d1bafeea1   Kload   [fix] Upgrade to ...
187
  		$tmpkey = $params['key'];
6d9380f96   Cédric Dupont   Update sources OC...
188
  		$addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']);
d1bafeea1   Kload   [fix] Upgrade to ...
189
190
191
  		$contact = $addressBook->getChild($params['contactId']);
  
  		$response = new JSONResponse();
6d9380f96   Cédric Dupont   Update sources OC...
192
193
194
195
  		$tmpPhoto = new TemporaryPhoto($this->cache, $tmpkey);
  		$image = $tmpPhoto->getPhoto();
  		if(!$image || !$image->valid()) {
  			return $response->bailOut(App::$l10n->t('Error loading image from cache'));
d1bafeea1   Kload   [fix] Upgrade to ...
196
197
198
199
  		}
  
  		$w = ($w !== -1 ? $w : $image->width());
  		$h = ($h !== -1 ? $h : $image->height());
6d9380f96   Cédric Dupont   Update sources OC...
200
  		$image->crop($x, $y, $w, $h);
d1bafeea1   Kload   [fix] Upgrade to ...
201

6d9380f96   Cédric Dupont   Update sources OC...
202
203
204
  		if (!$contact->setPhoto($image)) {
  			$tmpPhoto->remove($tmpkey);
  			return $response->bailOut(App::$l10n->t('Error getting PHOTO property.'));
d1bafeea1   Kload   [fix] Upgrade to ...
205
  		}
6d9380f96   Cédric Dupont   Update sources OC...
206

d1bafeea1   Kload   [fix] Upgrade to ...
207
208
209
210
211
212
213
214
  		if(!$contact->save()) {
  			return $response->bailOut(App::$l10n->t('Error saving contact.'));
  		}
  
  		$thumbnail = Properties::cacheThumbnail(
  			$params['backend'],
  			$params['addressBookId'],
  			$params['contactId'],
6d9380f96   Cédric Dupont   Update sources OC...
215
216
  			$image,
  			$contact
d1bafeea1   Kload   [fix] Upgrade to ...
217
218
219
220
221
222
223
224
225
  		);
  
  		$response->setData(array(
  			'status' => 'success',
  			'data' => array(
  				'id' => $params['contactId'],
  				'thumbnail' => $thumbnail,
  			)
  		));
6d9380f96   Cédric Dupont   Update sources OC...
226
  		$tmpPhoto->remove($tmpkey);
d1bafeea1   Kload   [fix] Upgrade to ...
227
228
229
230
231
  
  		return $response;
  	}
  
  }