/** * Process document and write the result in the document cache. * * When the original document is required, create either a symbolic link with the * original document in the cache dir, or copy it in the cache dir if it's not already done. * * This method updates the cache_file_path and file_url attributes of the event * * @param DocumentEvent $event Event * * @throws \Thelia\Exception\DocumentException * @throws \InvalidArgumentException , DocumentException */ public function processDocument(DocumentEvent $event) { $subdir = $event->getCacheSubdirectory(); $sourceFile = $event->getSourceFilepath(); if (null == $subdir || null == $sourceFile) { throw new \InvalidArgumentException("Cache sub-directory and source file path cannot be null"); } $originalDocumentPathInCache = $this->getCacheFilePath($subdir, $sourceFile, true); if (!file_exists($originalDocumentPathInCache)) { if (!file_exists($sourceFile)) { throw new DocumentException(sprintf("Source document file %s does not exists.", $sourceFile)); } $mode = ConfigQuery::read('original_document_delivery_mode', 'symlink'); if ($mode == 'symlink') { if (false == symlink($sourceFile, $originalDocumentPathInCache)) { throw new DocumentException(sprintf("Failed to create symbolic link for %s in %s document cache directory", basename($sourceFile), $subdir)); } } else { // mode = 'copy' if (false == @copy($sourceFile, $originalDocumentPathInCache)) { throw new DocumentException(sprintf("Failed to copy %s in %s document cache directory", basename($sourceFile), $subdir)); } } } // Compute the document URL $documentUrl = $this->getCacheFileURL($subdir, basename($originalDocumentPathInCache)); // Update the event with file path and file URL $event->setDocumentPath($documentUrl); $event->setDocumentUrl(URL::getInstance()->absoluteUrl($documentUrl, null, URL::PATH_TO_FILE)); }
public function parseResults(LoopResult $loopResult) { foreach ($loopResult->getResultDataCollection() as $result) { // Create document processing event $event = new DocumentEvent($this->request); // Put source document file path $source_filepath = sprintf("%s%s/%s/%s", THELIA_ROOT, ConfigQuery::read('documents_library_path', 'local/media/documents'), $this->objectType, $result->getFile()); $event->setSourceFilepath($source_filepath); $event->setCacheSubdirectory($this->objectType); try { // Dispatch document processing event $this->dispatcher->dispatch(TheliaEvents::DOCUMENT_PROCESS, $event); $loopResultRow = new LoopResultRow($result); $loopResultRow->set("ID", $result->getId())->set("LOCALE", $this->locale)->set("DOCUMENT_URL", $event->getDocumentUrl())->set("DOCUMENT_PATH", $event->getDocumentPath())->set("ORIGINAL_DOCUMENT_PATH", $source_filepath)->set("TITLE", $result->getVirtualColumn('i18n_TITLE'))->set("CHAPO", $result->getVirtualColumn('i18n_CHAPO'))->set("DESCRIPTION", $result->getVirtualColumn('i18n_DESCRIPTION'))->set("POSTSCRIPTUM", $result->getVirtualColumn('i18n_POSTSCRIPTUM'))->set("VISIBLE", $result->getVisible())->set("POSITION", $result->getPosition())->set("OBJECT_TYPE", $this->objectType)->set("OBJECT_ID", $this->objectId); $loopResult->addRow($loopResultRow); } catch (\Exception $ex) { // Ignore the result and log an error Tlog::getInstance()->addError(sprintf("Failed to process document in document loop: %s", $ex->getMessage())); } } return $loopResult; }
/** * Try to clear directory ouside of the cache * * @expectedException \InvalidArgumentException */ public function testClearUnallowedPathCache() { $event = new DocumentEvent($this->request); $event->setCacheSubdirectory('../../../..'); $document = new Document($this->getFileManager()); $document->clearCache($event); }