|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Application\Model; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\ORM\Mapping as ORM; |
|
8
|
|
|
use GraphQL\Doctrine\Annotation as API; |
|
9
|
|
|
use Psr\Http\Message\UploadedFileInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Wrapping class for an uploaded file |
|
13
|
|
|
* |
|
14
|
|
|
* @ORM\MappedSuperclass |
|
15
|
|
|
* @ORM\Table(uniqueConstraints={ |
|
16
|
|
|
* @ORM\UniqueConstraint(name="unique_name", columns={"filename"}) |
|
17
|
|
|
* }) |
|
18
|
|
|
*/ |
|
19
|
|
|
abstract class AbstractFile extends AbstractModel |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Get base path where the files are stored in the server |
|
23
|
|
|
* |
|
24
|
|
|
* @return string |
|
25
|
|
|
*/ |
|
26
|
|
|
abstract protected function getBasePath(): string; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Get list of accepted MIME types |
|
30
|
|
|
* |
|
31
|
|
|
* @return string[] |
|
32
|
|
|
*/ |
|
33
|
|
|
abstract protected function getAcceptedMimeTypes(): array; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
* @ORM\Column(type="string", length=190) |
|
38
|
|
|
*/ |
|
39
|
|
|
private $filename = ''; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var string |
|
43
|
|
|
* @ORM\Column(type="string", length=255) |
|
44
|
|
|
*/ |
|
45
|
|
|
private $mime = ''; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Set the file |
|
49
|
|
|
* |
|
50
|
|
|
* @param UploadedFileInterface $file |
|
51
|
|
|
* |
|
52
|
|
|
* @throws \Exception |
|
53
|
|
|
*/ |
|
54
|
2 |
|
public function setFile(UploadedFileInterface $file): void |
|
55
|
|
|
{ |
|
56
|
2 |
|
$this->generateUniqueFilename($file->getClientFilename()); |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
2 |
|
$path = $this->getPath(); |
|
59
|
2 |
|
if (file_exists($path)) { |
|
60
|
|
|
throw new \Exception('A file already exist with the same name: ' . $this->getFilename()); |
|
61
|
|
|
} |
|
62
|
2 |
|
$file->moveTo($path); |
|
63
|
|
|
|
|
64
|
2 |
|
$this->validateMimeType(); |
|
65
|
2 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Set filename (without path) |
|
69
|
|
|
* |
|
70
|
|
|
* @API\Exclude |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $filename |
|
73
|
|
|
*/ |
|
74
|
5 |
|
public function setFilename(string $filename): void |
|
75
|
|
|
{ |
|
76
|
5 |
|
$this->filename = $filename; |
|
77
|
5 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Get filename (without path) |
|
81
|
|
|
* |
|
82
|
|
|
* @API\Exclude |
|
83
|
|
|
* |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
5 |
|
public function getFilename(): string |
|
87
|
|
|
{ |
|
88
|
5 |
|
return $this->filename; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get mime |
|
93
|
|
|
* |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getMime(): string |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->mime; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Get absolute path to file on disk |
|
103
|
|
|
* |
|
104
|
|
|
* @API\Exclude |
|
105
|
|
|
* |
|
106
|
|
|
* @return string |
|
107
|
|
|
*/ |
|
108
|
5 |
|
public function getPath(): string |
|
109
|
|
|
{ |
|
110
|
5 |
|
return realpath('.') . '/' . $this->getBasePath() . $this->getFilename(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Automatically called by Doctrine when the object is deleted |
|
115
|
|
|
* Is called after database update because we can have issues on remove operation (like integrity test) |
|
116
|
|
|
* and it's preferable to keep a related file on drive before removing it definitely. |
|
117
|
|
|
* |
|
118
|
|
|
* @ORM\PostRemove |
|
119
|
|
|
*/ |
|
120
|
1 |
|
public function deleteFile(): void |
|
121
|
|
|
{ |
|
122
|
1 |
|
$path = $this->getPath(); |
|
123
|
1 |
|
if (file_exists($path) && is_file($path) && mb_strpos($this->getFilename(), 'dw4jV3zYSPsqE2CB8BcP8ABD0.') === false) { |
|
124
|
1 |
|
unlink($path); |
|
125
|
|
|
} |
|
126
|
1 |
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Generate unique filename while trying to preserve original extension |
|
130
|
|
|
* |
|
131
|
|
|
* @param string $originalFilename |
|
132
|
|
|
*/ |
|
133
|
2 |
|
private function generateUniqueFilename(string $originalFilename): void |
|
134
|
|
|
{ |
|
135
|
2 |
|
$extension = pathinfo($originalFilename, PATHINFO_EXTENSION); |
|
136
|
2 |
|
$filename = uniqid() . ($extension ? '.' . $extension : ''); |
|
137
|
2 |
|
$this->setFilename($filename); |
|
138
|
2 |
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Delete file and throw exception if MIME type is invalid |
|
142
|
|
|
* |
|
143
|
|
|
* @throws \Exception |
|
144
|
|
|
*/ |
|
145
|
2 |
|
private function validateMimeType(): void |
|
146
|
|
|
{ |
|
147
|
2 |
|
$path = $this->getPath(); |
|
148
|
2 |
|
$mime = mime_content_type($path); |
|
149
|
|
|
|
|
150
|
|
|
// Validate mimetype |
|
151
|
2 |
|
if (!in_array($mime, $this->getAcceptedMimeTypes(), true)) { |
|
152
|
|
|
unlink($path); |
|
153
|
|
|
|
|
154
|
|
|
throw new \Exception('Invalid file type of: ' . $mime); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
2 |
|
$this->mime = $mime; |
|
158
|
2 |
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|