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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
namespace App\Entity\EntityTrait;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Validator\Constraints as Assert;
trait OldFileEntityTrait
{
/**
* @var null|File|UploadedFile
*/
protected $file;
/**
* @var null|string
*/
protected $oldPath = null;
/**
* @var null|int
*
* @ORM\Column(type="integer", length=255, nullable=true)
*/
protected $size;
/**
* @var null|string
*
* @ORM\Column(type="string", length=10, nullable=true)
*/
protected $extension;
/**
* @var null|string
*
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $type;
/**
* @var null|string
*
* @ORM\Column(name="file", type="string", length=255, nullable=true)
*/
protected $path = null;
/**
* @param File|null $file
* @return $this
*/
public function setFile(File $file = null)
{
$this->file = $file;
return $this;
}
/**
* @return null|File|UploadedFile
*/
public function getFile(): ?File
{
return $this->file;
}
/**
* @return null|string
*/
public function getOldPath(): ?string
{
return $this->oldPath;
}
/**
* @param null|string $oldPath
* @return $this
*/
public function setOldPath(?string $oldPath)
{
$this->oldPath = $oldPath;
return $this;
}
/**
* @return int|null
*/
public function getSize(): ?int
{
return $this->size;
}
/**
* @param int|null $size
* @return $this
*/
public function setSize(?int $size)
{
$this->size = $size;
return $this;
}
/**
* @return null|string
*/
public function getExtension(): ?string
{
return $this->extension;
}
/**
* @param null|string $extension
* @return $this
*/
public function setExtension(?string $extension)
{
$this->extension = $extension;
return $this;
}
/**
* @return null|string
*/
public function getType(): ?string
{
return $this->type;
}
/**
* @param null|string $type
* @return $this
*/
public function setType(?string $type)
{
$this->type = $type;
return $this;
}
/**
* @return null|string
*/
public function getPath(): ?string
{
return $this->path;
}
/**
* @param null|string $path
* @return $this
*/
public function setPath(?string $path)
{
$this->path = $path;
return $this;
}
public function getAbsolutePath()
{
return null === $this->getPath()
? null
: $this->getUploadRootDir().'/'.$this->getPath();
}
public function getWebPath()
{
return null === $this->getPath()
? null
: $this->getUploadDir().'/'.$this->getPath();
}
public function getUploadRootDir()
{
return __DIR__.'/../../../../web/'.$this->getUploadDir();
}
public function getUploadDir()
{
return 'uploads/'.strtolower($this->getType());
}
/**
* Au chargement de l'entité génération d'un objet File stocker dans la proprieté file
* @ORM\PostLoad()
*/
public function postLoadFile()
{
if($this->getPath() != null) {
try {
$this->setFile(new File($this->getAbsolutePath()));
} catch (\Exception $e){
}
}
}
/**
* A la modification ou création de l'entité je vérifie qu'il y a un UploadedFile dans la proprieté file
* @ORM\PreFlush()
*/
public function upload()
{
if (null === $this->getFile() || !$this->getFile() instanceof UploadedFile) {
return;
}
$file_name = date('m-d-Y_his') . '-' . $this->getFile()->getClientOriginalName();
if($this->getPath() != null) {
$this->setOldPath($this->getPath());
}
$this->setPath($file_name);
$this->setSize($this->getFile()->getSize());
$this->setExtension($this->getFile()->getClientOriginalExtension());
$fs = new Filesystem();
if (!$fs->exists($this->getUploadRootDir())) {
$fs->mkdir($this->getUploadRootDir(), 0775);
}
$this->file->move(
$this->getUploadRootDir(), $this->getPath()
);
}
/**
* Après la création ou la modification de l'entité, si le fichier à été remplacé je supprime l'ancien
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function cleanOldFile()
{
if($this->getOldPath() !== null) {
$fs = new Filesystem();
try {
$fs->remove($this->getUploadRootDir().'/'. $this->getOldPath());
} catch(\Exception $exception) {
}
}
}
}