validate('Uuid', $uuid); } #[NoAdminRequired] #[NoCSRFRequired] #[PublicPage] public function validateFileId($fileId): JSONResponse { return $this->validate('FileId', $fileId); } #[NoAdminRequired] #[NoCSRFRequired] #[PublicPage] public function validate(?string $type = null, $identifier = null): JSONResponse { try { if ($type === 'Uuid' && !empty($identifier)) { try { $this->fileService ->setFileByType('Uuid', $identifier); } catch (LibresignException $e) { $this->fileService ->setFileByType('SignerUuid', $identifier); } } elseif (!empty($type) && !empty($identifier)) { $this->fileService ->setFileByType($type, $identifier); } elseif ($this->request->getParam('path')) { $this->fileService ->setMe($this->userSession->getUser()) ->setFileByPath($this->request->getParam('path')); } elseif ($this->request->getParam('fileId')) { $this->fileService->setFileByType( 'FileId', $this->request->getParam('fileId') ); } elseif ($this->request->getParam('uuid')) { $this->fileService->setFileByType( 'Uuid', $this->request->getParam('uuid') ); } $return = []; $statusCode = Http::STATUS_OK; } catch (LibresignException $e) { $message = $this->l10n->t($e->getMessage()); $return = [ 'action' => JSActions::ACTION_DO_NOTHING, 'errors' => [$message] ]; $statusCode = $e->getCode() ?? Http::STATUS_UNPROCESSABLE_ENTITY; } catch (\Throwable $th) { $message = $this->l10n->t($th->getMessage()); $this->logger->error($message); $return = [ 'action' => JSActions::ACTION_DO_NOTHING, 'errors' => [$message] ]; $statusCode = $th->getCode() ?? Http::STATUS_UNPROCESSABLE_ENTITY; } $return = array_merge($return, $this->fileService ->setMe($this->userSession->getUser()) ->setIdentifyMethodId($this->sessionService->getIdentifyMethodId()) ->showVisibleElements() ->showSigners() ->showSettings() ->showMessages() ->formatFile() ); return new JSONResponse($return, $statusCode); } #[NoAdminRequired] #[NoCSRFRequired] public function list($page = null, $length = null, ?array $filter = []): JSONResponse { $return = $this->fileService ->setMe($this->userSession->getUser()) ->listAssociatedFilesOfSignFlow($page, $length, $filter); return new JSONResponse($return, Http::STATUS_OK); } #[NoAdminRequired] #[NoCSRFRequired] public function getThumbnail( int $nodeId = -1, int $x = 32, int $y = 32, bool $a = false, bool $forceIcon = true, string $mode = 'fill', bool $mimeFallback = false ) { if ($nodeId === -1 || $x === 0 || $y === 0) { return new DataResponse([], Http::STATUS_BAD_REQUEST); } try { $myLibreSignFile = $this->fileService ->setMe($this->userSession->getUser()) ->getMyLibresignFile($nodeId); $node = $this->accountService->getPdfByUuid($myLibreSignFile->getUuid()); } catch (DoesNotExistException $e) { return new DataResponse([], Http::STATUS_NOT_FOUND); } return $this->fetchPreview($node, $x, $y, $a, $forceIcon, $mode, $mimeFallback); } /** * @return FileDisplayResponse|DataResponse, array{}>|RedirectResponse */ private function fetchPreview( Node $node, int $x, int $y, bool $a, bool $forceIcon, string $mode, bool $mimeFallback = false, ) : Http\Response { if (!($node instanceof File) || (!$forceIcon && !$this->preview->isAvailable($node))) { return new DataResponse([], Http::STATUS_NOT_FOUND); } if (!$node->isReadable()) { return new DataResponse([], Http::STATUS_FORBIDDEN); } $storage = $node->getStorage(); if ($storage->instanceOfStorage(SharedStorage::class)) { /** @var SharedStorage $storage */ $share = $storage->getShare(); $attributes = $share->getAttributes(); if ($attributes !== null && $attributes->getAttribute('permissions', 'download') === false) { return new DataResponse([], Http::STATUS_FORBIDDEN); } } try { $f = $this->preview->getPreview($node, $x, $y, !$a, $mode); $response = new FileDisplayResponse($f, Http::STATUS_OK, [ 'Content-Type' => $f->getMimeType(), ]); $response->cacheFor(3600 * 24, false, true); return $response; } catch (NotFoundException $e) { // If we have no preview enabled, we can redirect to the mime icon if any if ($mimeFallback) { if ($url = $this->mimeIconProvider->getMimeIconUrl($node->getMimeType())) { return new RedirectResponse($url); } } return new DataResponse([], Http::STATUS_NOT_FOUND); } catch (\InvalidArgumentException $e) { return new DataResponse([], Http::STATUS_BAD_REQUEST); } } #[NoAdminRequired] #[NoCSRFRequired] #[RequireManager] public function save(array $file, string $name = '', array $settings = []): JSONResponse { try { if (empty($name)) { if (!empty($file['url'])) { $name = rawurldecode(pathinfo($file['url'], PATHINFO_FILENAME)); } } if (empty($name)) { // The name of file to sign is mandatory. This phrase is used when we do a request to API sending a file to sign. throw new \Exception($this->l10n->t('Name is mandatory')); } $this->validateHelper->validateNewFile([ 'file' => $file, 'userManager' => $this->userSession->getUser(), ]); $this->validateHelper->canRequestSign($this->userSession->getUser()); $node = $this->fileService->getNodeFromData([ 'userManager' => $this->userSession->getUser(), 'name' => $name, 'file' => $file, 'settings' => $settings ]); return new JSONResponse( [ 'message' => $this->l10n->t('Success'), 'name' => $name, 'id' => $node->getId(), 'etag' => $node->getEtag(), 'path' => $node->getPath(), 'type' => $node->getType(), ], Http::STATUS_OK ); } catch (\Exception $e) { return new JSONResponse( [ 'message' => $e->getMessage(), ], Http::STATUS_UNPROCESSABLE_ENTITY, ); } } }