getResource(tao_helpers_Uri::decode($this->getRequestParameter('uri'))); $lang = DEFAULT_LANG; $previewUrl = taoItems_models_classes_ItemsService::singleton()->getPreviewUrl($item, $lang); if (null === $previewUrl) { throw new OntologyItemNotFoundException(); } $this->forwardUrl($previewUrl); } /** * @requiresRight uri READ */ public function index() { $item = $this->getResource(tao_helpers_Uri::decode($this->getRequestParameter('uri'))); $itemService = taoItems_models_classes_ItemsService::singleton(); if ($itemService->hasItemContent($item) && $itemService->isItemModelDefined($item)) { //this is this url that will contains the preview //@see taoItems_actions_LegacyPreviewApi $previewUrl = $this->getPreviewUrl($item); $this->setData('previewUrl', $previewUrl); $this->setData('client_config_url', $this->getClientConfigUrl()); $this->setData('resultServer', $this->getResultServer()); } $this->setData('state', html_entity_decode($this->getRequestParameter('state'))); $this->setView('ItemPreview/index.tpl', 'taoItems'); } protected function getPreviewUrl($item, $options = []) { $code = base64_encode($item->getUri()); return _url('render/' . $code . '/index', 'ItemPreview', 'taoItems', $options); } public function render() { $relPath = tao_helpers_Request::getRelativeUrl(); [$extension, $module, $action, $codedUri, $path] = explode('/', $relPath, 5); $path = rawurldecode($path); $uri = base64_decode($codedUri); if (!common_Utils::isUri($uri)) { throw new common_exception_BadRequest('"' . $codedUri . '" does not decode to a valid item URI'); } $item = $this->getResource($uri); if ($path === 'index') { $this->renderItem($item); } else { $this->renderResource($item, $path); } } protected function getRenderedItem($item) { $itemModel = $item->getOnePropertyValue($this->getProperty(taoItems_models_classes_ItemsService::PROPERTY_ITEM_MODEL)); $impl = taoItems_models_classes_ItemsService::singleton()->getItemModelImplementation($itemModel); if (is_null($impl)) { throw new common_Exception('preview not supported for this item type ' . $itemModel->getUri()); } return $impl->render($item, ''); } /** * Add the rendered item to psr7 response * * @param $item * @throws common_Exception */ private function renderItem($item) { $this->response = $this->response->withBody(stream_for($this->getRenderedItem($item))); } /** * @param $item * @param $path * * @throws common_Exception * @throws common_exception_Error * @throws tao_models_classes_FileNotFoundException */ private function renderResource($item, $path) { $lang = $this->getSession()->getDataLanguage(); $resolver = new ItemMediaResolver($item, $lang); $asset = $resolver->resolve($path); $mediaSource = $asset->getMediaSource(); $mediaIdentifier = $asset->getMediaIdentifier(); if ($mediaSource instanceof HttpSource || Base64::isEncodedImage($mediaIdentifier)) { throw new common_Exception('Only tao files available for rendering through item preview'); } $info = $mediaSource->getFileInfo($mediaIdentifier); $stream = $mediaSource->getFileStream($mediaIdentifier); \tao_helpers_Http::returnStream($stream, $info['mime']); } /** * Get the ResultServer API call to be used by the item. * * @return string A string representing JavaScript instructions. */ protected function getResultServer() { return [ 'module' => 'taoItems/runtime/ConsoleResultServer' ]; } }