1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Entity\S3; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Mapping as ORM; |
6
|
|
|
use Knp\DoctrineBehaviors\Model\Timestampable\Timestampable; |
7
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Media. |
11
|
|
|
* |
12
|
|
|
* @ORM\Table() |
13
|
|
|
* @ORM\Entity() |
14
|
|
|
*/ |
15
|
|
|
class Image implements \JsonSerializable, S3ObjectInterface |
16
|
|
|
{ |
17
|
|
|
const ALLOWED_CONTENT_TYPES = ['image/jpeg', 'image/png']; |
18
|
|
|
|
19
|
|
|
use Timestampable; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var int |
23
|
|
|
* |
24
|
|
|
* @ORM\Id |
25
|
|
|
* @ORM\GeneratedValue |
26
|
|
|
* @ORM\Column(type="integer") |
27
|
|
|
*/ |
28
|
|
|
private $id; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var resource|string |
32
|
|
|
* |
33
|
|
|
* @Assert\NotBlank(groups={"Api"}) |
34
|
|
|
*/ |
35
|
|
|
private $content; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
* |
40
|
|
|
* @ORM\Column(type="string", length=190, nullable=true) |
41
|
|
|
* @Assert\Choice(callback="getAllowedTypes", groups={"Api"}) |
42
|
|
|
* @Assert\NotNull(groups={"Api"}) |
43
|
|
|
*/ |
44
|
|
|
private $contentType; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string |
48
|
|
|
* |
49
|
|
|
* @ORM\Column(type="text", nullable=true) |
50
|
|
|
*/ |
51
|
|
|
private $s3Key; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
private $oldS3Key; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var string |
60
|
|
|
* |
61
|
|
|
* @ORM\Column(type="string", length=191, nullable=true) |
62
|
|
|
*/ |
63
|
|
|
private $nameFile; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var string |
67
|
|
|
* |
68
|
|
|
* @ORM\Column(type="text", nullable=true) |
69
|
|
|
*/ |
70
|
|
|
private $url; |
71
|
|
|
|
72
|
|
|
public function __construct($prefix = null) |
73
|
|
|
{ |
74
|
|
|
$name = bin2hex(random_bytes(16)); |
75
|
|
|
$this->setS3Key($prefix ? sprintf('%s/%s', $prefix, $name) : $name); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return int |
80
|
|
|
*/ |
81
|
|
|
public function getId() |
82
|
|
|
{ |
83
|
|
|
return $this->id; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return resource|string |
88
|
|
|
*/ |
89
|
|
|
public function getContent() |
90
|
|
|
{ |
91
|
|
|
return $this->content; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param resource|string $content |
96
|
|
|
* |
97
|
|
|
* @return $this |
98
|
|
|
*/ |
99
|
|
|
public function setContent($content) |
100
|
|
|
{ |
101
|
|
|
$this->content = $content; |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
public function getS3Key() |
110
|
|
|
{ |
111
|
|
|
return $this->s3Key; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $s3Key |
116
|
|
|
* |
117
|
|
|
* @return $this |
118
|
|
|
*/ |
119
|
|
|
public function setS3Key($s3Key) |
120
|
|
|
{ |
121
|
|
|
if ($this->getS3Key()) { |
122
|
|
|
$this->setOldS3Key($this->getS3Key()); |
123
|
|
|
} |
124
|
|
|
$this->s3Key = $s3Key; |
125
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
public function getOldS3Key() |
133
|
|
|
{ |
134
|
|
|
return $this->oldS3Key; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $oldS3Key |
139
|
|
|
* |
140
|
|
|
* @return $this |
141
|
|
|
*/ |
142
|
|
|
public function setOldS3Key($oldS3Key) |
143
|
|
|
{ |
144
|
|
|
$this->oldS3Key = $oldS3Key; |
145
|
|
|
|
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
public function getNameFile() |
153
|
|
|
{ |
154
|
|
|
return $this->nameFile; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param string $nameFile |
159
|
|
|
* |
160
|
|
|
* @return $this |
161
|
|
|
*/ |
162
|
|
|
public function setNameFile($nameFile) |
163
|
|
|
{ |
164
|
|
|
$this->nameFile = $nameFile; |
165
|
|
|
|
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
|
|
public function getUrl() |
173
|
|
|
{ |
174
|
|
|
return $this->url; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param string $url |
179
|
|
|
* |
180
|
|
|
* @return $this |
181
|
|
|
*/ |
182
|
|
|
public function setUrl($url) |
183
|
|
|
{ |
184
|
|
|
$this->url = $url; |
185
|
|
|
|
186
|
|
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
|
|
public function getContentType() |
193
|
|
|
{ |
194
|
|
|
return $this->contentType; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param string $contentType |
199
|
|
|
* |
200
|
|
|
* @return $this |
201
|
|
|
*/ |
202
|
|
|
public function setContentType($contentType) |
203
|
|
|
{ |
204
|
|
|
$this->contentType = $contentType; |
205
|
|
|
|
206
|
|
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public static function getAllowedTypes() |
210
|
|
|
{ |
211
|
|
|
return static::ALLOWED_CONTENT_TYPES; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function jsonSerialize() |
215
|
|
|
{ |
216
|
|
|
return $this->getUrl(); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|