From 8a17ff2ca375cfcc4b61462ce438d122888937af Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Tue, 14 Apr 2020 19:35:21 +0300 Subject: [PATCH] attachment api: get file --- application/Espo/Controllers/Attachment.php | 17 +++++++++++++++++ application/Espo/Core/Application.php | 15 +++++++++------ application/Espo/Resources/routes.json | 8 ++++++++ application/Espo/Services/Attachment.php | 15 +++++++++++++++ 4 files changed, 49 insertions(+), 6 deletions(-) diff --git a/application/Espo/Controllers/Attachment.php b/application/Espo/Controllers/Attachment.php index 43ee85afe0..863b872891 100644 --- a/application/Espo/Controllers/Attachment.php +++ b/application/Espo/Controllers/Attachment.php @@ -57,4 +57,21 @@ class Attachment extends \Espo\Core\Controllers\Record return $this->getRecordService()->getCopiedAttachment($data)->getValueMap(); } + + public function getActionFile($params, $data, $request, $response) + { + $id = $params['id'] ?? null; + + if (!$id) throw new BadRequest(); + + $fileData = $this->getRecordService()->getFileData($id); + + $response->headers->set('Content-Type', $fileData->type); + $response->headers->set('Content-Disposition', 'Content-Disposition: attachment; filename="'.$fileData->name.'"'); + if ($fileData->size) { + $response->headers->set('Content-Length', $fileData->size); + } + + return $fileData->contents; + } } diff --git a/application/Espo/Core/Application.php b/application/Espo/Core/Application.php index 16b3c8710c..a802849864 100644 --- a/application/Espo/Core/Application.php +++ b/application/Espo/Core/Application.php @@ -304,13 +304,16 @@ class Application }); $this->getSlim()->hook('slim.after.router', function () use (&$slim) { - $slim->contentType('application/json'); + $response = $slim->response(); - $res = $slim->response(); - $res->header('Expires', '0'); - $res->header('Last-Modified', gmdate("D, d M Y H:i:s") . " GMT"); - $res->header('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'); - $res->header('Pragma', 'no-cache'); + if (!$response->headers->has('Content-Type')) { + $response->headers->set('Content-Type', 'application/json'); + } + + $response->headers->set('Expires', '0'); + $response->headers->set('Last-Modified', gmdate("D, d M Y H:i:s") . " GMT"); + $response->headers->set('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'); + $response->headers->set('Pragma', 'no-cache'); }); } diff --git a/application/Espo/Resources/routes.json b/application/Espo/Resources/routes.json index c664ab2a38..66759c8c81 100644 --- a/application/Espo/Resources/routes.json +++ b/application/Espo/Resources/routes.json @@ -245,6 +245,14 @@ "action": "update" } }, + { + "route": "/Attachment/file/:id", + "method": "get", + "params": { + "controller": "Attachment", + "action": "file" + } + }, { "route": "/:controller/:id", "method": "get", diff --git a/application/Espo/Services/Attachment.php b/application/Espo/Services/Attachment.php index 1c2831f4bd..ee49dcb9d3 100644 --- a/application/Espo/Services/Attachment.php +++ b/application/Espo/Services/Attachment.php @@ -355,4 +355,19 @@ class Attachment extends Record } return null; } + + public function getFileData(string $id) + { + $attachment = $this->getEntity($id); + if (!$attachment) throw new NotFound(); + + $data = (object) [ + 'name' => $attachment->get('name'), + 'type' => $attachment->get('type'), + 'contents' => $this->getRepository()->getContents($attachment), + 'size' => $attachment->get('size'), + ]; + + return $data; + } }