* @throws OCSNotFoundException File not found * @throws OCSBadRequestException Getting direct link is not possible * @throws OCSForbiddenException Missing permissions to get direct link * * 200: Direct link returned */ #[NoAdminRequired] public function getUrl(int $fileId, int $expirationTime = 60 * 60 * 8): DataResponse { $userFolder = $this->rootFolder->getUserFolder($this->userId); $file = $userFolder->getFirstNodeById($fileId); if (!$file) { throw new OCSNotFoundException(); } if ($expirationTime <= 0 || $expirationTime > (60 * 60 * 24)) { throw new OCSBadRequestException('Expiration time should be greater than 0 and less than or equal to ' . (60 * 60 * 24)); } if (!($file instanceof File)) { throw new OCSBadRequestException('Direct download only works for files'); } $event = new BeforeDirectFileDownloadEvent($userFolder->getRelativePath($file->getPath())); $this->eventDispatcher->dispatchTyped($event); if ($event->isSuccessful() === false) { throw new OCSForbiddenException('Permission denied to download file'); } //TODO: at some point we should use the directdownlaod function of storages $direct = new Direct(); $direct->setUserId($this->userId); $direct->setFileId($fileId); $token = $this->random->generate(60, ISecureRandom::CHAR_ALPHANUMERIC); $direct->setToken($token); $direct->setExpiration($this->timeFactory->getTime() + $expirationTime); $this->mapper->insert($direct); $url = $this->urlGenerator->getAbsoluteURL('remote.php/direct/' . $token); return new DataResponse([ 'url' => $url, ]); } }