|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace WebServCo\Framework\Files; |
|
6
|
|
|
|
|
7
|
|
|
use WebServCo\Framework\Http\Response; |
|
8
|
|
|
|
|
9
|
|
|
abstract class AbstractFile |
|
10
|
|
|
{ |
|
11
|
|
|
public const CONTENT_TYPE = 'application/octet-stream'; |
|
12
|
|
|
|
|
13
|
|
|
protected string $fileName; |
|
14
|
|
|
protected string $fileData; |
|
15
|
|
|
protected string $contentType; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(string $fileName, string $fileData, string $contentType = self::CONTENT_TYPE) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->fileName = $fileName; |
|
20
|
|
|
$this->fileData = $fileData; |
|
21
|
|
|
$this->contentType = $contentType; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function getContentType(): string |
|
25
|
|
|
{ |
|
26
|
|
|
return $this->contentType; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function getDownloadResponse(): Response |
|
30
|
|
|
{ |
|
31
|
|
|
return new Response( |
|
32
|
|
|
$this->fileData, |
|
33
|
|
|
200, |
|
34
|
|
|
[ |
|
35
|
|
|
'Last-Modified' => [\gmdate('D, d M Y H:i:s') . ' GMT'], |
|
36
|
|
|
'ETag' => [\md5($this->fileData)], |
|
37
|
|
|
'Accept-Ranges' => ['bytes'], |
|
38
|
|
|
'Cache-Control' => ['public'], |
|
39
|
|
|
'Content-Description' => ['File Transfer'], |
|
40
|
|
|
'Content-Disposition' => [\sprintf('attachment; filename="%s"', $this->fileName)], |
|
41
|
|
|
'Content-Type' => [$this->contentType], |
|
42
|
|
|
'Content-Transfer-Encoding' => ['binary'], |
|
43
|
|
|
'Connection' => ['close'], |
|
44
|
|
|
], |
|
45
|
|
|
); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function getFileData(): string |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->fileData; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function getFileName(): string |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->fileName; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function getOutputResponse(): Response |
|
59
|
|
|
{ |
|
60
|
|
|
return new Response( |
|
61
|
|
|
$this->fileData, |
|
62
|
|
|
200, |
|
63
|
|
|
[ |
|
64
|
|
|
'Last-Modified' => [\gmdate('D, d M Y H:i:s') . ' GMT'], |
|
65
|
|
|
'ETag' => [\md5($this->fileData)], |
|
66
|
|
|
'Accept-Ranges' => ['bytes'], |
|
67
|
|
|
'Cache-Control' => ['public'], |
|
68
|
|
|
'Content-Type' => [$this->contentType], |
|
69
|
|
|
'Content-Transfer-Encoding' => ['binary'], |
|
70
|
|
|
], |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function setFileName(string $fileName): bool |
|
75
|
|
|
{ |
|
76
|
|
|
$this->fileName = $fileName; |
|
77
|
|
|
return true; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|