mirror of
https://github.com/espocrm/espocrm.git
synced 2026-06-28 06:56:05 +00:00
attachment api: get file
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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');
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -245,6 +245,14 @@
|
||||
"action": "update"
|
||||
}
|
||||
},
|
||||
{
|
||||
"route": "/Attachment/file/:id",
|
||||
"method": "get",
|
||||
"params": {
|
||||
"controller": "Attachment",
|
||||
"action": "file"
|
||||
}
|
||||
},
|
||||
{
|
||||
"route": "/:controller/:id",
|
||||
"method": "get",
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user