use OCA\DAV\Connector\Sabre\Principal;
use OCP\Accounts\IAccountManager;
use OCP\App\IAppManager;
+use OCP\IAvatarManager;
use Psr\Log\LoggerInterface;
use Sabre\CardDAV\Plugin;
$server->addPlugin(new \Sabre\DAV\Sync\Plugin());
$server->addPlugin(new \Sabre\CardDAV\VCFExportPlugin());
-$server->addPlugin(new \OCA\DAV\CardDAV\ImageExportPlugin(new \OCA\DAV\CardDAV\PhotoCache(
- \OC::$server->getAppDataDir('dav-photocache'),
- \OC::$server->get(LoggerInterface::class)
-)));
+$server->addPlugin(new \OCA\DAV\CardDAV\ImageExportPlugin(
+ new \OCA\DAV\CardDAV\PhotoCache(
+ \OC::$server->getAppDataDir('dav-photocache'),
+ \OC::$server->get(LoggerInterface::class)
+ ),
+ \OC::$server->get(IAvatarManager::class),
+));
$server->addPlugin(new ExceptionLoggerPlugin('carddav', \OC::$server->get(LoggerInterface::class)));
// And off we go!
namespace OCA\DAV\CardDAV;
use OCP\Files\NotFoundException;
+use OCP\IAvatarManager;
use Sabre\CardDAV\Card;
use Sabre\DAV\Server;
use Sabre\DAV\ServerPlugin;
use Sabre\HTTP\RequestInterface;
use Sabre\HTTP\ResponseInterface;
+use Sabre\VObject\Reader;
class ImageExportPlugin extends ServerPlugin {
/** @var PhotoCache */
private $cache;
+ private IAvatarManager $avatarManager;
+
/**
* ImageExportPlugin constructor.
- *
- * @param PhotoCache $cache
*/
- public function __construct(PhotoCache $cache) {
+ public function __construct(PhotoCache $cache, IAvatarManager $avatarManager) {
$this->cache = $cache;
+ $this->avatarManager = $avatarManager;
}
/**
$response->setHeader('Cache-Control', 'private, max-age=3600, must-revalidate');
$response->setHeader('Etag', $node->getETag());
+ // Try to use embedded avatar image
try {
$file = $this->cache->get($addressbook->getResourceId(), $node->getName(), $size, $node);
$response->setHeader('Content-Type', $file->getMimeType());
$response->setStatus(200);
$response->setBody($file->getContent());
+ return false;
} catch (NotFoundException $e) {
- $response->setStatus(404);
+ // Fall back to generated avatar
+ }
+
+ $name = '?';
+ $vObject = Reader::read($node->get());
+ if (isset($vObject->FN)) {
+ $name = $vObject->FN->getValue();
+ } elseif (isset($vObject->N)) {
+ [$lastName, $firstName] = $vObject->N->getParts();
+ $name = "$firstName $lastName";
}
+ $avatar = $this->avatarManager->getGuestAvatar($name);
+ $image = $avatar->get($size);
+
+ $response->setHeader('Content-Type', $image->mimeType());
+ $fileName = $node->getName() . '.' . PhotoCache::ALLOWED_CONTENT_TYPES[$image->mimeType()];
+ $response->setHeader('Content-Disposition', "attachment; filename=$fileName");
+ $response->setStatus(200);
+ $response->setBody($image->data());
+
return false;
}
}
use OCP\Diagnostics\IEventLogger;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\FilesMetadata\IFilesMetadataManager;
+use OCP\IAvatarManager;
use OCP\ICacheFactory;
use OCP\IRequest;
use OCP\Profiler\IProfiler;
$this->server->addPlugin(new VCFExportPlugin());
$this->server->addPlugin(new MultiGetExportPlugin());
$this->server->addPlugin(new HasPhotoPlugin());
- $this->server->addPlugin(new ImageExportPlugin(new PhotoCache(
- \OC::$server->getAppDataDir('dav-photocache'),
- $logger)
+ $this->server->addPlugin(
+ new ImageExportPlugin(new PhotoCache(
+ \OC::$server->getAppDataDir('dav-photocache'),
+ $logger,
+ ),
+ \OC::$server->get(IAvatarManager::class),
));
}
use OCA\DAV\CardDAV\PhotoCache;
use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\ISimpleFile;
+use OCP\IAvatarManager;
use Sabre\CardDAV\Card;
use Sabre\DAV\Node;
use Sabre\DAV\Server;
private $tree;
/** @var PhotoCache|\PHPUnit\Framework\MockObject\MockObject */
private $cache;
+ /** @var IAvatarManager|\PHPUnit\Framework\MockObject\MockObject */
+ private $avatarManager;
protected function setUp(): void {
parent::setUp();
$this->tree = $this->createMock(Tree::class);
$this->server->tree = $this->tree;
$this->cache = $this->createMock(PhotoCache::class);
+ $this->avatarManager = $this->createMock(IAvatarManager::class);
$this->plugin = $this->getMockBuilder(ImageExportPlugin::class)
->setMethods(['getPhoto'])
- ->setConstructorArgs([$this->cache])
+ ->setConstructorArgs([$this->cache, $this->avatarManager])
->getMock();
$this->plugin->initialize($this->server);
}