Issues (183)

src/actions/GetFileAction.php (3 issues)

1
<?php
2
3
namespace floor12\files\actions;
4
5
use floor12\files\components\SimpleImage;
6
use floor12\files\models\File;
7
use floor12\files\models\FileType;
8
use Yii;
9
use yii\base\Action;
10
use yii\web\NotFoundHttpException;
11
12
class GetFileAction extends Action
13
{
14
    public function run($hash)
15
    {
16
        $model = File::findOne(['hash' => $hash]);
17
18
        if (!$model)
0 ignored issues
show
$model is of type yii\db\ActiveRecord, thus it always evaluated to true.
Loading history...
19
            throw new NotFoundHttpException("Запрашиваемый файл не найден");
20
21
        if (!file_exists($model->rootPath))
0 ignored issues
show
It seems like $model->rootPath can also be of type null; however, parameter $filename of file_exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

21
        if (!file_exists(/** @scrutinizer ignore-type */ $model->rootPath))
Loading history...
22
            throw new NotFoundHttpException('Запрашиваемый файл не найден на диске.');
23
24
        Yii::$app->response->headers->set('Last-Modified', date("c", $model->created));
25
        Yii::$app->response->headers->set('Cache-Control', 'public, max-age=' . (60 * 60 * 24 * 15));
26
27
        if ($model->type == FileType::IMAGE && $model->watermark) {
28
            $image = new SimpleImage();
29
            $image->load($model->rootPath);
30
            $image->watermark($model->watermark);
31
            $res = $image->output(IMAGETYPE_JPEG);
32
            Yii::$app->response->sendContentAsFile($res, $model->title, ['inline' => true, 'mimeType' => "image/jpeg", 'filesize' => $model->size]);
33
        } else {
34
            $stream = fopen($model->rootPath, 'rb');
0 ignored issues
show
It seems like $model->rootPath can also be of type null; however, parameter $filename of fopen() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

34
            $stream = fopen(/** @scrutinizer ignore-type */ $model->rootPath, 'rb');
Loading history...
35
            Yii::$app->response->sendStreamAsFile($stream, $model->title, ['inline' => true, 'mimeType' => $model->content_type, 'filesize' => $model->size]);
36
        }
37
    }
38
}
39