Skip to content

Commit

Permalink
Fix for #73
Browse files Browse the repository at this point in the history
(cherry picked from commit 436ab61)
  • Loading branch information
alexz707 authored and kingjia90 committed Apr 30, 2024
1 parent e95efc4 commit 66f9a61
Showing 1 changed file with 35 additions and 8 deletions.
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) {
}
}
}

0 comments on commit 66f9a61

Please sign in to comment.