1 | <?php |
||
22 | abstract class AbstractFile |
||
23 | { |
||
24 | /** |
||
25 | * @var string |
||
26 | * Column(type="string", length=255, nullable=true) |
||
27 | */ |
||
28 | protected $fileName = null; |
||
29 | |||
30 | /** |
||
31 | * @var UploadedFile |
||
32 | */ |
||
33 | protected $file; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $oldFileName; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $namePrefix; |
||
44 | |||
45 | protected function __construct(?string $fileName) |
||
46 | { |
||
47 | $this->fileName = $fileName; |
||
48 | } |
||
49 | |||
50 | public function getFileName(): ?string |
||
51 | { |
||
52 | return $this->fileName; |
||
53 | } |
||
54 | |||
55 | public function getAbsolutePath(): ?string |
||
56 | { |
||
57 | return $this->fileName === null ? null : $this->getUploadRootDir() . '/' . $this->fileName; |
||
58 | } |
||
59 | |||
60 | public function getWebPath(): string |
||
61 | { |
||
62 | $file = $this->getAbsolutePath(); |
||
63 | if (is_file($file) && file_exists($file)) { |
||
64 | return '/files/' . $this->getUploadDir() . '/' . $this->fileName; |
||
65 | } |
||
66 | |||
67 | return ''; |
||
68 | } |
||
69 | |||
70 | protected function getUploadRootDir(): string |
||
71 | { |
||
72 | // the absolute directory path where uploaded documents should be saved |
||
73 | return __DIR__ . '/../../../../web/files/' . $this->getTrimmedUploadDir(); |
||
74 | } |
||
75 | |||
76 | protected function getTrimmedUploadDir(): string |
||
77 | { |
||
78 | return trim($this->getUploadDir(), '/\\'); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * The dir in the web folder where the file needs to be uploaded. |
||
83 | * The base directory is always the web/files directory |
||
84 | * |
||
85 | * @return string |
||
86 | */ |
||
87 | abstract protected function getUploadDir(): string; |
||
88 | |||
89 | public function setFile(UploadedFile $file = null): self |
||
90 | { |
||
91 | if ($file === null) { |
||
92 | return $this; |
||
93 | } |
||
94 | |||
107 | |||
108 | /** |
||
109 | * @param UploadedFile|null $uploadedFile |
||
110 | * @param string|null $namePrefix If set this will be prepended to the generated filename |
||
111 | * |
||
112 | * @return static |
||
113 | */ |
||
114 | public static function fromUploadedFile(UploadedFile $uploadedFile = null, string $namePrefix = null) |
||
124 | |||
125 | public function getFile(): ?UploadedFile |
||
129 | |||
130 | final public function hasFile(): bool |
||
134 | |||
135 | /** |
||
136 | * This function should be called for the life cycle events PrePersist() and PreUpdate() |
||
137 | */ |
||
138 | public function prepareToUpload(): void |
||
151 | |||
152 | /** |
||
153 | * This function should be called for the life cycle events PostPersist() and PostUpdate() |
||
154 | */ |
||
155 | public function upload(): void |
||
170 | |||
171 | |||
172 | |||
173 | /** |
||
174 | * This will remove the old file, can be extended to add extra functionality |
||
175 | */ |
||
176 | protected function removeOldFile(): void |
||
186 | |||
187 | /** |
||
188 | * if there is an error when moving the file, an exception will |
||
189 | * be automatically thrown by move(). This will properly prevent |
||
190 | * the entity from being persisted to the database on error |
||
191 | */ |
||
192 | protected function writeFileToDisk(): void |
||
196 | |||
197 | /** |
||
198 | * This function should be called for the life cycle event PostRemove() |
||
199 | */ |
||
200 | public function remove(): void |
||
209 | |||
210 | public function __toString(): string |
||
214 | |||
215 | public static function fromString(?string $fileName): ?self |
||
219 | |||
220 | /** |
||
221 | * The next time doctrine saves this to the database the file will be removed |
||
222 | */ |
||
223 | public function markForDeletion(): void |
||
228 | |||
229 | /** |
||
230 | * @param string $namePrefix If set this will be prepended to the generated filename |
||
231 | * |
||
232 | * @return self |
||
233 | */ |
||
234 | public function setNamePrefix(string $namePrefix): self |
||
240 | |||
241 | /** |
||
242 | * @internal Used by the form types |
||
243 | * |
||
244 | * @param bool $isPendingDeletion |
||
245 | */ |
||
246 | public function setPendingDeletion($isPendingDeletion): void |
||
252 | |||
253 | /** |
||
254 | * @internal Used by the form types |
||
255 | * |
||
256 | * @return bool |
||
257 | */ |
||
258 | public function isPendingDeletion(): bool |
||
262 | |||
263 | public function jsonSerialize(): string |
||
267 | } |
||
268 |