This repository has been archived by the owner on Dec 1, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
FileBehavior.php
606 lines (554 loc) · 20.7 KB
/
FileBehavior.php
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
<?php
/**
* @link https://github.com/yii2tech
* @copyright Copyright (c) 2015 Yii2tech
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
*/
namespace yii2tech\ar\file;
use Yii;
use yii\base\Behavior;
use yii\base\UnknownPropertyException;
use yii\db\BaseActiveRecord;
use yii\di\Instance;
use yii\helpers\FileHelper;
use yii\helpers\Inflector;
use yii\helpers\StringHelper;
use yii\web\UploadedFile;
use yii2tech\filestorage\BucketInterface;
use yii2tech\filestorage\StorageInterface;
/**
* Behavior for the [[BaseActiveRecord]], which allows to save a single file per each table record.
* Behavior tracks the file extension and manage file version in order to prevent cache problems.
* Due to this the database table, which the model refers to, must contain fields [[fileExtensionAttribute]] and [[fileVersionAttribute]].
* On the model save behavior will automatically search for the attached file in $_FILES.
* However you can manipulate attached file using property [[uploadedFile]].
* For the tabular file input use [[fileTabularInputIndex]] property.
*
* Note: you can always use [[saveFile()]] method to attach any file (not just uploaded one) to the model.
*
* Attention: this extension requires the extension "yii2tech/file-storage" to be attached to the application!
* Files will be saved using file storage component.
*
* @see StorageInterface
* @see BucketInterface
*
* @property UploadedFile|string|null $uploadedFile related uploaded file
* @property BaseActiveRecord $owner owner ActiveRecord instance.
*
* @author Paul Klimov <[email protected]>
* @since 1.0
*/
class FileBehavior extends Behavior
{
/**
* @var string name of virtual model's attribute, which will be used
* to fetch file uploaded from the web form.
* Use value of this attribute to create web form file input field.
*/
public $fileAttribute = 'file';
/**
* @var string name of the file storage application component.
*/
public $fileStorage = 'fileStorage';
/**
* @var string|BucketInterface name of the file storage bucket, which stores the related files or
* bucket instance itself.
* If empty, bucket name will be generated automatically using owner class name and [[fileAttribute]].
*/
public $fileStorageBucket;
/**
* @var string|callable template of all sub directories, which will store a particular
* model instance's files. Value of this parameter will be parsed per each model instance.
* You can use model attribute names to create sub directories, for example place all transformed
* files in the subfolder with the name of model id. To use a dynamic value of attribute
* place attribute name in curly brackets, for example: {id}.
*
* Since 1.0.3 template can be set as a callback returning actual string template:
*
* ```php
* function (BaseActiveRecord $model) {
* // return string, the actual path or mix it with placeholders
* }
* ```
*
* You may also specify special placeholders:
*
* - {pk} - resolved as primary key value of the owner model,
* - {__model__} - resolved as class name of the owner model, replacing namespace separator (`\`) with underscore (`_`),
* - {__file__} - resolved as value of [[fileAttribute]].
*
* You may place symbols "^" before any placeholder name, such placeholder will be resolved as single
* symbol of the normal value. Number of symbol determined by count of "^".
* For example: if model id equal to 54321, placeholder {^id} will be resolved as "5", {^^id} - as "4" and so on.
* Example value:
* '{__model__}/{__file__}/{groupId}/{^pk}/{pk}'
*/
public $subDirTemplate = '{^^pk}/{^pk}';
/**
* @var string name of model's attribute, which will be used to store file extension.
* Corresponding model's attribute should be a string type.
*/
public $fileExtensionAttribute = 'fileExtension';
/**
* @var string name of model's attribute, which will be used to store file version number.
* Corresponding model's attribute should be a string or integer type.
*/
public $fileVersionAttribute = 'fileVersion';
/**
* @var int index of the HTML input file field in case of tabular input (input name has format "ModelName[$i][file]").
* Note: after owner is saved this property will be reset.
*/
public $fileTabularInputIndex;
/**
* @var string URL which is used to set up web links, which will be returned, if requested file does not exists.
* For example: 'http://www.myproject.com/materials/default/image.jpg'
*/
public $defaultFileUrl;
/**
* @var bool indicates if behavior will attempt to fetch uploaded file automatically from the HTTP request.
*/
public $autoFetchUploadedFile = true;
/**
* @var UploadedFile instance of [[UploadedFile]], allows to save file,
* passed through the web form.
*/
private $_uploadedFile;
// Set / Get:
/**
* @param UploadedFile|string|null $uploadedFile related uploaded file
*/
public function setUploadedFile($uploadedFile)
{
$this->_uploadedFile = $uploadedFile;
}
/**
* @return UploadedFile|null related uploaded file
*/
public function getUploadedFile()
{
if (!is_object($this->_uploadedFile)) {
$this->_uploadedFile = $this->ensureUploadedFile($this->_uploadedFile);
}
return $this->_uploadedFile;
}
/**
* Returns the file storage bucket for the files by name given with [[fileStorageBucket]].
* If no bucket exists attempts to create it.
* @return BucketInterface file storage bucket instance.
*/
public function ensureFileStorageBucket()
{
if (!is_object($this->fileStorageBucket)) {
/* @var StorageInterface $fileStorage */
$fileStorage = Instance::ensure($this->fileStorage, 'yii2tech\filestorage\StorageInterface');
if ($this->fileStorageBucket === null) {
$bucketName = $this->defaultFileStorageBucketName();
} else {
$bucketName = $this->fileStorageBucket;
}
if (!$fileStorage->hasBucket($bucketName)) {
$fileStorage->addBucket($bucketName);
}
$this->fileStorageBucket = $fileStorage->getBucket($bucketName);
}
return $this->fileStorageBucket;
}
/**
* Composes default [[fileStorageBucket]] name, using owner class name and [[fileAttribute]].
* @return string bucket name.
*/
protected function defaultFileStorageBucketName()
{
return Inflector::camel2id(StringHelper::basename(get_class($this->owner)), '-');
}
// SubDir Template:
/**
* Gets file storage sub dirs path, resolving [[subDirTemplate]].
* @return string actual sub directory string.
*/
public function getActualSubDir()
{
if (!is_scalar($this->subDirTemplate) && is_callable($this->subDirTemplate)) {
$subDirTemplate = call_user_func($this->subDirTemplate, $this->owner);
} else {
$subDirTemplate = $this->subDirTemplate;
}
if (empty($subDirTemplate)) {
return $subDirTemplate;
}
$result = preg_replace_callback('/{(\^*(\w+))}/', [$this, 'getSubDirPlaceholderValue'], $subDirTemplate);
return $result;
}
/**
* Internal callback function for [[getActualSubDir()]].
* @param array $matches - set of regular expression matches.
* @return string replacement for the match.
*/
protected function getSubDirPlaceholderValue($matches)
{
$placeholderName = $matches[1];
$placeholderPartSymbolPosition = strspn($placeholderName, '^') - 1;
if ($placeholderPartSymbolPosition >= 0) {
$placeholderName = $matches[2];
}
switch ($placeholderName) {
case 'pk': {
$placeholderValue = $this->getPrimaryKeyStringValue();
break;
}
case '__model__': {
$placeholderValue = str_replace('\\', '_', get_class($this->owner));
break;
}
case '__file__': {
$placeholderValue = $this->fileAttribute;
break;
}
default: {
try {
$placeholderValue = $this->owner->{$placeholderName};
} catch (UnknownPropertyException $exception) {
$placeholderValue = $placeholderName;
}
}
}
if ($placeholderPartSymbolPosition >= 0) {
if ($placeholderPartSymbolPosition < strlen($placeholderValue)) {
$placeholderValue = substr($placeholderValue, $placeholderPartSymbolPosition, 1);
} else {
$placeholderValue = '0';
}
}
return $placeholderValue;
}
// Service:
/**
* Creates string representation of owner model primary key value,
* handles case when primary key is complex and consist of several fields.
* @return string representation of owner model primary key value.
*/
protected function getPrimaryKeyStringValue()
{
$owner = $this->owner;
$primaryKey = $owner->getPrimaryKey();
if (is_array($primaryKey)) {
return implode('_', $primaryKey);
}
return $primaryKey;
}
/**
* Creates base part of the file name.
* This value will be append with the version and extension for the particular file.
* @return string file name's base part.
*/
protected function getFileBaseName()
{
return $this->getPrimaryKeyStringValue();
}
/**
* Returns current version value of the model's file.
* @return int current version of model's file.
*/
public function getCurrentFileVersion()
{
$owner = $this->owner;
return $owner->getAttribute($this->fileVersionAttribute);
}
/**
* Returns next version value of the model's file.
* @return int next version of model's file.
*/
public function getNextFileVersion()
{
return $this->getCurrentFileVersion() + 1;
}
/**
* Creates file itself name (without path) including version and extension.
* @param int $fileVersion file version number.
* @param string $fileExtension file extension.
* @return string file self name.
*/
public function getFileSelfName($fileVersion = null, $fileExtension = null)
{
$owner = $this->owner;
if ($fileVersion === null) {
$fileVersion = $this->getCurrentFileVersion();
}
if ($fileExtension === null) {
$fileExtension = $owner->getAttribute($this->fileExtensionAttribute);
}
return $this->getFileBaseName() . '_' . $fileVersion . '.' . $fileExtension;
}
/**
* Creates the file name in the file storage.
* This name contains the sub directory, resolved by [[subDirTemplate]].
* @param int $fileVersion file version number.
* @param string $fileExtension file extension.
* @return string file full name.
*/
public function getFileFullName($fileVersion = null, $fileExtension = null)
{
$fileName = $this->getFileSelfName($fileVersion, $fileExtension);
$subDir = $this->getActualSubDir();
if (!empty($subDir)) {
$fileName = $subDir . DIRECTORY_SEPARATOR . $fileName;
}
return $fileName;
}
// Main File Operations:
/**
* Associate new file with the owner model.
* This method will determine new file version and extension, and will update the owner
* model correspondingly.
* @param string|UploadedFile $sourceFileNameOrUploadedFile file system path to source file or [[UploadedFile]] instance.
* @param bool $deleteSourceFile determines would the source file be deleted in the process or not,
* if null given file will be deleted if it was uploaded via POST.
* @return bool save success.
*/
public function saveFile($sourceFileNameOrUploadedFile, $deleteSourceFile = null)
{
$this->deleteFile();
$fileVersion = $this->getNextFileVersion();
if (is_object($sourceFileNameOrUploadedFile)) {
$sourceFileName = $sourceFileNameOrUploadedFile->tempName;
$fileExtension = $sourceFileNameOrUploadedFile->getExtension();
} else {
$sourceFileName = $sourceFileNameOrUploadedFile;
$fileExtension = strtolower(pathinfo($sourceFileName, PATHINFO_EXTENSION));
}
$result = $this->newFile($sourceFileName, $fileVersion, $fileExtension);
if ($result) {
if ($deleteSourceFile === null) {
$deleteSourceFile = is_uploaded_file($sourceFileName);
}
if ($deleteSourceFile) {
unlink($sourceFileName);
}
$owner = $this->owner;
$attributes = [
$this->fileVersionAttribute => $fileVersion,
$this->fileExtensionAttribute => $fileExtension
];
$owner->updateAttributes($attributes);
}
return $result;
}
/**
* Creates the file for the model from the source file.
* File version and extension are passed to this method.
* @param string $sourceFileName - source full file name.
* @param int $fileVersion - file version number.
* @param string $fileExtension - file extension.
* @return bool success.
*/
protected function newFile($sourceFileName, $fileVersion, $fileExtension)
{
$fileFullName = $this->getFileFullName($fileVersion, $fileExtension);
$fileStorageBucket = $this->ensureFileStorageBucket();
return $fileStorageBucket->copyFileIn($sourceFileName, $fileFullName);
}
/**
* Removes file associated with the owner model.
* @return bool success.
*/
public function deleteFile()
{
$fileStorageBucket = $this->ensureFileStorageBucket();
$fileName = $this->getFileFullName();
if ($fileStorageBucket->fileExists($fileName)) {
return $fileStorageBucket->deleteFile($fileName);
}
return true;
}
/**
* Finds the uploaded through the web file, creating [[UploadedFile]] instance.
* If parameter $fullFileName is passed, creates a mock up instance of [[UploadedFile]] from the local file,
* passed with this parameter.
* @param UploadedFile|string|null $uploadedFile - source full file name for the [[UploadedFile]] mock up.
* @return UploadedFile|null uploaded file.
*/
protected function ensureUploadedFile($uploadedFile = null)
{
if ($uploadedFile instanceof UploadedFile) {
return $uploadedFile;
}
if (!empty($uploadedFile)) {
return new UploadedFile([
'name' => basename($uploadedFile),
'tempName' => $uploadedFile,
'type' => FileHelper::getMimeType($uploadedFile),
'size' => filesize($uploadedFile),
'error' => UPLOAD_ERR_OK
]);
}
if ($this->autoFetchUploadedFile) {
$owner = $this->owner;
$fileAttributeName = $this->fileAttribute;
$tabularInputIndex = $this->fileTabularInputIndex;
if ($tabularInputIndex !== null) {
$fileAttributeName = "[{$tabularInputIndex}]{$fileAttributeName}";
}
$uploadedFile = UploadedFile::getInstance($owner, $fileAttributeName);
if (is_object($uploadedFile)) {
if (!$uploadedFile->getHasError() && !file_exists($uploadedFile->tempName)) {
// uploaded file has been already processed:
return null;
} else {
return $uploadedFile;
}
}
}
return null;
}
// File Interface Function Shortcuts:
/**
* Checks if file related to the model exists.
* @return bool file exists.
*/
public function fileExists()
{
$fileStorageBucket = $this->ensureFileStorageBucket();
return $fileStorageBucket->fileExists($this->getFileFullName());
}
/**
* Returns the content of the model related file.
* @return string file content.
*/
public function getFileContent()
{
$fileStorageBucket = $this->ensureFileStorageBucket();
return $fileStorageBucket->getFileContent($this->getFileFullName());
}
/**
* Returns full web link to the model related file.
* @return string web link to file.
*/
public function getFileUrl()
{
$fileStorageBucket = $this->ensureFileStorageBucket();
$fileFullName = $this->getFileFullName();
if ($this->defaultFileUrl !== null) {
if (!$fileStorageBucket->fileExists($fileFullName)) {
return $this->defaultFileUrl;
}
}
return $fileStorageBucket->getFileUrl($fileFullName);
}
/**
* Opens a file as stream resource, e.g. like `fopen()` function.
* @param string $mode - the type of access you require to the stream, e.g. `r`, `w`, `a` and so on.
* You should prefer usage of simple modes like `r` and `w`, avoiding complex ones like `w+`, as they
* may not supported by some storages.
* @return resource|false file pointer resource on success, or `false` on error.
* @since 1.0.3
*/
public function openFile($mode)
{
$fileStorageBucket = $this->ensureFileStorageBucket();
return $fileStorageBucket->openFile($this->getFileFullName(), $mode);
}
// Property Access Extension:
/**
* PHP getter magic method.
* This method is overridden so that variation attributes can be accessed like properties.
*
* @param string $name property name
* @throws UnknownPropertyException if the property is not defined
* @return mixed property value
*/
public function __get($name)
{
try {
return parent::__get($name);
} catch (UnknownPropertyException $exception) {
if ($this->owner !== null) {
if ($name === $this->fileAttribute) {
return $this->getUploadedFile();
}
}
throw $exception;
}
}
/**
* PHP setter magic method.
* This method is overridden so that variation attributes can be accessed like properties.
* @param string $name property name
* @param mixed $value property value
* @throws UnknownPropertyException if the property is not defined
*/
public function __set($name, $value)
{
try {
parent::__set($name, $value);
} catch (UnknownPropertyException $exception) {
if ($this->owner !== null) {
if ($name === $this->fileAttribute) {
$this->setUploadedFile($value);
return;
}
}
throw $exception;
}
}
/**
* @inheritdoc
*/
public function canGetProperty($name, $checkVars = true)
{
if (parent::canGetProperty($name, $checkVars)) {
return true;
}
if ($this->owner === null) {
return false;
}
return ($name === $this->fileAttribute);
}
/**
* @inheritdoc
*/
public function canSetProperty($name, $checkVars = true)
{
if (parent::canSetProperty($name, $checkVars)) {
return true;
}
if ($this->owner === null) {
return false;
}
return ($name === $this->fileAttribute);
}
// Events:
/**
* Declares events and the corresponding event handler methods.
* @return array events (array keys) and the corresponding event handler methods (array values).
*/
public function events()
{
return [
BaseActiveRecord::EVENT_AFTER_INSERT => 'afterSave',
BaseActiveRecord::EVENT_AFTER_UPDATE => 'afterSave',
BaseActiveRecord::EVENT_AFTER_DELETE => 'afterDelete',
];
}
/**
* This event raises after owner saved.
* It saves uploaded file if it exists.
* @param \yii\base\Event $event event instance.
*/
public function afterSave($event)
{
$uploadedFile = $this->getUploadedFile();
if (is_object($uploadedFile) && !$uploadedFile->getHasError()) {
$this->saveFile($uploadedFile);
}
$this->setUploadedFile(null);
}
/**
* This event raises before owner deleted.
* It deletes related file.
* @param \yii\base\Event $event event instance.
*/
public function afterDelete($event)
{
$this->deleteFile();
}
}