-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Contacts API: replace raw image data with url #25081
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
61c704a
add uri to AddressBookImpl array
georgehrke 067ec21
Introduce ImageExportPlugin for CardDav
DeepDiver1975 a890571
add plugin to v1 routes
georgehrke 543e119
replace binary contact photo with link
georgehrke 9c7689e
update tests
georgehrke 836def3
Adding unit tests
DeepDiver1975 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
<?php | ||
/** | ||
* @author Thomas Müller <[email protected]> | ||
* | ||
* @copyright Copyright (c) 2016, ownCloud, Inc. | ||
* @license AGPL-3.0 | ||
* | ||
* This code is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License, version 3, | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License, version 3, | ||
* along with this program. If not, see <http://www.gnu.org/licenses/> | ||
* | ||
*/ | ||
|
||
namespace OCA\DAV\CardDAV; | ||
|
||
use OCP\ILogger; | ||
use Sabre\CardDAV\Card; | ||
use Sabre\DAV\Server; | ||
use Sabre\DAV\ServerPlugin; | ||
use Sabre\HTTP\RequestInterface; | ||
use Sabre\HTTP\ResponseInterface; | ||
use Sabre\VObject\Parameter; | ||
use Sabre\VObject\Property\Binary; | ||
use Sabre\VObject\Reader; | ||
|
||
class ImageExportPlugin extends ServerPlugin { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this plugin deserves some unit tests as well 🙈 |
||
|
||
/** @var Server */ | ||
protected $server; | ||
/** @var ILogger */ | ||
private $logger; | ||
|
||
public function __construct(ILogger $logger) { | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* Initializes the plugin and registers event handlers | ||
* | ||
* @param Server $server | ||
* @return void | ||
*/ | ||
function initialize(Server $server) { | ||
|
||
$this->server = $server; | ||
$this->server->on('method:GET', [$this, 'httpGet'], 90); | ||
} | ||
|
||
/** | ||
* Intercepts GET requests on addressbook urls ending with ?photo. | ||
* | ||
* @param RequestInterface $request | ||
* @param ResponseInterface $response | ||
* @return bool|void | ||
*/ | ||
function httpGet(RequestInterface $request, ResponseInterface $response) { | ||
|
||
$queryParams = $request->getQueryParameters(); | ||
// TODO: in addition to photo we should also add logo some point in time | ||
if (!array_key_exists('photo', $queryParams)) { | ||
return true; | ||
} | ||
|
||
$path = $request->getPath(); | ||
$node = $this->server->tree->getNodeForPath($path); | ||
|
||
if (!($node instanceof Card)) { | ||
return true; | ||
} | ||
|
||
$this->server->transactionType = 'carddav-image-export'; | ||
|
||
// Checking ACL, if available. | ||
if ($aclPlugin = $this->server->getPlugin('acl')) { | ||
/** @var \Sabre\DAVACL\Plugin $aclPlugin */ | ||
$aclPlugin->checkPrivileges($path, '{DAV:}read'); | ||
} | ||
|
||
if ($result = $this->getPhoto($node)) { | ||
$response->setHeader('Content-Type', $result['Content-Type']); | ||
$response->setStatus(200); | ||
|
||
$response->setBody($result['body']); | ||
|
||
// Returning false to break the event chain | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
function getPhoto(Card $node) { | ||
// TODO: this is kind of expensive - load carddav data from database and parse it | ||
// we might want to build up a cache one day | ||
try { | ||
$vObject = $this->readCard($node->get()); | ||
if (!$vObject->PHOTO) { | ||
return false; | ||
} | ||
|
||
$photo = $vObject->PHOTO; | ||
$type = $this->getType($photo); | ||
|
||
$valType = $photo->getValueType(); | ||
$val = ($valType === 'URI' ? $photo->getRawMimeDirValue() : $photo->getValue()); | ||
return [ | ||
'Content-Type' => $type, | ||
'body' => $val | ||
]; | ||
} catch(\Exception $ex) { | ||
$this->logger->logException($ex); | ||
} | ||
return false; | ||
} | ||
|
||
private function readCard($cardData) { | ||
return Reader::read($cardData); | ||
} | ||
|
||
/** | ||
* @param Binary $photo | ||
* @return Parameter | ||
*/ | ||
private function getType($photo) { | ||
$params = $photo->parameters(); | ||
if (isset($params['TYPE']) || isset($params['MEDIATYPE'])) { | ||
/** @var Parameter $typeParam */ | ||
$typeParam = isset($params['TYPE']) ? $params['TYPE'] : $params['MEDIATYPE']; | ||
$type = $typeParam->getValue(); | ||
|
||
if (strpos($type, 'image/') === 0) { | ||
return $type; | ||
} else { | ||
return 'image/' . strtolower($type); | ||
} | ||
} | ||
return ''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@deepdiver Is there some helper method to generate the full carddav url here?
I have access to the principal uris, but no direct access to addressbooks/users/admin/contacts/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no helper method at hand - the addressbook name is in addressBookInfo or in the addrebook instance.
No idea if this helps. THX