From b5dfa7c65d7d3ffbb395c83d0177c5b5752efb45 Mon Sep 17 00:00:00 2001 From: Matias De lellis Date: Sat, 14 Nov 2020 00:22:39 -0300 Subject: [PATCH] Add direct deep-link to attachments --- lib/Controller/AttachmentApiController.php | 7 ++++--- lib/Db/Attach.php | 18 +++++++++++------ lib/Service/FileService.php | 23 +++++++++++++++++++++- lib/Service/NoteService.php | 6 ++++++ 4 files changed, 44 insertions(+), 10 deletions(-) diff --git a/lib/Controller/AttachmentApiController.php b/lib/Controller/AttachmentApiController.php index 8450d11..9d2eff0 100644 --- a/lib/Controller/AttachmentApiController.php +++ b/lib/Controller/AttachmentApiController.php @@ -65,9 +65,10 @@ class AttachmentApiController extends ApiController { $fileId = $this->fileService->upload($file['name'], file_get_contents($file['tmp_name'])); return new JSONResponse([ - 'file_id' => $fileId, - 'preview_url' => $this->fileService->getPreviewUrl($fileId, 512), - 'redirect_url' => $this->fileService->getRedirectToFileUrl($fileId) + 'file_id' => $fileId, + 'preview_url' => $this->fileService->getPreviewUrl($fileId, 512), + 'redirect_url' => $this->fileService->getRedirectToFileUrl($fileId), + 'deep_link_url' => $this->fileService->getDeepLinkUrl($fileId) ]); } diff --git a/lib/Db/Attach.php b/lib/Db/Attach.php index 76a5588..f27f67c 100644 --- a/lib/Db/Attach.php +++ b/lib/Db/Attach.php @@ -12,6 +12,7 @@ class Attach extends Entity implements JsonSerializable { protected $createdAt; protected $previewUrl; protected $redirectUrl; + protected $deepLinkUrl; public function setPreviewUrl($previewUrl) { $this->previewUrl = $previewUrl; @@ -21,14 +22,19 @@ class Attach extends Entity implements JsonSerializable { $this->redirectUrl = $redirectUrl; } + public function setDeepLinkUrl($deepLinkUrl) { + $this->deepLinkUrl = $deepLinkUrl; + } + public function jsonSerialize() { return [ - 'id' => $this->id, - 'note_id' => $this->noteId, - 'file_id' => $this->fileId, - 'created_at' => $this->createdAt, - 'preview_url' => $this->previewUrl, - 'redirect_url' => $this->redirectUrl + 'id' => $this->id, + 'note_id' => $this->noteId, + 'file_id' => $this->fileId, + 'created_at' => $this->createdAt, + 'preview_url' => $this->previewUrl, + 'redirect_url' => $this->redirectUrl, + 'deep_link_url' => $this->deepLinkUrl ]; } } diff --git a/lib/Service/FileService.php b/lib/Service/FileService.php index e7666cb..b2293e6 100644 --- a/lib/Service/FileService.php +++ b/lib/Service/FileService.php @@ -100,7 +100,28 @@ class FileService { $params['dir'] = $userFolder->getRelativePath($file->getParent()->getPath()); $params['scrollto'] = $file->getName(); - return $this->urlGenerator->linkToRoute('files.view.index', $params); + return $this->urlGenerator->getAbsoluteURL( + $this->urlGenerator->linkToRoute('files.view.index', $params) + ); + } + + /** + * Get a deep link that can open directly on clients to the given file id + * + * @param int $fileId file id to open + */ + public function getDeepLinkUrl(int $fileId): ?string { + $userFolder = $this->rootFolder->getUserFolder($this->userId); + $file = current($userFolder->getById($fileId)); + + if (!($file instanceof File)) { + return null; + } + +// return "nc://directlink/f/" . $fileId; + return $this->urlGenerator->getAbsoluteURL( + "/f/" . $fileId + ); } /** diff --git a/lib/Service/NoteService.php b/lib/Service/NoteService.php index 7d2040a..0a485cc 100644 --- a/lib/Service/NoteService.php +++ b/lib/Service/NoteService.php @@ -125,8 +125,13 @@ class NoteService { if (is_null($redirectUrl)) continue; + $deepLinkUrl = $this->fileService->getDeepLinkUrl($attach->getFileId()); + if (is_null($deepLinkUrl)) + continue; + $attach->setPreviewUrl($previewUrl); $attach->setRedirectUrl($redirectUrl); + $attach->setDeepLinkUrl($deepLinkUrl); $rAttachts[] = $attach; } @@ -372,6 +377,7 @@ class NoteService { foreach ($attachts as $attach) { $attach->setPreviewUrl($this->fileService->getPreviewUrl($attach->getFileId(), 512)); $attach->setRedirectUrl($this->fileService->getRedirectToFileUrl($attach->getFileId())); + $attach->setDeepLinkUrl($this->fileService->getDeepLinkUrl($attach->getFileId())); } $newnote->setAttachts($attachts);