HEX
Server: LiteSpeed
System: Linux premium260.web-hosting.com 4.18.0-553.45.1.lve.el8.x86_64 #1 SMP Wed Mar 26 12:08:09 UTC 2025 x86_64
User: aliazzsr (627)
PHP: 7.4.33
Disabled: NONE
Upload Files
File: //home/aliazzsr/api.crm.vqode.com/models/core/UserFile.php
<?php
namespace app\models\core;

use app\components\BaseModel;
use app\models\enums\FileType;
use yii\web\UploadedFile;

/**
 * Class User
 * @package app\modules\ksa\models
 * @property string $type MIME type
 * @property string $filename file name
 * @property integer $size
 * @property integer $filetype_id
 * @property UploadedFile|string $content
 */
class UserFile extends BaseModel
{
    public static function tableName()
    {
        return 'user_image';
    }

    public function rules()
    {
        return [
            [['content'], 'required'],
            [
                ['content'], 'file',
                'extensions' => ['png', 'jpg', 'jpeg'],
                'maxSize' => 1024 * 1024,
                'mimeTypes' => ['image/jpeg', 'image/png', 'image/jpg'],
                'when' => function() {
                    return $this->content instanceof UploadedFile;
                },
                'tooBig' => 'File is too large. The maximum size allowed is 1Mb',
                'wrongExtension' => 'Wrong file format. Supports the following file formats: JPG, JPEG, PNG.',
                'wrongMimeType' => 'Wrong file format. Supports the following file formats: JPG, JPEG, PNG.',
            ],
        ];
    }

    public function fields()
    {
        return [
            'user_id',
            'filename',
            'size',
        ];
    }

    public function beforeSave($insert)
    {
        if ($this->content instanceof UploadedFile) {
            $this->type = $this->content->type;
            $this->size = $this->content->size;
            $this->filename = $this->content->name;
            $this->content = file_get_contents($this->content->tempName);
        }

        return parent::beforeSave($insert);
    }

    /**
     *
     * @param integer $userId user identifier
     * @return static
     */
    public static function findOrCreateAvatar($userId)
    {
        $attributes = [
            'user_id' => $userId,
            'filetype_id' => FileType::USER_AVATAR,
        ];

        return static::findOne($attributes) ?: new static($attributes);
    }
}