ROAResearch /
yii2-roa
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace roaresearch\yii2\roa\actions; |
||||||
| 4 | |||||||
| 5 | use roaresearch\yii2\roa\FileRecord; |
||||||
| 6 | use Yii; |
||||||
| 7 | use yii\base\InvalidConfigException; |
||||||
| 8 | |||||||
| 9 | /** |
||||||
| 10 | * Access and show s the content of a file on the browser or download it. |
||||||
| 11 | * |
||||||
| 12 | * @author Angel (Faryshta) Guevara <[email protected]> |
||||||
| 13 | */ |
||||||
| 14 | class FileStream extends Action |
||||||
| 15 | { |
||||||
| 16 | /** |
||||||
| 17 | * @var string GET parameter to decide if force the download or show it on |
||||||
| 18 | * the browser. |
||||||
| 19 | */ |
||||||
| 20 | public $downloadParam = 'download'; |
||||||
| 21 | |||||||
| 22 | /** |
||||||
| 23 | * @inheritdoc |
||||||
| 24 | */ |
||||||
| 25 | public function init() |
||||||
| 26 | { |
||||||
| 27 | parent::init(); |
||||||
| 28 | $interfaces = class_implements($this->modelClass); |
||||||
| 29 | if (empty($interfaces[FileRecord::class])) { |
||||||
| 30 | throw new InvalidConfigException( |
||||||
| 31 | "The class `{$this->modelClass}` must implement " |
||||||
| 32 | . FileRecord::class |
||||||
| 33 | ); |
||||||
| 34 | } |
||||||
| 35 | } |
||||||
| 36 | |||||||
| 37 | /** |
||||||
| 38 | * Shows the file on the browser or download it after checking access. |
||||||
| 39 | * |
||||||
| 40 | * @param mixed $id the identifier value. |
||||||
| 41 | * @param string $ext the requested file extension. |
||||||
| 42 | */ |
||||||
| 43 | public function run($id, string $ext) |
||||||
| 44 | { |
||||||
| 45 | $this->checkAccess( |
||||||
| 46 | ($model = $this->findModel($id)), |
||||||
| 47 | Yii::$app->request->getQueryParams() |
||||||
| 48 | ); |
||||||
| 49 | |||||||
| 50 | return Yii::$app->response->sendFile( |
||||||
| 51 | $model->filePath($ext), |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 52 | $model->fileName($ext), |
||||||
|
0 ignored issues
–
show
The method
fileName() does not exist on yii\db\ActiveRecordInterface. It seems like you code against a sub-type of yii\db\ActiveRecordInterface such as yii\db\BaseActiveRecord.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 53 | [ |
||||||
| 54 | 'mimeType' => $model->fileMimeType($ext), |
||||||
|
0 ignored issues
–
show
The method
fileMimeType() does not exist on yii\db\ActiveRecordInterface. It seems like you code against a sub-type of yii\db\ActiveRecordInterface such as yii\db\BaseActiveRecord.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 55 | 'inline' => !Yii::$app->request |
||||||
| 56 | ->getQueryParam($this->downloadParam, false), |
||||||
| 57 | ] |
||||||
| 58 | ); |
||||||
| 59 | } |
||||||
| 60 | } |
||||||
| 61 |