_handlerPath = '_library'; $this->_endpoints = [ 'GET' => [ [ 'pattern' => $this->getEndpointPattern(), 'handler' => [$this, 'getLibrary'], 'roles' => [Role::ROLE_ID_SITE_ADMIN, Role::ROLE_ID_MANAGER, Role::ROLE_ID_SUB_EDITOR], ], ], ]; parent::__construct(); } /** * @copydoc PKPHandler::authorize */ public function authorize($request, &$args, $roleAssignments) { $this->addPolicy(new UserRolesRequiredPolicy($request), true); $rolePolicy = new PolicySet(PolicySet::COMBINING_PERMIT_OVERRIDES); foreach ($roleAssignments as $role => $operations) { $rolePolicy->addPolicy(new RoleBasedHandlerOperationPolicy($request, $role, $operations)); } $this->addPolicy($rolePolicy); if ($request->getUserVar('includeSubmissionId')) { $this->addPolicy(new SubmissionAccessPolicy($request, $args, $roleAssignments, 'includeSubmissionId')); } return parent::authorize($request, $args, $roleAssignments); } /** * Get a list of all files in the library * * @param array $args arguments * * @return APIResponse */ public function getLibrary(ServerRequestInterface $slimRequest, APIResponse $response, array $args) { /** @var LibraryFileDAO $libraryFileDao */ $libraryFileDao = DAORegistry::getDAO('LibraryFileDAO'); $submission = $this->getAuthorizedContextObject(Application::ASSOC_TYPE_SUBMISSION); $context = $this->getRequest()->getContext(); $contextId = $context->getId(); $libraryFileManager = new LibraryFileManager($contextId); $files = []; $params = $slimRequest->getQueryParams(); if (isset($params['includeSubmissionId'])) { $result = $libraryFileDao->getBySubmissionId($submission->getId()); while ($file = $result->next()) { $files[] = $this->fileToResponse($file, $libraryFileManager); } } $result = $libraryFileDao->getByContextId($contextId); while ($file = $result->next()) { $files[] = $this->fileToResponse($file, $libraryFileManager); } return $response->withJson([ 'items' => $files, 'itemsMax' => count($files), ], 200); } /** * Convert a file object to the JSON response object */ protected function fileToResponse(LibraryFile $file, LibraryFileManager $libraryFileManager): array { $request = Application::get()->getRequest(); $urlArgs = [ 'libraryFileId' => $file->getId(), ]; if ($file->getSubmissionId()) { $urlArgs['submissionId'] = $file->getSubmissionId(); } return [ 'id' => $file->getId(), 'filename' => $file->getServerFileName(), 'name' => $file->getName(null), 'mimetype' => $file->getFileType(), 'documentType' => Services::get('file')->getDocumentType($file->getFileType()), 'submissionId' => $file->getSubmissionId() ?? 0, 'type' => $file->getType(), 'typeName' => __($libraryFileManager->getTitleKeyFromType($file->getType())), 'url' => $request->getDispatcher()->url( $request, Application::ROUTE_COMPONENT, null, 'api.file.FileApiHandler', 'downloadLibraryFile', null, $urlArgs ), ]; } }