Issues (19)

src/FileUploadTrait.php (2 issues)

Labels
1
<?php
2
3
4
namespace carono\yii2file;
5
6
use Yii;
7
use yii\db\ActiveRecord;
8
use yii\helpers\FileHelper;
9
use carono\yii2file\Uploader;
10
use yii\helpers\Url;
11
12
/**
13
 * Trait FileUploadTrait
14
 *
15
 * @package carono\yii2file
16
 * @property integer $id
17
 * @property string $uid
18
 * @property integer $user_id
19
 * @property string $name
20
 * @property string $extension
21
 * @property string $folder
22
 * @property string $mime_type
23
 * @property integer $size
24
 * @property string $data
25
 * @property string $session
26
 * @property string $md5
27
 * @property string $slug
28
 * @property integer $is_active
29
 * @property integer $is_exist
30
 * @property resource $binary
31
 * @property string $created_at
32
 * @property string $updated_at
33
 *
34
 * @property string $fileName
35
 * @property string $realFileName
36
 * @property string $realFilePath
37
 *
38
 * @mixin ActiveRecord
39
 */
40
trait FileUploadTrait
41
{
42
    public $fileNameAsUid = true;
43
    public $eraseOnDelete = true;
44
    public $uploaderClass = Uploader::class;
45
    public $fileUploadFolder = '@app/files';
46
    public $fileWebFolder = '@web/files';
47
48
    public function init()
49
    {
50
        $this->on(self::EVENT_BEFORE_DELETE, [$this, 'eraseOnDelete']);
51
        parent::init();
52
    }
53
54
    /**
55
     * @param $file
56
     * @return Uploader|mixed
57
     */
58
    public static function startUpload($file)
59
    {
60
        $model = new static();
61
        return \Yii::createObject([
62
            'class' => $model->uploaderClass,
63
            'modelClass' => static::class,
64
            'file' => $file,
65
            'folder' => $model->fileUploadFolder,
66
        ]);
67
    }
68
69
    /**
70
     * @return bool
71
     */
72
    public function deleteFile()
73
    {
74
        if ($this->fileExist()) {
75
            unlink($this->getRealFilePath());
76
            if ($f = !$this->fileExist()) {
77
                $this->updateAttributes(['is_exist' => false]);
78
            }
79
            return $f;
80
        }
81
82
        return true;
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public function getRealFileName()
89
    {
90
        if ($this->fileNameAsUid) {
91
            return $this->uid . '.' . $this->extension;
92
        }
93
94
        return $this->name . '.' . $this->extension;
95
    }
96
97
    /**
98
     * @return mixed
99
     */
100
    public function getRealFilePath()
101
    {
102
        $path = \Yii::getAlias($this->folder) . DIRECTORY_SEPARATOR . $this->getRealFileName();
0 ignored issues
show
Are you sure Yii::getAlias($this->folder) of type false|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

102
        $path = /** @scrutinizer ignore-type */ \Yii::getAlias($this->folder) . DIRECTORY_SEPARATOR . $this->getRealFileName();
Loading history...
103
        return str_replace('/', DIRECTORY_SEPARATOR, $path);
104
    }
105
106
    /**
107
     * @return bool
108
     */
109
    public function fileExist()
110
    {
111
        return file_exists($this->getRealFilePath());
112
    }
113
114
    /**
115
     * @return bool|null
116
     */
117
    public function isImage()
118
    {
119
        if (($mime = $this->mime_type) || ($mime = FileHelper::getMimeType($this->getRealFilePath()))) {
120
            return strpos($mime, 'image') === 0;
121
        }
122
123
        return null;
124
    }
125
126
    /**
127
     * @return string
128
     */
129
    public function getFileName()
130
    {
131
        return implode('.', array_filter([$this->name, $this->extension]));
132
    }
133
134
    public function eraseOnDelete()
135
    {
136
        if ($this->eraseOnDelete && $this->fileExist()) {
137
            $this->deleteFile();
138
        }
139
    }
140
141
    public function formUrl($web = null)
142
    {
143
        $class = $this->uploaderClass;
144
        return Url::to([Yii::getAlias($web ?: $this->fileWebFolder) . '/' . $class::formAliasPath($this->uid) . '/' . $this->uid . '.' . $this->extension], true);
0 ignored issues
show
Are you sure Yii::getAlias($web ?: $this->fileWebFolder) of type false|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

144
        return Url::to([/** @scrutinizer ignore-type */ Yii::getAlias($web ?: $this->fileWebFolder) . '/' . $class::formAliasPath($this->uid) . '/' . $this->uid . '.' . $this->extension], true);
Loading history...
145
    }
146
}