1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SumoCoders\FrameworkCoreBundle\ValueObject; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Mapping as ORM; |
6
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* The following things are mandatory to use this class. |
10
|
|
|
* |
11
|
|
|
* You need to implement the method getUploadDir. |
12
|
|
|
* When using this class in an entity certain life cycle callbacks should be called |
13
|
|
|
* prepareToUpload for @ORM\PrePersist() and @ORM\PreUpdate() |
14
|
|
|
* upload for @ORM\PostPersist() and @ORM\PostUpdate() |
15
|
|
|
* remove for @ORM\PostRemove() |
16
|
|
|
*/ |
17
|
|
|
abstract class AbstractFile |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
* @ORM\Column(type="string", length=255, nullable=true) |
22
|
|
|
*/ |
23
|
|
|
protected $fileName = null; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var UploadedFile |
27
|
|
|
*/ |
28
|
|
|
protected $file; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $oldFileName; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $fileName |
37
|
|
|
*/ |
38
|
|
|
protected function __construct($fileName) |
39
|
|
|
{ |
40
|
|
|
$this->fileName = $fileName; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
public function getFileName() |
47
|
|
|
{ |
48
|
|
|
return $this->fileName; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return string|null |
53
|
|
|
*/ |
54
|
|
|
public function getAbsolutePath() |
55
|
|
|
{ |
56
|
|
|
return $this->fileName === null ? null : $this->getUploadRootDir() . '/' . $this->fileName; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function getWebPath() |
63
|
|
|
{ |
64
|
|
|
$file = $this->getAbsolutePath(); |
65
|
|
|
if (is_file($file) && file_exists($file)) { |
66
|
|
|
return '/' . $this->getUploadDir() . '/' . $this->fileName; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return ''; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
protected function getUploadRootDir() |
76
|
|
|
{ |
77
|
|
|
// the absolute directory path where uploaded documents should be saved |
78
|
|
|
return __DIR__ . '/../../../../web/' . $this->getTrimmedUploadDir(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
protected function getTrimmedUploadDir() |
85
|
|
|
{ |
86
|
|
|
return trim($this->getUploadDir(), '/\\'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* The dir in the web folder where the file needs to be uploaded. |
91
|
|
|
* The base directory is always the src/Frontend/Files/ directory |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
abstract protected function getUploadDir(); |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Sets file. |
99
|
|
|
* |
100
|
|
|
* @param UploadedFile $file |
101
|
|
|
* |
102
|
|
|
* @return static |
103
|
|
|
*/ |
104
|
|
|
public function setFile(UploadedFile $file = null) |
105
|
|
|
{ |
106
|
|
|
$this->file = $file; |
107
|
|
|
// check if we have an old image path |
108
|
|
|
if ($this->fileName === null) { |
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// store the old name to delete after the update |
113
|
|
|
$this->oldFileName = $this->fileName; |
114
|
|
|
$this->fileName = null; |
115
|
|
|
|
116
|
|
|
return clone $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param UploadedFile|null $uploadedFile |
121
|
|
|
* |
122
|
|
|
* @return static |
123
|
|
|
*/ |
124
|
|
|
public static function fromUploadedFile(UploadedFile $uploadedFile = null) |
125
|
|
|
{ |
126
|
|
|
$file = new static(null); |
127
|
|
|
$file->setFile($uploadedFile); |
128
|
|
|
|
129
|
|
|
return $file; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get file. |
134
|
|
|
* |
135
|
|
|
* @return UploadedFile |
136
|
|
|
*/ |
137
|
|
|
public function getFile() |
138
|
|
|
{ |
139
|
|
|
return $this->file; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* This function should be called for the life cycle events @ORM\PrePersist() and @ORM\PreUpdate() |
144
|
|
|
*/ |
145
|
|
|
public function prepareToUpload() |
146
|
|
|
{ |
147
|
|
|
if ($this->getFile() === null) { |
148
|
|
|
return; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
// do whatever you want to generate a unique name |
152
|
|
|
$filename = sha1(uniqid(mt_rand(), true)); |
153
|
|
|
$this->fileName = $filename . '.' . $this->getFile()->guessExtension(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* This function should be called for the life cycle events @ORM\PostPersist() and @ORM\PostUpdate() |
158
|
|
|
*/ |
159
|
|
|
public function upload() |
160
|
|
|
{ |
161
|
|
|
// check if we have an old image |
162
|
|
|
if ($this->oldFileName !== null) { |
163
|
|
|
// delete the old image |
164
|
|
|
$oldFile = $this->getUploadRootDir() . '/' . $this->oldFileName; |
165
|
|
|
if (is_file($oldFile) && file_exists($oldFile)) { |
166
|
|
|
unlink($oldFile); |
167
|
|
|
} |
168
|
|
|
// clear the $this->oldFileName image path |
169
|
|
|
$this->oldFileName = null; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if ($this->getFile() === null) { |
173
|
|
|
return; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$this->writeFileToDisk(); |
177
|
|
|
|
178
|
|
|
$this->file = null; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* if there is an error when moving the file, an exception will |
183
|
|
|
* be automatically thrown by move(). This will properly prevent |
184
|
|
|
* the entity from being persisted to the database on error |
185
|
|
|
*/ |
186
|
|
|
protected function writeFileToDisk() |
187
|
|
|
{ |
188
|
|
|
$this->getFile()->move($this->getUploadRootDir(), $this->fileName); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* This function should be called for the life cycle event @ORM\PostRemove() |
193
|
|
|
*/ |
194
|
|
|
public function remove() |
195
|
|
|
{ |
196
|
|
|
$file = $this->getAbsolutePath(); |
197
|
|
|
if (!is_file($file) || !file_exists($file)) { |
198
|
|
|
return; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
unlink($file); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Returns a string representation of the image. |
206
|
|
|
* |
207
|
|
|
* @return string |
208
|
|
|
*/ |
209
|
|
|
public function __toString() |
210
|
|
|
{ |
211
|
|
|
return (string) $this->fileName; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param string $fileName |
216
|
|
|
* |
217
|
|
|
* @return static |
218
|
|
|
*/ |
219
|
|
|
public static function fromString($fileName) |
220
|
|
|
{ |
221
|
|
|
return new static($fileName); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* {@inheritdoc} |
226
|
|
|
*/ |
227
|
|
|
public function jsonSerialize() |
228
|
|
|
{ |
229
|
|
|
return (string) $this->getWebPath(); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function markForDeletion() |
233
|
|
|
{ |
234
|
|
|
$this->oldFileName = $this->fileName; |
235
|
|
|
$this->fileName = null; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|