Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Preview PDF is not shown on NFS shares #76

Merged
merged 3 commits into from
Apr 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions src/Controller/Document/PrintpageControllerBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ abstract class PrintpageControllerBase extends DocumentControllerBase
*
* @return JsonResponse
*
* @throws \Exception
* @throws Exception
*/
public function getDataByIdAction(Request $request): JsonResponse
{
Expand Down Expand Up @@ -250,7 +250,7 @@ protected function setValuesToDocument(Request $request, Document $document): vo
*
* @return JsonResponse
*
* @throws \Exception
* @throws Exception
*/
public function activeGenerateProcessAction(Request $request): JsonResponse
{
Expand All @@ -276,7 +276,7 @@ public function activeGenerateProcessAction(Request $request): JsonResponse
'activeGenerateProcess' => !empty($inProgress),
'date' => $date,
'message' => $document->getLastGenerateMessage(),
'downloadAvailable' => file_exists($document->getPdfFileName()),
'downloadAvailable' => $this->checkFileExists($document->getPdfFileName()),
'statusUpdate' => $statusUpdate,
]);
}
Expand All @@ -288,7 +288,7 @@ public function activeGenerateProcessAction(Request $request): JsonResponse
*
* @return BinaryFileResponse
*
* @throws \Exception
* @throws Exception
*/
public function pdfDownloadAction(Request $request): BinaryFileResponse
{
Expand All @@ -298,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')) {
Expand All @@ -319,7 +319,7 @@ public function pdfDownloadAction(Request $request): BinaryFileResponse
*
* @return JsonResponse
*
* @throws \Exception
* @throws Exception
*/
public function startPdfGenerationAction(Request $request, \Pimcore\Config $config): JsonResponse
{
Expand Down Expand Up @@ -404,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;
Expand All @@ -426,12 +426,39 @@ private function saveProcessingOptions(int $documentId, array $options): void
*
* @return JsonResponse
*
* @throws \Exception
* @throws Exception
*/
public function cancelGenerationAction(Request $request): JsonResponse
{
Processor::getInstance()->cancelGeneration((int)$request->get('id'));

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) {
}
}
}
Loading