1 | <?php |
||
33 | class FileStorage extends Component |
||
34 | { |
||
35 | /** |
||
36 | * @var string Secret string used to create hashes of files. |
||
37 | * Required property, must be configured in config |
||
38 | */ |
||
39 | public $secret; |
||
40 | |||
41 | /** |
||
42 | * @var string Path to the directory for temporary files storage. |
||
43 | * Used to save file after upload until API downloads it. |
||
44 | * Defaults to `@runtime/tmp` |
||
45 | */ |
||
46 | public $tempDirectory = '@runtime/tmp'; |
||
47 | |||
48 | /** |
||
49 | * @var string Path to the directory for files permanent storing. |
||
50 | * Defaults to `@runtime/upload`. |
||
51 | */ |
||
52 | public $directory = '@runtime/upload'; |
||
53 | |||
54 | /** |
||
55 | * @var string The route that will be passed to API in order to download uploaded file. |
||
56 | * The action must accept 2 GET parameters: `filename` and `key`, then |
||
57 | * call [[FileStorage::readTemporary($filename, $key)]] and send file contents in its body. |
||
58 | * Defaults to `@file/temp-view`. |
||
59 | */ |
||
60 | public $temporaryViewRoute = '@file/temp-view'; |
||
61 | |||
62 | /** |
||
63 | * @var string Namespace of the class that represents File. |
||
64 | * Defaults to [[File]]. |
||
65 | */ |
||
66 | public $fileModelClass = File::class; |
||
67 | |||
68 | /** @inheritdoc */ |
||
69 | public function init() |
||
82 | |||
83 | /** |
||
84 | * Saves uploaded file under the [[tempDirectory]] with random file name |
||
85 | * |
||
86 | * @param UploadedFile $file |
||
87 | * @return string randomly generated file name |
||
88 | * @throws ErrorException when file is not saved |
||
89 | */ |
||
90 | public function saveUploadedFile(UploadedFile $file) |
||
103 | |||
104 | /** |
||
105 | * Builds path to the temporary location of $filename under the [[tempDirectory]] |
||
106 | * |
||
107 | * @param string $filename |
||
108 | * @return string full path to the temporary file |
||
109 | */ |
||
110 | protected function getTemporaryPath($filename = '') |
||
114 | |||
115 | /** |
||
116 | * Puts file $filename to the API. |
||
117 | * |
||
118 | * File must be previously saved to the [[tempDirectory]] using [[saveUploadedFile]] method, |
||
119 | * otherwise exception will be thrown. |
||
120 | * |
||
121 | * @param string $filename The temporary file name |
||
122 | * @param string $originalName Original (as file was uploaded) file name. Optional, defaults to $filename |
||
123 | * @return File The file model |
||
124 | * @throws Exception when file $filename does not exist |
||
125 | */ |
||
126 | public function put($filename, $originalName = null) |
||
149 | |||
150 | /** |
||
151 | * Builds key identifying the [[File]] model to be cached. |
||
152 | * @param integer $fileId |
||
153 | * @return array |
||
154 | * @see get |
||
155 | * @see getFileModel |
||
156 | */ |
||
157 | protected function buildCacheKey($fileId) |
||
161 | |||
162 | /** |
||
163 | * Gets the path of the file with $id |
||
164 | * |
||
165 | * Method downloads the requested file from the API and saves it to the local machine. |
||
166 | * Method respects authentication and access rules. |
||
167 | * |
||
168 | * @param integer $id the ID of the file |
||
169 | * @param bool $overrideCache whether the cache must be invalidated |
||
170 | * @return string full path to the file. File is located under the [[directory]] |
||
171 | * @throws Exception when fails to save file locally |
||
172 | * @throws ForbiddenHttpException when file is not available to client due to policies |
||
173 | */ |
||
174 | public function get($id, $overrideCache = false) |
||
197 | |||
198 | /** |
||
199 | * @return \yii\caching\Cache |
||
200 | */ |
||
201 | protected function getCache() |
||
205 | |||
206 | /** |
||
207 | * Retrieves [[File]] model for the $id. |
||
208 | * Uses cache and can get model from it. |
||
209 | * |
||
210 | * @param integer $id the ID of the file |
||
211 | * @param bool $overrideCache whether the cache must be invalidated |
||
212 | * @return File |
||
213 | * @throws ForbiddenHttpException when file is not available to client due to policies |
||
214 | */ |
||
215 | public function getFileModel($id, $overrideCache = false) |
||
237 | |||
238 | /** |
||
239 | * Return URL to the route that provides access to the temporary file. |
||
240 | * @param string $filename the file name |
||
241 | * @return string URL |
||
242 | * @see temporaryViewRoute |
||
243 | */ |
||
244 | protected function getTemporaryViewUrl($filename) |
||
248 | |||
249 | /** |
||
250 | * Builds MD5 hash using [[secret]] and $sting |
||
251 | * |
||
252 | * @param $string |
||
253 | * @return string MD5 hash |
||
254 | */ |
||
255 | protected function buildHash($string) |
||
259 | |||
260 | /** |
||
261 | * Gets path to the temporary file $filename located under the [[tempDirectory]] |
||
262 | * |
||
263 | * @param string $filename the file name |
||
264 | * @param string $key secret key that was previously generated by [[buildHash]] method unauthorized access |
||
265 | * @return string path to the temporary file |
||
266 | * @throws ForbiddenHttpException when failed to verify secret $key |
||
267 | * @throws NotFoundHttpException when the requested files does not exist |
||
268 | */ |
||
269 | public function getTemporary($filename, $key) |
||
282 | } |
||
283 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.