-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Backend for reference metadata fetching #33494
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
Changes from 1 commit
650b809
de3e541
9d9c7f4
31be855
f58218d
0ce0d37
eaef568
68d0038
bee8fd2
a392235
80f6a58
1ab6698
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Signed-off-by: Julius Härtl <[email protected]>
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,18 +24,21 @@ | |
|
|
||
| namespace OC\Core\Controller; | ||
|
|
||
| use OC\Collaboration\Reference\ReferenceManager; | ||
| use OCP\Collaboration\Reference\IReferenceManager; | ||
| use OCP\AppFramework\Controller; | ||
| use OCP\AppFramework\Http; | ||
| use OCP\AppFramework\Http\DataDownloadResponse; | ||
| use OCP\AppFramework\Http\DataResponse; | ||
| use OCP\Files\AppData\IAppDataFactory; | ||
| use OCP\Files\NotFoundException; | ||
| use OCP\Files\NotPermittedException; | ||
| use OCP\IRequest; | ||
|
|
||
| class ReferenceController extends \OCP\AppFramework\Controller { | ||
| private ReferenceManager $referenceManager; | ||
| class ReferenceController extends Controller { | ||
| private IReferenceManager $referenceManager; | ||
| private IAppDataFactory $appDataFactory; | ||
|
|
||
| public function __construct($appName, IRequest $request, ReferenceManager $referenceManager, IAppDataFactory $appDataFactory) { | ||
| public function __construct(string $appName, IRequest $request, IReferenceManager $referenceManager, IAppDataFactory $appDataFactory) { | ||
| parent::__construct($appName, $request); | ||
| $this->referenceManager = $referenceManager; | ||
| $this->appDataFactory = $appDataFactory; | ||
|
|
@@ -44,10 +47,8 @@ public function __construct($appName, IRequest $request, ReferenceManager $refer | |
| /** | ||
| * @PublicPage | ||
| * @NoCSRFRequired | ||
| * @param $referenceId | ||
| * @throws \OCP\Files\NotFoundException | ||
| */ | ||
| public function preview($referenceId) { | ||
| public function preview(string $referenceId) { | ||
juliusknorr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| $reference = $this->referenceManager->getReferenceByCacheKey($referenceId); | ||
juliusknorr marked this conversation as resolved.
Fixed
Show fixed
Hide fixed
Member
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. I don't get a hit here locally so I don't have any previews. Is there something else needed to configure? Is a Redis required? Or is the method not loading the providers or something?
Member
Author
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. Yes, would require redis/apcu in its current state. I'd need to think how we can make this work without, as currently we have no reference from the hashed reference that is used for the preview to the original one. |
||
| if ($reference === null) { | ||
| return new DataResponse('', Http::STATUS_NOT_FOUND); | ||
|
|
@@ -57,9 +58,9 @@ public function preview($referenceId) { | |
| $appData = $this->appDataFactory->get('core'); | ||
|
||
| $folder = $appData->getFolder('opengraph'); | ||
| $file = $folder->getFile($referenceId); | ||
| } catch (NotFoundException $e) { | ||
| return new DataDownloadResponse($file->getContent(), $referenceId, $reference->getImageContentType()); | ||
| } catch (NotFoundException|NotPermittedException $e) { | ||
| return new DataResponse('', Http::STATUS_NOT_FOUND); | ||
| } | ||
| return new DataDownloadResponse($file->getContent(), $referenceId, $reference->getImageContentType()); | ||
| } | ||
| } | ||
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| <?php | ||
juliusknorr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| declare(strict_types=1); | ||
| /** | ||
| * @copyright Copyright (c) 2022 Julius Härtl <[email protected]> | ||
| * | ||
|
|
@@ -22,23 +24,29 @@ | |
|
|
||
| namespace OC\Collaboration\Reference; | ||
|
|
||
| use OC\User\NoUserException; | ||
| use OCP\Collaboration\Reference\IReference; | ||
| use OCP\Collaboration\Reference\IReferenceProvider; | ||
| use OCP\Files\InvalidPathException; | ||
| use OCP\Files\IRootFolder; | ||
| use OCP\Files\Node; | ||
| use OCP\Files\NotFoundException; | ||
| use OCP\Files\NotPermittedException; | ||
| use OCP\IPreview; | ||
| use OCP\IURLGenerator; | ||
| use OCP\IUserSession; | ||
|
|
||
| class FileReferenceProvider implements IReferenceProvider { | ||
| private IURLGenerator $urlGenerator; | ||
| private IRootFolder $rootFolder; | ||
| private ?string $userId; | ||
| private IPreview $previewManager; | ||
|
|
||
| public function __construct(IURLGenerator $urlGenerator, IRootFolder $rootFolder, IUserSession $userSession) { | ||
| public function __construct(IURLGenerator $urlGenerator, IRootFolder $rootFolder, IUserSession $userSession, IPreview $previewManager) { | ||
| $this->urlGenerator = $urlGenerator; | ||
| $this->rootFolder = $rootFolder; | ||
| $this->userId = $userSession->getUser() ? $userSession->getUser()->getUID() : null; | ||
juliusknorr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| $this->previewManager = $previewManager; | ||
| } | ||
|
|
||
| public function matchReference(string $referenceText): bool { | ||
|
|
@@ -52,6 +60,7 @@ public function resolveReference(string $referenceText): ?IReference { | |
| try { | ||
| $this->fetchReference($reference); | ||
| } catch (NotFoundException $e) { | ||
| $reference->setRichObject('file', null); | ||
| $reference->setAccessible(false); | ||
| } | ||
| return $reference; | ||
|
|
@@ -61,10 +70,7 @@ public function resolveReference(string $referenceText): ?IReference { | |
| } | ||
|
|
||
| /** | ||
| * @throws \OCP\Files\NotPermittedException | ||
| * @throws \OCP\Files\InvalidPathException | ||
| * @throws NotFoundException | ||
| * @throws \OC\User\NoUserException | ||
| */ | ||
| private function fetchReference(Reference $reference) { | ||
| if ($this->userId === null) { | ||
|
|
@@ -74,37 +80,41 @@ private function fetchReference(Reference $reference) { | |
| $fileId = str_replace($this->urlGenerator->getAbsoluteURL('/index.php/f/'), '', $reference->getId()); | ||
| $fileId = str_replace($this->urlGenerator->getAbsoluteURL('/f/'), '', $fileId); | ||
|
|
||
| $userFolder = $this->rootFolder->getUserFolder($this->userId); | ||
| $files = $userFolder->getById((int)$fileId); | ||
| try { | ||
| $userFolder = $this->rootFolder->getUserFolder($this->userId); | ||
| $files = $userFolder->getById((int)$fileId); | ||
|
|
||
| if (empty($files)) { | ||
| throw new NotFoundException(); | ||
| } | ||
|
|
||
| if (empty($files)) { | ||
| /** @var Node $file */ | ||
| $file = array_shift($files); | ||
|
|
||
| $reference->setTitle($file->getName()); | ||
| $reference->setDescription($file->getMimetype()); | ||
| $reference->setUrl($this->urlGenerator->getAbsoluteURL('/index.php/f/' . $fileId)); | ||
| $reference->setImageUrl($this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', ['x' => 1600, 'y' => 630, 'fileId' => $fileId])); | ||
|
|
||
| $reference->setRichObject('file', [ | ||
| 'id' => $file->getId(), | ||
| 'name' => $file->getName(), | ||
| 'size' => $file->getSize(), | ||
| 'path' => $file->getPath(), | ||
| 'link' => $reference->getUrl(), | ||
| 'mimetype' => $file->getMimetype(), | ||
| 'preview-available' => $this->previewManager->isAvailable($file) | ||
| ]); | ||
| } catch (InvalidPathException|NotFoundException|NotPermittedException|NoUserException $e) { | ||
| throw new NotFoundException(); | ||
| } | ||
|
|
||
| /** @var Node $file */ | ||
| $file = array_shift($files); | ||
|
|
||
| $reference->setTitle($file->getName()); | ||
| $reference->setDescription($file->getMimetype()); | ||
| $reference->setUrl($this->urlGenerator->getAbsoluteURL('/index.php/f/' . $fileId)); | ||
| $reference->setImageUrl($this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', ['x' => 1600, 'y' => 630, 'fileId' => $fileId])); | ||
|
|
||
| $reference->setRichObject('file', [ | ||
| 'id' => $file->getId(), | ||
| 'name' => $file->getName(), | ||
| 'size' => $file->getSize(), | ||
| 'path' => $file->getPath(), | ||
| 'link' => $reference->getUrl(), | ||
| 'mimetype' => $file->getMimetype(), | ||
| 'preview-available' => false | ||
| ]); | ||
| } | ||
|
|
||
| public function isGloballyCacheable(): bool { | ||
| return false; | ||
| } | ||
|
|
||
| public function getCacheKey(string $referenceId): string { | ||
|
||
| return $this->userId; | ||
| return $this->userId ?? ''; | ||
juliusknorr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.