*/ class tao_actions_TaskQueueWebApi extends tao_actions_CommonModule { use FilesystemAwareTrait; use FileReferenceSerializerAwareTrait; use HttpJsonResponseTrait; private const PARAMETER_TASK_ID = 'taskId'; private const PARAMETER_LIMIT = 'limit'; private const PARAMETER_OFFSET = 'offset'; private const ALL = 'all'; /** * @throws common_exception_NotImplemented * @throws Exception */ public function getAll(): void { $this->checkIfIsXmlHttpRequest(); $taskLogService = $this->getTaskLogService(); $limit = $offset = null; if ($this->hasRequestParameter(self::PARAMETER_LIMIT)) { $limit = (int) $this->getRequestParameter(self::PARAMETER_LIMIT); } if ($this->hasRequestParameter(self::PARAMETER_OFFSET)) { $offset = (int) $this->getRequestParameter(self::PARAMETER_OFFSET); } $collection = new SimpleManagementCollectionDecorator( $taskLogService->findAvailableByUser($this->getSessionUserUri(), $limit, $offset), $taskLogService, $this->getFileSystemService(), $this->getFileReferenceSerializer(), false ); $this->setSuccessJsonResponse($collection->toArray()); } /** * @throws common_exception_NotImplemented * @throws Exception */ public function get(): void { $this->checkIfIsXmlHttpRequest(); try { $this->checkIfTaskIdExists(); $taskLogService = $this->getTaskLogService(); $entity = $taskLogService->getByIdAndUser( $this->getRequestParameter(self::PARAMETER_TASK_ID), $this->getSessionUserUri() ); $this->setSuccessJsonResponse((new RedirectUrlEntityDecorator( new HasFileEntityDecorator( new CategoryEntityDecorator($entity, $taskLogService), $this->getFileSystemService(), $this->getFileReferenceSerializer() ), $taskLogService, common_session_SessionManager::getSession()->getUser() ))->toArray()); } catch (Exception $e) { $this->setErrorJsonResponse( $e instanceof common_exception_UserReadableException ? $e->getUserMessage() : $e->getMessage(), $e->getCode() ); } } /** * @throws common_exception_NotImplemented * @throws Exception */ public function stats(): void { $this->checkIfIsXmlHttpRequest(); $this->setSuccessJsonResponse( $this->getTaskLogService()->getStats($this->getSessionUserUri())->toArray() ); } /** * @throws common_exception_NotImplemented * @throws Exception */ public function archive() { $this->checkIfIsXmlHttpRequest(); try { $this->checkIfTaskIdExists(); $taskIds = $this->detectTaskIds(); $taskLogService = $this->getTaskLogService(); $filter = $taskIds === static::ALL ? (new TaskLogFilter())->availableForArchived($this->getSessionUserUri()) : (new TaskLogFilter())->addAvailableFilters($this->getSessionUserUri())->in(TaskLogBrokerInterface::COLUMN_ID, $taskIds); return $this->returnJson([ 'success' => (bool) $taskLogService->archiveCollection($taskLogService->search($filter)) ]); } catch (Exception $e) { $this->setErrorJsonResponse( $e instanceof common_exception_UserReadableException ? $e->getUserMessage() : $e->getMessage(), $e instanceof \common_exception_NotFound ? 404 : $e->getCode() ); } } /** * @throws Exception */ public function cancel() { $this->checkIfIsXmlHttpRequest(); try { $this->checkIfTaskIdExists(); $taskIds = $this->detectTaskIds(); $taskLogService = $this->getTaskLogService(); $filter = $taskIds === static::ALL ? (new TaskLogFilter())->availableForCancelled($this->getSessionUserUri()) : (new TaskLogFilter())->addAvailableFilters($this->getSessionUserUri())->in(TaskLogBrokerInterface::COLUMN_ID, $taskIds); return $this->returnJson([ 'success' => (bool) $taskLogService->cancelCollection($taskLogService->search($filter)) ]); } catch (Exception $e) { $this->setErrorJsonResponse( $e instanceof common_exception_UserReadableException ? $e->getUserMessage() : $e->getMessage(), $e instanceof \common_exception_NotFound ? 404 : $e->getCode() ); } } /** * Download the file created by task. */ public function download() { try { $this->checkIfTaskIdExists(); $taskLogEntity = $this->getTaskLogService()->getByIdAndUser( $this->getRequestParameter(self::PARAMETER_TASK_ID), $this->getSessionUserUri() ); if (!$taskLogEntity->getStatus()->isCompleted()) { throw new \common_Exception('Task "' . $taskLogEntity->getId() . '" is not downloadable.'); } $fileNameOrSerial = $taskLogEntity->getFileNameFromReport(); if (empty($fileNameOrSerial)) { throw new \common_Exception('Filename not found in report.'); } // first try to get a referenced file is it is a serial $file = $this->getReferencedFile($fileNameOrSerial); // if no file let's try the task queue storage if (is_null($file)) { $file = $this->getQueueStorageFile($fileNameOrSerial); } if (!$file) { throw new \common_exception_NotFound('File not found.'); } header('Set-Cookie: fileDownload=true'); setcookie('fileDownload', 'true', 0, '/'); header('Content-Disposition: attachment; filename="' . $file->getBasename() . '"'); header('Content-Type: ' . $file->getMimeType()); \tao_helpers_Http::returnStream($file->readPsrStream()); exit(); } catch (Exception $e) { $this->setErrorJsonResponse( $e instanceof common_exception_UserReadableException ? $e->getUserMessage() : $e->getMessage(), $e->getCode() ); } } /** * @throws common_exception_MissingParameter */ protected function checkIfTaskIdExists(): void { if (!$this->hasRequestParameter(self::PARAMETER_TASK_ID)) { throw new common_exception_MissingParameter(self::PARAMETER_TASK_ID, $this->getRequestURI()); } } /** * @throws Exception */ protected function checkIfIsXmlHttpRequest(): void { if (!$this->isXmlHttpRequest()) { throw new Exception('Only ajax call allowed.'); } } /** * @return array|string */ protected function detectTaskIds() { $taskIdsParams = $this->getRequestParameter(self::PARAMETER_TASK_ID); if (is_array($taskIdsParams)) { return $taskIdsParams; } elseif ($taskIdsParams === static::ALL) { return static::ALL; } else { return [$taskIdsParams]; } } /** * @throws common_exception_Error */ protected function getSessionUserUri(): string { return $this->getSession()->getUserUri(); } protected function getFileReferenceSerializer(): FileReferenceSerializer { return $this->getServiceLocator()->get(FileReferenceSerializer::SERVICE_ID); } protected function getFileSystemService(): FileSystemService { return $this->getServiceLocator()->get(FileSystemService::SERVICE_ID); } protected function getTaskLogService(): TaskLogInterface { return $this->getServiceLocator()->get(TaskLogInterface::SERVICE_ID); } }