/** * @param $documentId * @throws \Exception */ public function startPdfGeneration($documentId) { $jobConfigFile = $this->loadJobConfigObject($documentId); $document = $this->getPrintDocument($documentId); // check if there is already a generating process running, wait if so ... Model\Tool\Lock::acquire($document->getLockKey(), 0); try { $pdf = $this->buildPdf($document, $jobConfigFile->config); file_put_contents($document->getPdfFileName(), $pdf); \Pimcore::getEventManager()->trigger("document.print.postPdfGeneration", $document, ["filename" => $document->getPdfFileName(), "pdf" => $pdf]); $creationDate = \Zend_Date::now(); $document->setLastGenerated($creationDate->get() + 1); $document->save(); } catch (\Exception $e) { $document->save(); \Logger::err($e); } Model\Tool\Lock::release($document->getLockKey()); Model\Tool\TmpStore::delete($document->getLockKey()); @unlink($this->getJobConfigFile($documentId)); }
/** * */ public function generate() { $errorImage = PIMCORE_PATH . '/static6/img/filetype-not-supported.png'; $deferred = false; $generated = false; if (!$this->asset) { $this->filesystemPath = $errorImage; } elseif (!$this->filesystemPath) { $cs = $this->asset->getCustomSetting("image_thumbnail_time"); $im = $this->asset->getCustomSetting("image_thumbnail_asset"); if ($im || $this->imageAsset) { if ($this->imageAsset) { $im = $this->imageAsset; } else { $im = Model\Asset::getById($im); } if ($im instanceof Image) { $imageThumbnail = $im->getThumbnail($this->getConfig()); $this->filesystemPath = $imageThumbnail->getFileSystemPath(); } } if (!$this->filesystemPath) { $timeOffset = $this->timeOffset; if (!$this->timeOffset && $cs) { $timeOffset = $cs; } // fallback if (!$timeOffset) { $timeOffset = ceil($this->asset->getDuration() / 3); } $converter = \Pimcore\Video::getInstance(); $converter->load($this->asset->getFileSystemPath()); $path = PIMCORE_TEMPORARY_DIRECTORY . "/video-image-cache/video_" . $this->asset->getId() . "__thumbnail_" . $timeOffset . ".png"; if (!is_dir(dirname($path))) { File::mkdir(dirname($path)); } if (!is_file($path)) { $lockKey = "video_image_thumbnail_" . $this->asset->getId() . "_" . $timeOffset; Model\Tool\Lock::acquire($lockKey); // after we got the lock, check again if the image exists in the meantime - if not - generate it if (!is_file($path)) { $converter->saveImage($path, $timeOffset); $generated = true; } Model\Tool\Lock::release($lockKey); } if ($this->getConfig()) { $this->getConfig()->setFilenameSuffix("time-" . $timeOffset); try { $path = Image\Thumbnail\Processor::process($this->asset, $this->getConfig(), $path, $deferred, true, $generated); } catch (\Exception $e) { Logger::error("Couldn't create image-thumbnail of video " . $this->asset->getRealFullPath()); Logger::error($e); $path = $errorImage; } } $this->filesystemPath = $path; } \Pimcore::getEventManager()->trigger("asset.video.image-thumbnail", $this, ["deferred" => $deferred, "generated" => $generated]); } }
/** * @param null $path * @return null|string|void * @throws \Exception */ public function getPdf($path = null) { if ($path) { $path = $this->preparePath($path); } $pdfPath = null; if (!$path && $this->path) { $path = $this->path; } try { // if the document is already an PDF, delegate the call directly to parent::getPdf() (Ghostscript) $pdfPath = parent::getPdf($path); return $pdfPath; } catch (\Exception $e) { // nothing to do, delegate to libreoffice } $pdfFile = PIMCORE_TEMPORARY_DIRECTORY . "/document-pdf-cache/document_" . md5($path . filemtime($path)) . "__libreoffice.pdf"; if (!is_dir(dirname($pdfFile))) { File::mkdir(dirname($pdfFile)); } $lockKey = "soffice"; if (!file_exists($pdfFile)) { // a list of all available filters is here: // http://cgit.freedesktop.org/libreoffice/core/tree/filter/source/config/fragments/filters $cmd = self::getLibreOfficeCli() . " --headless --nologo --nofirststartwizard --norestore --convert-to pdf:writer_web_pdf_Export --outdir " . escapeshellarg(PIMCORE_SYSTEM_TEMP_DIRECTORY) . " " . escapeshellarg($path); Model\Tool\Lock::acquire($lockKey); // avoid parallel conversions $out = Console::exec($cmd, PIMCORE_LOG_DIRECTORY . "/libreoffice-pdf-convert.log", 240); Model\Tool\Lock::release($lockKey); Logger::debug("LibreOffice Output was: " . $out); $tmpName = PIMCORE_SYSTEM_TEMP_DIRECTORY . "/" . preg_replace("/\\." . File::getFileExtension($path) . "\$/", ".pdf", basename($path)); if (file_exists($tmpName)) { File::rename($tmpName, $pdfFile); $pdfPath = $pdfFile; } else { $message = "Couldn't convert document to PDF: " . $path . " with the command: '" . $cmd . "'"; Logger::error($message); throw new \Exception($message); } } else { $pdfPath = $pdfFile; } return $pdfPath; }
/** * @param $processId */ public static function execute($processId) { $instance = new self(); $instance->setProcessId($processId); $instanceItem = TmpStore::get($instance->getJobStoreId($processId)); $instance = $instanceItem->getData(); $formats = []; $conversionStatus = "finished"; // check if there is already a transcoding process running, wait if so ... Model\Tool\Lock::acquire("video-transcoding", 7200, 10); // expires after 2 hrs, refreshes every 10 secs $asset = Model\Asset::getById($instance->getAssetId()); // start converting foreach ($instance->queue as $converter) { try { Logger::info("start video " . $converter->getFormat() . " to " . $converter->getDestinationFile()); $success = $converter->save(); Logger::info("finished video " . $converter->getFormat() . " to " . $converter->getDestinationFile()); File::rename($converter->getDestinationFile(), $converter->getStorageFile()); // set proper permissions @chmod($converter->getStorageFile(), File::getDefaultMode()); if ($success) { $formats[$converter->getFormat()] = str_replace($asset->getVideoThumbnailSavePath(), "", $converter->getStorageFile()); } else { $conversionStatus = "error"; } $converter->destroy(); } catch (\Exception $e) { Logger::error($e); } } Model\Tool\Lock::release("video-transcoding"); if ($asset) { $customSetting = $asset->getCustomSetting("thumbnails"); $customSetting = is_array($customSetting) ? $customSetting : []; if (array_key_exists($instance->getConfig()->getName(), $customSetting) && array_key_exists("formats", $customSetting[$instance->getConfig()->getName()]) && is_array($customSetting[$instance->getConfig()->getName()]["formats"])) { $formats = array_merge($customSetting[$instance->getConfig()->getName()]["formats"], $formats); } $customSetting[$instance->getConfig()->getName()] = ["status" => $conversionStatus, "formats" => $formats]; $asset->setCustomSetting("thumbnails", $customSetting); $asset->save(); } TmpStore::delete($instance->getJobStoreId()); }
public function wordExportDownloadAction() { $id = $this->getParam("id"); $exportFile = PIMCORE_SYSTEM_TEMP_DIRECTORY . "/" . $id . ".html"; // add closing body/html //$f = fopen($exportFile, "a+"); //fwrite($f, "</body></html>"); //fclose($f); // should be done via Pimcore_Document(_Adapter_LibreOffice) in the future if (\Pimcore\Document::isFileTypeSupported("docx")) { $lockKey = "soffice"; Model\Tool\Lock::acquire($lockKey); // avoid parallel conversions of the same document $out = Tool\Console::exec(\Pimcore\Document\Adapter\LibreOffice::getLibreOfficeCli() . ' --headless --convert-to docx:"Office Open XML Text" --outdir ' . PIMCORE_TEMPORARY_DIRECTORY . " " . $exportFile); \Logger::debug("LibreOffice Output was: " . $out); $tmpName = PIMCORE_TEMPORARY_DIRECTORY . "/" . preg_replace("/\\." . File::getFileExtension($exportFile) . "\$/", ".docx", basename($exportFile)); Model\Tool\Lock::release($lockKey); // end what should be done in Pimcore_Document header("Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document"); header('Content-Disposition: attachment; filename="' . basename($tmpName) . '"'); } else { // no conversion, output html file $tmpName = $exportFile; header("Content-Type: text/html"); header('Content-Disposition: attachment; filename="' . basename($tmpName) . '"'); } while (@ob_end_flush()) { } flush(); readfile($tmpName); @unlink($exportFile); @unlink($tmpName); exit; }
/** * @param $thumbnailName * @param null $timeOffset * @param null $imageAsset * @return mixed|string * @throws \Exception */ public function getImageThumbnail($thumbnailName, $timeOffset = null, $imageAsset = null) { if (!\Pimcore\Video::isAvailable()) { \Logger::error("Couldn't create image-thumbnail of video " . $this->getFullPath() . " no video adapter is available"); return "/pimcore/static/img/filetype-not-supported.png"; } $cs = $this->getCustomSetting("image_thumbnail_time"); $im = $this->getCustomSetting("image_thumbnail_asset"); if ($im || $imageAsset) { if ($im) { $imageAsset = Model\Asset::getById($im); } if ($imageAsset instanceof Image) { return $imageAsset->getThumbnail($thumbnailName); } } if (!$timeOffset && $cs) { $timeOffset = $cs; } // fallback if (!$timeOffset) { $timeOffset = ceil($this->getDuration() / 3); } $converter = \Pimcore\Video::getInstance(); $converter->load($this->getFileSystemPath()); $path = PIMCORE_TEMPORARY_DIRECTORY . "/video-image-cache/video_" . $this->getId() . "__thumbnail_" . $timeOffset . ".png"; if (!is_dir(dirname($path))) { File::mkdir(dirname($path)); } if (!is_file($path)) { $lockKey = "video_image_thumbnail_" . $this->getId() . "_" . $timeOffset; Model\Tool\Lock::acquire($lockKey); // after we got the lock, check again if the image exists in the meantime - if not - generate it if (!is_file($path)) { $converter->saveImage($path, $timeOffset); } Model\Tool\Lock::release($lockKey); } $thumbnail = $this->getImageThumbnailConfig($thumbnailName); if ($thumbnail) { $thumbnail->setFilenameSuffix("time-" . $timeOffset); try { $path = Image\Thumbnail\Processor::process($this, $thumbnail, $path); } catch (\Exception $e) { \Logger::error("Couldn't create image-thumbnail of video " . $this->getFullPath()); \Logger::error($e); return "/pimcore/static/img/filetype-not-supported.png"; } } $path = preg_replace("@^" . preg_quote(PIMCORE_DOCUMENT_ROOT, "@") . "@", "", $path); return $path; }
/** * @param $processId */ public static function execute($processId) { $instance = new self(); $instance->setProcessId($processId); $instanceItem = TmpStore::get($instance->getJobStoreId($processId)); $instance = $instanceItem->getData(); $formats = array(); $overallStatus = array(); $conversionStatus = "finished"; // set overall status for all formats to 0 foreach ($instance->queue as $converter) { $overallStatus[$converter->getFormat()] = 0; } // check if there is already a transcoding process running, wait if so ... Model\Tool\Lock::acquire("video-transcoding", 7200, 10); // expires after 2 hrs, refreshes every 10 secs // start converting foreach ($instance->queue as $converter) { try { \Logger::info("start video " . $converter->getFormat() . " to " . $converter->getDestinationFile()); $converter->save(); while (!$converter->isFinished()) { sleep(5); $overallStatus[$converter->getFormat()] = $converter->getConversionStatus(); $a = 0; foreach ($overallStatus as $f => $s) { $a += $s; } $a = $a / count($overallStatus); $instance->setStatus($a); $instance->save(); } \Logger::info("finished video " . $converter->getFormat() . " to " . $converter->getDestinationFile()); // set proper permissions @chmod($converter->getDestinationFile(), File::getDefaultMode()); if ($converter->getConversionStatus() !== "error") { $formats[$converter->getFormat()] = str_replace(PIMCORE_DOCUMENT_ROOT, "", $converter->getDestinationFile()); } else { $conversionStatus = "error"; } $converter->destroy(); } catch (\Exception $e) { \Logger::error($e); } } Model\Tool\Lock::release("video-transcoding"); $asset = Model\Asset::getById($instance->getAssetId()); if ($asset) { $customSetting = $asset->getCustomSetting("thumbnails"); $customSetting = is_array($customSetting) ? $customSetting : array(); if (array_key_exists($instance->getConfig()->getName(), $customSetting) && array_key_exists("formats", $customSetting[$instance->getConfig()->getName()]) && is_array($customSetting[$instance->getConfig()->getName()]["formats"])) { $formats = array_merge($customSetting[$instance->getConfig()->getName()]["formats"], $formats); } $customSetting[$instance->getConfig()->getName()] = array("status" => $conversionStatus, "formats" => $formats); $asset->setCustomSetting("thumbnails", $customSetting); $asset->save(); } TmpStore::delete($instance->getJobStoreId()); }