attachment api: get file

This commit is contained in:
Yuri Kuznetsov
2020-04-14 19:35:21 +03:00
parent e6b1299293
commit 8a17ff2ca3
4 changed files with 49 additions and 6 deletions

View File

@@ -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;
}
}

View File

@@ -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');
});
}

View File

@@ -245,6 +245,14 @@
"action": "update"
}
},
{
"route": "/Attachment/file/:id",
"method": "get",
"params": {
"controller": "Attachment",
"action": "file"
}
},
{
"route": "/:controller/:id",
"method": "get",

View File

@@ -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;
}
}