From 816a831ff1eb7a5419c32edbdcc75f234863b3ce Mon Sep 17 00:00:00 2001 From: Alex Zamponi <562324+alexz707@users.noreply.github.com> Date: Fri, 26 Apr 2024 15:53:28 +0200 Subject: [PATCH 1/2] First test to fix pimcore/web-to-print-bundle#73 --- src/Controller/Document/PrintpageControllerBase.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Controller/Document/PrintpageControllerBase.php b/src/Controller/Document/PrintpageControllerBase.php index 0a17761..f76cdc3 100644 --- a/src/Controller/Document/PrintpageControllerBase.php +++ b/src/Controller/Document/PrintpageControllerBase.php @@ -272,6 +272,18 @@ public function activeGenerateProcessAction(Request $request): JsonResponse $statusUpdate = Processor::getInstance()->getStatusUpdate($document->getId()); } + + // Workaround for a bug which happens when the local filesystem is using a NFS with cache. + // This invalidates the cache and the file_exists() function returns the correct value. + // TODO: check if there are any side effects + try { + if ($dh = opendir(dirname($document->getPdfFileName()))) { + closedir($dh); + } + } catch (\Exception) { + } + + return $this->adminJson([ 'activeGenerateProcess' => !empty($inProgress), 'date' => $date, From 436ab61e8d19b24cb2b9528458dd70569e14060b Mon Sep 17 00:00:00 2001 From: Alex Zamponi <562324+alexz707@users.noreply.github.com> Date: Tue, 30 Apr 2024 10:53:53 +0200 Subject: [PATCH 2/2] Fix for pimcore/web-to-print-bundle#73 --- .../Document/PrintpageControllerBase.php | 55 ++++++++++++------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/src/Controller/Document/PrintpageControllerBase.php b/src/Controller/Document/PrintpageControllerBase.php index f76cdc3..d20a796 100644 --- a/src/Controller/Document/PrintpageControllerBase.php +++ b/src/Controller/Document/PrintpageControllerBase.php @@ -42,7 +42,7 @@ abstract class PrintpageControllerBase extends DocumentControllerBase * * @return JsonResponse * - * @throws \Exception + * @throws Exception */ public function getDataByIdAction(Request $request): JsonResponse { @@ -250,7 +250,7 @@ protected function setValuesToDocument(Request $request, Document $document): vo * * @return JsonResponse * - * @throws \Exception + * @throws Exception */ public function activeGenerateProcessAction(Request $request): JsonResponse { @@ -272,23 +272,11 @@ public function activeGenerateProcessAction(Request $request): JsonResponse $statusUpdate = Processor::getInstance()->getStatusUpdate($document->getId()); } - - // Workaround for a bug which happens when the local filesystem is using a NFS with cache. - // This invalidates the cache and the file_exists() function returns the correct value. - // TODO: check if there are any side effects - try { - if ($dh = opendir(dirname($document->getPdfFileName()))) { - closedir($dh); - } - } catch (\Exception) { - } - - return $this->adminJson([ 'activeGenerateProcess' => !empty($inProgress), 'date' => $date, 'message' => $document->getLastGenerateMessage(), - 'downloadAvailable' => file_exists($document->getPdfFileName()), + 'downloadAvailable' => $this->checkFileExists($document->getPdfFileName()), 'statusUpdate' => $statusUpdate, ]); } @@ -300,7 +288,7 @@ public function activeGenerateProcessAction(Request $request): JsonResponse * * @return BinaryFileResponse * - * @throws \Exception + * @throws Exception */ public function pdfDownloadAction(Request $request): BinaryFileResponse { @@ -310,7 +298,7 @@ public function pdfDownloadAction(Request $request): BinaryFileResponse throw $this->createNotFoundException('Document with id ' . $request->get('id') . ' not found.'); } - if (file_exists($document->getPdfFileName())) { + if ($this->checkFileExists($document->getPdfFileName())) { $response = new BinaryFileResponse($document->getPdfFileName()); $response->headers->set('Content-Type', 'application/pdf'); if ($request->get('download')) { @@ -331,7 +319,7 @@ public function pdfDownloadAction(Request $request): BinaryFileResponse * * @return JsonResponse * - * @throws \Exception + * @throws Exception */ public function startPdfGenerationAction(Request $request, \Pimcore\Config $config): JsonResponse { @@ -416,7 +404,7 @@ public function getProcessingOptionsAction(Request $request): JsonResponse private function getStoredProcessingOptions(int $documentId): array { $filename = PIMCORE_SYSTEM_TEMP_DIRECTORY . DIRECTORY_SEPARATOR . 'web2print-processingoptions-' . $documentId . '_' . $this->getAdminUser()->getId() . '.psf'; - if (file_exists($filename)) { + if ($this->checkFileExists($filename)) { $options = \Pimcore\Tool\Serialize::unserialize(file_get_contents($filename)); if (is_array($options)) { return $options; @@ -438,7 +426,7 @@ private function saveProcessingOptions(int $documentId, array $options): void * * @return JsonResponse * - * @throws \Exception + * @throws Exception */ public function cancelGenerationAction(Request $request): JsonResponse { @@ -446,4 +434,31 @@ public function cancelGenerationAction(Request $request): JsonResponse return $this->adminJson(['success' => true]); } + + /** + * Checks if a file exists on the filesystem. + * @param string $filePath + * @return bool + */ + private function checkFileExists(string $filePath): bool + { + $this->invalidateFsCacheFor($filePath); + return file_exists($filePath); + } + + /** + * Invalidates the FS cache for a given file path by opening and closing the directory. + * This is a workaround for a bug which happens when the local filesystem is using a NFS with cache. + * @param string $filePath + * @return void + */ + private function invalidateFsCacheFor(string $filePath): void + { + try { + if ($dh = opendir(dirname($filePath))) { + closedir($dh); + } + } catch (Exception) { + } + } }