-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileFromDbService.php
49 lines (42 loc) · 1.22 KB
/
FileFromDbService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
declare(strict_types=1);
namespace App\Services;
use App\Interfaces\FileStoredInDb;
use Illuminate\Http\Response;
/**
* Manipulate file stored in DB
*/
class FileFromDbService
{
public function __construct(public FileStoredInDb $model)
{
}
/**
* Return a response with the file
*/
public function servePdfFile(): Response
{
return $this->serveFile('application/pdf');
}
public function serveFile(string $mimeType = null): Response
{
$data = $this->model->getFileData();
if (empty($data)) {
abort(404);
}
return response(
content: $data,
status: 200,
headers: [
'Content-type' => $mimeType ?? $this->model->getMime(),
'Pragma' => 'no-cache',
'Cache-control' => 'public, must-revalidate, max-age=0',
'Pragme' => 'public',
'Expires' => 'Sat, 26 Jul 1997 05:00:00 GMT',
'Last-Modified' => ''.gmdate('D, d m Y H:i:s').' GMT',
'Content-Disposition' => "inline; filename=\"{$this->model->getFileName()}\";",
'Content-length' => strlen($data),
]
);
}
}