|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace tkanstantsin\yii2fileupload; |
|
5
|
|
|
|
|
6
|
|
|
use tkanstantsin\fileupload\FileManager as BaseFileManager; |
|
7
|
|
|
use tkanstantsin\fileupload\formatter\Factory as FormatterFactory; |
|
8
|
|
|
use tkanstantsin\fileupload\model\IFile; |
|
9
|
|
|
use tkanstantsin\fileupload\model\Type; |
|
10
|
|
|
use tkanstantsin\fileupload\model\Type as FileType; |
|
11
|
|
|
use tkanstantsin\yii2fileupload\model\File; |
|
12
|
|
|
use yii\base\Component; |
|
|
|
|
|
|
13
|
|
|
use yii\base\InvalidConfigException; |
|
|
|
|
|
|
14
|
|
|
use yii\helpers\ArrayHelper; |
|
|
|
|
|
|
15
|
|
|
use yii\helpers\Url; |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class FileComponent |
|
19
|
|
|
* |
|
20
|
|
|
* @todo: create repository only for yii2-filemanager widget |
|
21
|
|
|
* @todo: proxy all methods to the real filemanager. |
|
22
|
|
|
*/ |
|
23
|
|
|
class FileManager extends Component |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Will hide file formatting exception if true |
|
27
|
|
|
* @var bool |
|
28
|
|
|
*/ |
|
29
|
|
|
public $silentMode = true; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Base url for uploading files |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
public $uploadBaseUrl; |
|
36
|
|
|
/** |
|
37
|
|
|
* Path to folder which would contain cached files. |
|
38
|
|
|
* /cache-base-path/alias/part-of-hash/file-name |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
public $cacheBasePath; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Url for default image |
|
45
|
|
|
* @var string|array |
|
46
|
|
|
*/ |
|
47
|
|
|
public $imageNotFoundUrl; |
|
48
|
|
|
/** |
|
49
|
|
|
* Url for 404 page for files |
|
50
|
|
|
* @var string|array |
|
51
|
|
|
*/ |
|
52
|
|
|
public $fileNotFoundUrl; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var BaseFileManager |
|
56
|
|
|
*/ |
|
57
|
|
|
public $manager; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @inheritdoc |
|
61
|
|
|
* @throws \ErrorException |
|
62
|
|
|
* @throws \yii\base\InvalidConfigException |
|
63
|
|
|
*/ |
|
64
|
|
|
public function init(): void |
|
65
|
|
|
{ |
|
66
|
|
|
if ($this->uploadBaseUrl === null) { |
|
67
|
|
|
throw new \ErrorException('Base upload url must be defined.'); |
|
68
|
|
|
} |
|
69
|
|
|
if ($this->cacheBasePath === null) { |
|
70
|
|
|
throw new \ErrorException('Base path for cache must be defined.'); |
|
71
|
|
|
} |
|
72
|
|
|
if ($this->imageNotFoundUrl === null || $this->fileNotFoundUrl === null) { |
|
73
|
|
|
throw new \ErrorException('URLs for not founded image and file must be defined.'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$this->manager['contentFS'] = \Yii::$app->{$this->manager['contentFS']}->getFileSystem(); |
|
|
|
|
|
|
77
|
|
|
$this->manager['cacheFS'] = \Yii::$app->{$this->manager['cacheFS']}->getFileSystem(); |
|
78
|
|
|
|
|
79
|
|
|
$class = ArrayHelper::remove($this->manager, 'class', BaseFileManager::class); |
|
80
|
|
|
$this->manager = new $class($this->manager); |
|
81
|
|
|
if (!($this->manager instanceof BaseFileManager)) { |
|
82
|
|
|
throw new InvalidConfigException(\Yii::t('yii2fileupload', 'Invalid file manager config')); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
parent::init(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Generates url for upload file with upload widget. |
|
90
|
|
|
* Url format: $uploadBaseUrl/$alias/$id |
|
91
|
|
|
* @example /upload/product/555 |
|
92
|
|
|
* @param string $aliasName |
|
93
|
|
|
* @param int $id of related model |
|
94
|
|
|
* @return string |
|
95
|
|
|
* @throws \yii\base\InvalidParamException |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getUploadUrl(string $aliasName, int $id = null): string |
|
98
|
|
|
{ |
|
99
|
|
|
return implode(DIRECTORY_SEPARATOR, array_filter([$this->uploadBaseUrl, $aliasName, $id])); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Caches file and returns url to it. Return 404 image or link if fails |
|
104
|
|
|
* without exception. |
|
105
|
|
|
* @param IFile|null $file |
|
106
|
|
|
* @param string $format |
|
107
|
|
|
* @param array $formatterConfig |
|
108
|
|
|
* @return string|null |
|
109
|
|
|
* @throws \InvalidArgumentException |
|
110
|
|
|
* @throws \yii\base\InvalidParamException |
|
111
|
|
|
* @throws \RuntimeException |
|
112
|
|
|
* @throws \Exception |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getFileUrl(?IFile $file, string $format, array $formatterConfig = []): ?string |
|
115
|
|
|
{ |
|
116
|
|
|
if ($file === null) { |
|
117
|
|
|
return null; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
if ($file->getId() !== null) { // null if file is not saved yet |
|
121
|
|
|
try { |
|
122
|
|
|
$path = $this->manager->getFilePath($file, $format, $formatterConfig); |
|
123
|
|
|
if ($path !== null) { |
|
124
|
|
|
return implode(DIRECTORY_SEPARATOR, [ |
|
125
|
|
|
$this->cacheBasePath, |
|
126
|
|
|
$path, |
|
127
|
|
|
]); |
|
128
|
|
|
} |
|
129
|
|
|
} catch (\Exception $e) { |
|
130
|
|
|
if (!$this->silentMode) { |
|
131
|
|
|
throw $e; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return $this->getNotFoundUrl($file); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Choose 404 url |
|
141
|
|
|
* @param IFile $file |
|
142
|
|
|
* @return string |
|
143
|
|
|
* @throws \yii\base\InvalidParamException |
|
144
|
|
|
*/ |
|
145
|
|
|
public function getNotFoundUrl(IFile $file): string |
|
146
|
|
|
{ |
|
147
|
|
|
switch ($file->getType()) { |
|
148
|
|
|
case Type::IMAGE: |
|
149
|
|
|
return Url::to($this->imageNotFoundUrl); |
|
150
|
|
|
case Type::FILE: |
|
151
|
|
|
default: |
|
152
|
|
|
return Url::to($this->fileNotFoundUrl); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Generates file info for response to jQueryFileUpload widget. |
|
158
|
|
|
* @param File $file |
|
159
|
|
|
* @return array |
|
160
|
|
|
* @throws \RuntimeException |
|
161
|
|
|
* @throws \InvalidArgumentException |
|
162
|
|
|
* @throws \yii\base\InvalidParamException |
|
163
|
|
|
* @throws \League\Flysystem\FileNotFoundException |
|
164
|
|
|
* @throws \tkanstantsin\fileupload\config\InvalidConfigException |
|
165
|
|
|
*/ |
|
166
|
|
|
public function getJQueryUploadFileData(File $file): array |
|
167
|
|
|
{ |
|
168
|
|
|
return [ |
|
169
|
|
|
// file model |
|
170
|
|
|
'id' => $file->getId(), |
|
171
|
|
|
'alias' => $file->getModelAlias(), |
|
172
|
|
|
'model_id' => $file->getModelId(), |
|
173
|
|
|
// file info |
|
174
|
|
|
'name' => $file->getFullName(), |
|
175
|
|
|
'size' => $file->getSize(), |
|
176
|
|
|
'is_deleted' => (integer) $file->is_deleted, |
|
177
|
|
|
'is_confirmed' => (integer) $file->is_confirmed, |
|
178
|
|
|
// urls |
|
179
|
|
|
// path to full image or file itself. |
|
180
|
|
|
'url' => Url::to($this->manager->getFilePath($file, FormatterFactory::FILE_ORIGINAL)), |
|
181
|
|
|
// path to image thumbnail or file icon. |
|
182
|
|
|
'preview_url' => $file->getType() === FileType::IMAGE |
|
183
|
|
|
? Url::to($this->manager->getFilePath($file, FormatterFactory::IMAGE_DEFAULT_FORMAT)) |
|
184
|
|
|
: null, |
|
185
|
|
|
'icon' => $this->manager->getIcon($file->getExtension()), |
|
186
|
|
|
]; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Generates file info for multiple files for jQuery File Upload widget. |
|
191
|
|
|
* @param IFile[] $fileArray |
|
192
|
|
|
* @return array |
|
193
|
|
|
*/ |
|
194
|
|
|
public function getJQueryFileDataArray(array $fileArray): array |
|
195
|
|
|
{ |
|
196
|
|
|
return array_map([$this, 'getFileData'], array_values($fileArray)); |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths