From c4cc02d472aa1fec3e7287e1e09f9fb80af826d7 Mon Sep 17 00:00:00 2001 From: Ruslan Baidan Date: Thu, 3 Oct 2024 10:39:09 +0200 Subject: [PATCH] Fixed the word generation response. --- .../ApiDeliveriesModelsController.php | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/Controller/ApiDeliveriesModelsController.php b/src/Controller/ApiDeliveriesModelsController.php index 7a0f39b..c7cd5ca 100644 --- a/src/Controller/ApiDeliveriesModelsController.php +++ b/src/Controller/ApiDeliveriesModelsController.php @@ -7,6 +7,7 @@ namespace Monarc\BackOffice\Controller; +use Laminas\Diactoros\Response; use Monarc\Core\Controller\AbstractController; use Monarc\Core\Controller\Handler\ControllerRequestResponseHandlerTrait; use Monarc\Core\Exception\Exception; @@ -75,21 +76,22 @@ public function get($id) $entity = $this->getService()->getEntity($id); if (!empty($entity)) { $lang = $this->params()->fromQuery('lang', 1); - if (isset($entity['path' . $lang]) && file_exists($entity['path' . $lang])) { - $name = pathinfo($entity['path' . $lang], PATHINFO_BASENAME); + $pathModel = getenv('APP_CONF_DIR') ?: ''; + $currentPath = $pathModel . $entity['path' . $lang]; + if (isset($entity['path' . $lang]) && file_exists($currentPath)) { + $filename = pathinfo($currentPath)['basename']; - $fileContents = file_get_contents($entity['path' . $lang]); + $fileContents = file_get_contents($currentPath); if ($fileContents !== false) { - $response = $this->getResponse(); - $response->setContent($fileContents); - - $headers = $response->getHeaders(); - $headers->clearHeaders() - ->addHeaderLine('Content-Type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') - ->addHeaderLine('Content-Disposition', 'attachment; filename="' . utf8_decode($name) . '"') - ->addHeaderLine('Content-Length', \strlen($fileContents)); - - return $this->response; + $stream = fopen('php://memory', 'rb+'); + fwrite($stream, $fileContents); + rewind($stream); + + return new Response($stream, 200, [ + 'Content-Type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', + 'Content-Length' => strlen($fileContents), + 'Content-Disposition' => 'attachment; filename="' . utf8_decode($filename) . '"', + ]); } } }