1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace tkanstantsin\yii2fileupload; |
5
|
|
|
|
6
|
|
|
use tkanstantsin\fileupload\FileManager as BaseFileManager; |
7
|
|
|
use tkanstantsin\fileupload\model\IFile; |
8
|
|
|
use tkanstantsin\fileupload\model\Type as FileType; |
9
|
|
|
use yii\base\Component; |
|
|
|
|
10
|
|
|
use yii\base\InvalidConfigException; |
|
|
|
|
11
|
|
|
use yii\helpers\ArrayHelper; |
|
|
|
|
12
|
|
|
use yii\helpers\Url; |
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class FileComponent |
16
|
|
|
* |
17
|
|
|
* @todo: create repository only for yii2-filemanager widget |
18
|
|
|
* @todo: proxy all methods to the real filemanager. |
19
|
|
|
*/ |
20
|
|
|
class FileManager extends Component |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Will hide file formatting exception if true |
24
|
|
|
* @var bool |
25
|
|
|
*/ |
26
|
|
|
public $silentMode = true; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Base url for uploading files |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
public $uploadBaseUrl; |
33
|
|
|
/** |
34
|
|
|
* Path to folder which would contain cached files. |
35
|
|
|
* /cache-base-path/alias/part-of-hash/file-name |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
public $cacheBasePath; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Url for default image |
42
|
|
|
* @var string|array |
43
|
|
|
*/ |
44
|
|
|
public $imageNotFoundUrl; |
45
|
|
|
/** |
46
|
|
|
* Url for 404 page for files |
47
|
|
|
* @var string|array |
48
|
|
|
*/ |
49
|
|
|
public $fileNotFoundUrl; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var BaseFileManager |
53
|
|
|
*/ |
54
|
|
|
public $manager; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @inheritdoc |
58
|
|
|
* @throws \ErrorException |
59
|
|
|
* @throws \yii\base\InvalidConfigException |
60
|
|
|
*/ |
61
|
|
|
public function init(): void |
62
|
|
|
{ |
63
|
|
|
if ($this->uploadBaseUrl === null) { |
64
|
|
|
throw new \ErrorException('Base upload url must be defined.'); |
65
|
|
|
} |
66
|
|
|
if ($this->cacheBasePath === null) { |
67
|
|
|
throw new \ErrorException('Base path for cache must be defined.'); |
68
|
|
|
} |
69
|
|
|
if ($this->imageNotFoundUrl === null || $this->fileNotFoundUrl === null) { |
70
|
|
|
throw new \ErrorException('URLs for not founded image and file must be defined.'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->manager['contentFS'] = \Yii::$app->{$this->manager['contentFS']}->getFileSystem(); |
|
|
|
|
74
|
|
|
$this->manager['cacheFS'] = \Yii::$app->{$this->manager['cacheFS']}->getFileSystem(); |
75
|
|
|
|
76
|
|
|
$class = ArrayHelper::remove($this->manager, 'class', BaseFileManager::class); |
77
|
|
|
$this->manager = new $class($this->manager); |
78
|
|
|
if (!($this->manager instanceof BaseFileManager)) { |
79
|
|
|
throw new InvalidConfigException(\Yii::t('yii2fileupload', 'Invalid file manager config')); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
parent::init(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Generates url for upload file with upload widget. |
87
|
|
|
* Url format: $uploadBaseUrl/$alias/$id |
88
|
|
|
* @example /upload/product/555 |
89
|
|
|
* @param string $aliasName |
90
|
|
|
* @param int $id of related model |
91
|
|
|
* @return string |
92
|
|
|
* @throws \yii\base\InvalidParamException |
93
|
|
|
*/ |
94
|
|
|
public function getUploadUrl(string $aliasName, int $id = null): string |
95
|
|
|
{ |
96
|
|
|
return implode(DIRECTORY_SEPARATOR, array_filter([$this->uploadBaseUrl, $aliasName, $id])); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param IFile|null $file |
101
|
|
|
* @param string $format |
102
|
|
|
* @param array $formatterConfig |
103
|
|
|
* @return string |
104
|
|
|
* @throws \Exception |
105
|
|
|
* @throws \yii\base\InvalidParamException |
106
|
|
|
*/ |
107
|
|
|
public function getFileUrl(?IFile $file, string $format, array $formatterConfig = []): string |
108
|
|
|
{ |
109
|
|
|
$path = $this->getFilePath($file, $format, $formatterConfig); |
110
|
|
|
if ($path !== null) { |
111
|
|
|
return $this->cacheBasePath . DIRECTORY_SEPARATOR . $path; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$fileTypeId = $file !== null ? $file->getType() : FileType::FILE; |
115
|
|
|
|
116
|
|
|
return $this->getNotFoundUrl($fileTypeId); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Format file and return image link if failed |
121
|
|
|
* @param IFile|null $file |
122
|
|
|
* @param string $format |
123
|
|
|
* @param array $formatterConfig |
124
|
|
|
* @param string|null $notFoundUrl |
125
|
|
|
* @return string |
126
|
|
|
* @throws \Exception |
127
|
|
|
* @throws \yii\base\InvalidParamException |
128
|
|
|
*/ |
129
|
|
|
public function getImageUrl(?IFile $file, string $format, array $formatterConfig = [], string $notFoundUrl = null): string |
130
|
|
|
{ |
131
|
|
|
$path = $this->getFilePath($file, $format, $formatterConfig); |
132
|
|
|
if ($path !== null) { |
133
|
|
|
return $this->cacheBasePath . DIRECTORY_SEPARATOR . $path; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $notFoundUrl ?? $this->getNotFoundUrl(FileType::IMAGE); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Choose 404 url |
141
|
|
|
* @param int $fileTypeId |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
public function getNotFoundUrl(int $fileTypeId): string |
145
|
|
|
{ |
146
|
|
|
switch ($fileTypeId) { |
147
|
|
|
case FileType::IMAGE: |
148
|
|
|
return Url::to($this->imageNotFoundUrl); |
149
|
|
|
case FileType::FILE: |
150
|
|
|
default: |
151
|
|
|
return Url::to($this->fileNotFoundUrl); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Caches file and returns url to it. Return 404 image or link if fails |
157
|
|
|
* without exception. |
158
|
|
|
* @param IFile|null $file |
159
|
|
|
* @param string $format |
160
|
|
|
* @param array $formatterConfig |
161
|
|
|
* @return string|null |
162
|
|
|
* @throws \InvalidArgumentException |
163
|
|
|
* @throws \yii\base\InvalidParamException |
164
|
|
|
* @throws \RuntimeException |
165
|
|
|
* @throws \Exception |
166
|
|
|
*/ |
167
|
|
|
protected function getFilePath(?IFile $file, string $format, array $formatterConfig = []): ?string |
168
|
|
|
{ |
169
|
|
|
if ($file === null || $file->getId() !== null /*null if file is not saved yet*/) { |
170
|
|
|
return null; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
try { |
174
|
|
|
return $this->manager->getFilePath($file, $format, $formatterConfig); |
175
|
|
|
} catch (\Exception $e) { |
176
|
|
|
if (!$this->silentMode) { |
177
|
|
|
throw $e; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return null; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
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