Completed
Push — master ( c5eccd...ec8b64 )
by Evgenii
04:45
created

GetFileAction::run()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 19
c 1
b 0
f 1
dl 0
loc 26
rs 9.3222
cc 5
nc 4
nop 1
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\web\NotFoundHttpException;
10
11
class GetFileAction extends \yii\base\Action
12
{
13
    public function run($hash)
14
    {
15
        $model = File::findOne(['hash' => $hash]);
16
17
        if (!$model)
0 ignored issues
show
introduced by
$model is of type yii\db\ActiveRecord, thus it always evaluated to true.
Loading history...
18
            throw new NotFoundHttpException("Запрашиваемый файл не найден");
19
20
        if (!file_exists($model->rootPath))
21
            throw new NotFoundHttpException('Запрашиваемый файл не найден на диске.');
22
23
        Yii::$app->response->headers->set('Last-Modified', date("c", $model->created));
24
        Yii::$app->response->headers->set('Cache-Control', 'public, max-age=' . (60 * 60 * 24 * 15));
25
26
        if ($model->type == FileType::IMAGE && $model->watermark) {
27
            $image = new SimpleImage();
28
            $image->load($model->rootPath);
29
            $image->watermark(Yii::getAlias("@frontend/web/design/logo-big.png"));
30
            $tmpName = Yii::getAlias("@runtime/" . md5(time() . $model->id));
31
            $image->save($tmpName, IMAGETYPE_JPEG);
32
            $stream = fopen($tmpName, 'rb');
33
            Yii::$app->response->sendStreamAsFile($stream, $model->title, ['inline' => true, 'mimeType' => "image/jpeg", 'filesize' => $model->size]);
0 ignored issues
show
Bug introduced by
It seems like $stream can also be of type false; however, parameter $handle of yii\web\Response::sendStreamAsFile() does only seem to accept resource, 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

33
            Yii::$app->response->sendStreamAsFile(/** @scrutinizer ignore-type */ $stream, $model->title, ['inline' => true, 'mimeType' => "image/jpeg", 'filesize' => $model->size]);
Loading history...
34
            @unlink($tmpName);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

34
            /** @scrutinizer ignore-unhandled */ @unlink($tmpName);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
35
36
        } else {
37
            $stream = fopen($model->rootPath, 'rb');
38
            Yii::$app->response->sendStreamAsFile($stream, $model->title, ['inline' => true, 'mimeType' => $model->content_type, 'filesize' => $model->size]);
39
        }
40
    }
41
}
42