|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\DAV\Controller; |
| 11 | + |
| 12 | +use OCA\DAV\AppInfo\Application; |
| 13 | +use OCP\App\IAppManager; |
| 14 | +use OCP\AppFramework\ApiController; |
| 15 | +use OCP\AppFramework\Http; |
| 16 | +use OCP\AppFramework\Http\JSONResponse; |
| 17 | +use OCP\Files\AppData\IAppDataFactory; |
| 18 | +use OCP\Files\IAppData; |
| 19 | +use OCP\Files\NotFoundException; |
| 20 | +use OCP\IConfig; |
| 21 | +use OCP\IRequest; |
| 22 | +use Psr\Log\LoggerInterface; |
| 23 | + |
| 24 | +class ExampleContentController extends ApiController { |
| 25 | + private IAppData $appData; |
| 26 | + public function __construct( |
| 27 | + IRequest $request, |
| 28 | + private IConfig $config, |
| 29 | + private IAppDataFactory $appDataFactory, |
| 30 | + private IAppManager $appManager, |
| 31 | + private LoggerInterface $logger, |
| 32 | + ) { |
| 33 | + parent::__construct(Application::APP_ID, $request); |
| 34 | + $this->appData = $this->appDataFactory->get('dav'); |
| 35 | + } |
| 36 | + |
| 37 | + public function setEnableDefaultContact($allow) { |
| 38 | + if ($allow === 'yes' && !$this->defaultContactExists()) { |
| 39 | + try { |
| 40 | + $this->setCard(); |
| 41 | + } catch (\Exception $e) { |
| 42 | + $this->logger->error('Could not create default contact', ['exception' => $e]); |
| 43 | + return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); |
| 44 | + } |
| 45 | + } |
| 46 | + $this->config->setAppValue(Application::APP_ID, 'enableDefaultContact', $allow); |
| 47 | + return new JSONResponse([], Http::STATUS_OK); |
| 48 | + } |
| 49 | + |
| 50 | + public function setDefaultContact(?string $contactData = null) { |
| 51 | + if (!$this->config->getAppValue(Application::APP_ID, 'enableDefaultContact', 'no')) { |
| 52 | + return new JSONResponse([], Http::STATUS_FORBIDDEN); |
| 53 | + } |
| 54 | + $this->setCard($contactData); |
| 55 | + return new JSONResponse([], Http::STATUS_OK); |
| 56 | + } |
| 57 | + |
| 58 | + private function setCard(?string $cardData = null) { |
| 59 | + try { |
| 60 | + $folder = $this->appData->getFolder('defaultContact'); |
| 61 | + } catch (NotFoundException $e) { |
| 62 | + $folder = $this->appData->newFolder('defaultContact'); |
| 63 | + } |
| 64 | + |
| 65 | + if (is_null($cardData)) { |
| 66 | + $cardData = file_get_contents(__DIR__ . '/../ExampleContentFiles/exampleContact.vcf'); |
| 67 | + } |
| 68 | + |
| 69 | + if (!$cardData) { |
| 70 | + throw new \Exception('Could not read exampleContact.vcf'); |
| 71 | + } |
| 72 | + |
| 73 | + $file = (!$folder->fileExists('defaultContact.vcf')) ? $folder->newFile('defaultContact.vcf') : $folder->getFile('defaultContact.vcf'); |
| 74 | + $file->putContent($cardData); |
| 75 | + } |
| 76 | + |
| 77 | + private function defaultContactExists(): bool { |
| 78 | + try { |
| 79 | + $folder = $this->appData->getFolder('defaultContact'); |
| 80 | + } catch (NotFoundException $e) { |
| 81 | + return false; |
| 82 | + } |
| 83 | + return $folder->fileExists('defaultContact.vcf'); |
| 84 | + } |
| 85 | + |
| 86 | +} |
0 commit comments