1 | <?php |
||
42 | abstract class BaseUpload extends Model |
||
43 | { |
||
44 | /** |
||
45 | * Scripts Constants. |
||
46 | * Required for certain validation rules to work for specific scenarios. |
||
47 | */ |
||
48 | const SCENARIO_UPLOAD = 'upload'; |
||
49 | const SCENARIO_UPDATE = 'update'; |
||
50 | |||
51 | /** |
||
52 | * Alt text for the file. |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | public $alt; |
||
57 | |||
58 | /** |
||
59 | * Title for the file. |
||
60 | * |
||
61 | * @var string |
||
62 | */ |
||
63 | public $title; |
||
64 | |||
65 | /** |
||
66 | * File description. |
||
67 | * |
||
68 | * @var string |
||
69 | */ |
||
70 | public $description; |
||
71 | |||
72 | /** |
||
73 | * Addition sub-directory for uploaded files. |
||
74 | * |
||
75 | * @var string |
||
76 | */ |
||
77 | public $subDir; |
||
78 | |||
79 | /** |
||
80 | * Owner name (post, page, article e.t.c.). |
||
81 | * |
||
82 | * @var string |
||
83 | */ |
||
84 | public $owner; |
||
85 | |||
86 | /** |
||
87 | * Owner id. |
||
88 | * |
||
89 | * @var int |
||
90 | */ |
||
91 | public $ownerId; |
||
92 | |||
93 | /** |
||
94 | * Owner attribute (image, audio, thumbnail e.t.c.). |
||
95 | * |
||
96 | * @var string |
||
97 | */ |
||
98 | public $ownerAttribute; |
||
99 | |||
100 | /** |
||
101 | * Needed file type for validation (thumbnail, image e.t.c.). |
||
102 | * |
||
103 | * @var string |
||
104 | */ |
||
105 | public $neededFileType; |
||
106 | |||
107 | /** |
||
108 | * Rename file after upload. |
||
109 | * |
||
110 | * @var bool |
||
111 | */ |
||
112 | public $renameFiles = true; |
||
113 | |||
114 | /** |
||
115 | * File extensions. |
||
116 | * |
||
117 | * @var array |
||
118 | */ |
||
119 | public $fileExtensions = [ |
||
120 | UploadModelInterface::FILE_TYPE_THUMB => [ |
||
121 | 'png', 'jpg', 'jpeg', 'gif', |
||
122 | ], |
||
123 | UploadModelInterface::FILE_TYPE_IMAGE => [ |
||
124 | 'png', 'jpg', 'jpeg', 'gif', |
||
125 | ], |
||
126 | UploadModelInterface::FILE_TYPE_AUDIO => [ |
||
127 | 'mp3', |
||
128 | ], |
||
129 | UploadModelInterface::FILE_TYPE_VIDEO => [ |
||
130 | 'mp4', 'ogg', 'ogv', 'oga', 'ogx', 'webm', |
||
131 | ], |
||
132 | UploadModelInterface::FILE_TYPE_APP => [ |
||
133 | 'doc', 'docx', 'rtf', 'pdf', 'rar', 'zip', 'jar', 'mcd', 'xls', |
||
134 | ], |
||
135 | UploadModelInterface::FILE_TYPE_TEXT => [ |
||
136 | 'txt', |
||
137 | ], |
||
138 | UploadModelInterface::FILE_TYPE_OTHER => null, |
||
139 | ]; |
||
140 | |||
141 | /** |
||
142 | * Check extension by MIME type (they are must match). |
||
143 | * |
||
144 | * @var bool |
||
145 | */ |
||
146 | public $checkExtensionByMimeType = true; |
||
147 | |||
148 | /** |
||
149 | * Maximum file size. |
||
150 | * |
||
151 | * @var int |
||
152 | */ |
||
153 | public $fileMaxSize = 1024*1024*64; |
||
154 | |||
155 | /** |
||
156 | * Thumbs config with their types and sizes. |
||
157 | * |
||
158 | * @var array |
||
159 | */ |
||
160 | public $thumbsConfig = []; |
||
161 | |||
162 | /** |
||
163 | * Thumbnails name template. |
||
164 | * Values can be the next: {original}, {width}, {height}, {alias}, {extension} |
||
165 | * |
||
166 | * @var string |
||
167 | */ |
||
168 | public $thumbFilenameTemplate = '{original}-{width}-{height}-{alias}.{extension}'; |
||
169 | |||
170 | /** |
||
171 | * Directories for uploaded files depending on the file type. |
||
172 | * |
||
173 | * @var array |
||
174 | */ |
||
175 | public $uploadDirs; |
||
176 | |||
177 | /** |
||
178 | * Directory for uploaded files. |
||
179 | * |
||
180 | * @var string |
||
181 | */ |
||
182 | protected $uploadDir; |
||
183 | |||
184 | /** |
||
185 | * Full directory path to upload file. |
||
186 | * |
||
187 | * @var string |
||
188 | */ |
||
189 | protected $uploadPath; |
||
190 | |||
191 | /** |
||
192 | * Prepared file name to save in database and storage. |
||
193 | * |
||
194 | * @var string |
||
195 | */ |
||
196 | protected $outFileName; |
||
197 | |||
198 | /** |
||
199 | * File url path for database. |
||
200 | * |
||
201 | * @var string |
||
202 | */ |
||
203 | protected $databaseUrl; |
||
204 | |||
205 | /** |
||
206 | * File object. |
||
207 | * |
||
208 | * @var UploadedFile |
||
209 | */ |
||
210 | private $file; |
||
211 | |||
212 | /** |
||
213 | * Mediafile model to save files data. |
||
214 | * |
||
215 | * @var Mediafile |
||
216 | */ |
||
217 | private $mediafileModel; |
||
218 | |||
219 | /** |
||
220 | * Set params for send file. |
||
221 | * |
||
222 | * @return void |
||
223 | */ |
||
224 | abstract protected function setParamsForSend(): void; |
||
225 | |||
226 | /** |
||
227 | * Set some params for delete. |
||
228 | * |
||
229 | * @return void |
||
230 | */ |
||
231 | abstract protected function setParamsForDelete(): void; |
||
232 | |||
233 | /** |
||
234 | * Send file to local directory or send file to remote storage. |
||
235 | * |
||
236 | * @return bool |
||
237 | */ |
||
238 | abstract protected function sendFile(): bool; |
||
239 | |||
240 | /** |
||
241 | * Delete files from local directory or from remote storage. |
||
242 | * |
||
243 | * @return void |
||
244 | */ |
||
245 | abstract protected function deleteFiles(): void; |
||
246 | |||
247 | /** |
||
248 | * Create thumb. |
||
249 | * |
||
250 | * @param ThumbConfigInterface $thumbConfig |
||
251 | * |
||
252 | * @return string|null |
||
253 | */ |
||
254 | abstract protected function createThumb(ThumbConfigInterface $thumbConfig); |
||
255 | |||
256 | /** |
||
257 | * Get storage type (local, s3, e.t.c...). |
||
258 | * |
||
259 | * @return string |
||
260 | */ |
||
261 | abstract protected function getStorageType(): string; |
||
262 | |||
263 | /** |
||
264 | * Actions after main save. |
||
265 | * |
||
266 | * @return mixed |
||
267 | */ |
||
268 | abstract protected function afterSave(); |
||
269 | |||
270 | /** |
||
271 | * Scenarios. |
||
272 | * |
||
273 | * @return array |
||
274 | */ |
||
275 | public function scenarios(): array |
||
282 | |||
283 | /** |
||
284 | * Attributes. |
||
285 | * |
||
286 | * @return array |
||
287 | */ |
||
288 | public function attributes() |
||
302 | |||
303 | /** |
||
304 | * {@inheritdoc} |
||
305 | */ |
||
306 | public function rules() |
||
390 | |||
391 | /** |
||
392 | * Set mediafile model. |
||
393 | * |
||
394 | * @param Mediafile $model |
||
395 | */ |
||
396 | public function setMediafileModel(Mediafile $model): void |
||
400 | |||
401 | /** |
||
402 | * Get mediafile model. |
||
403 | * |
||
404 | * @return Mediafile |
||
405 | */ |
||
406 | public function getMediafileModel(): Mediafile |
||
410 | |||
411 | /** |
||
412 | * Set file. |
||
413 | * |
||
414 | * @param UploadedFile|null $file |
||
415 | * |
||
416 | * @return void |
||
417 | */ |
||
418 | public function setFile(UploadedFile $file = null): void |
||
422 | |||
423 | /** |
||
424 | * Set file. |
||
425 | * |
||
426 | * @return mixed |
||
427 | */ |
||
428 | public function getFile() |
||
432 | |||
433 | /** |
||
434 | * Save file in the storage and database by using a "mediafileModel". |
||
435 | * |
||
436 | * @throws \Exception |
||
437 | * |
||
438 | * @return bool |
||
439 | */ |
||
440 | public function save(): bool |
||
484 | |||
485 | /** |
||
486 | * Delete files from local directory or from remote storage. |
||
487 | * |
||
488 | * @throws \Exception |
||
489 | * |
||
490 | * @return int |
||
491 | */ |
||
492 | public function delete(): int |
||
506 | |||
507 | /** |
||
508 | * Returns current model id. |
||
509 | * |
||
510 | * @return int|string |
||
511 | */ |
||
512 | public function getId() |
||
516 | |||
517 | /** |
||
518 | * Create thumbs for this image |
||
519 | * |
||
520 | * @throws InvalidConfigException |
||
521 | * |
||
522 | * @return bool |
||
523 | */ |
||
524 | public function createThumbs(): bool |
||
555 | |||
556 | /** |
||
557 | * Returns thumbnail name. |
||
558 | * |
||
559 | * @param $original |
||
560 | * @param $extension |
||
561 | * @param $alias |
||
562 | * @param $width |
||
563 | * @param $height |
||
564 | * |
||
565 | * @return string |
||
566 | */ |
||
567 | public function getThumbFilename($original, $extension, $alias, $width, $height) |
||
577 | |||
578 | /** |
||
579 | * Get upload directory configuration by file type. |
||
580 | * |
||
581 | * @param string $fileType |
||
582 | * |
||
583 | * @throws InvalidConfigException |
||
584 | * |
||
585 | * @return string |
||
586 | */ |
||
587 | protected function getUploadDirConfig(string $fileType): string |
||
612 | } |
||
613 |