Test Failed
Push — main ( 5e663a...1f6f9b )
by Sugeng
03:11
created

FileAction::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace diecoding\flysystem\actions;
4
5
use DateTimeImmutable;
6
use diecoding\flysystem\LocalComponent;
7
use Yii;
8
use yii\base\Action;
9
use yii\helpers\Json;
10
use yii\web\NotFoundHttpException;
11
12
/**
13
 * FileAction for handle LocalComponent.
14
 *
15
 * To use FileAction, you need to do the following steps:
16
 *
17
 * First, declare an action of FileAction type in the `actions()` method of your `SiteController`
18
 * class (or whatever controller you prefer), like the following:
19
 *
20
 * ```php
21
 * public function actions()
22
 * {
23
 *     return [
24
 *         'file' => [
25
 *             'class' => \diecoding\flysystem\actions\FileAction::class,
26
 *             'component' => 'fs',
27
 *         ],
28
 *     ];
29
 * }
30
 * ```
31
 * 
32
 * @package diecoding\flysystem\actions
33
 * 
34
 * @link      https://sugengsulistiyawan.my.id/
35
 * @author    Sugeng Sulistiyawan <[email protected]>
36
 * @copyright Copyright (c) 2023
37
 */
38
class FileAction extends Action
39
{
40
    /**
41
     * @var string flysystem component
42
     */
43
    public $component = 'fs';
44
45
    /**
46
     * @var mixed|LocalComponent
47
     */
48
    protected $filesystem;
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function init()
54
    {
55
        $this->filesystem = Yii::$app->get($this->component);
56
    }
57
58
    /**
59
     * Runs the action.
60
     *
61
     * @param string|null $data
62
     * @return mixed result content
63
     * @throws NotFoundHttpException if data not valid
64
     */
65
    public function run($data = null)
66
    {
67
        try {
68
            $params = Json::decode($this->filesystem->decrypt($data));
69
70
            $now     = (int) (new DateTimeImmutable())->getTimestamp();
71
            $expires = (int) $params['expires'];
72
            // $config  = (array) $params['config'];
73
74
            if (!$this->filesystem->fileExists($params['path']) || $expires <= 0 || $expires < $now) {
75
                throw new NotFoundHttpException(Yii::t('yii', 'Page not found.'));
76
            }
77
78
            $content        = $this->filesystem->read($params['path']);
79
            $mimeType       = $this->filesystem->mimeType($params['path']);
80
            $attachmentName = pathinfo($params['path'], PATHINFO_BASENAME);
81
        } catch (\Throwable $th) {
82
            throw new NotFoundHttpException(Yii::t('yii', 'Page not found.'));
83
        }
84
85
        return Yii::$app->getResponse()->sendContentAsFile($content, $attachmentName, [
0 ignored issues
show
Bug introduced by
It seems like $attachmentName can also be of type array; however, parameter $attachmentName of yii\web\Response::sendContentAsFile() 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

85
        return Yii::$app->getResponse()->sendContentAsFile($content, /** @scrutinizer ignore-type */ $attachmentName, [
Loading history...
86
            'mimeType' => $mimeType,
87
            'inline'   => true,
88
        ]);
89
    }
90
}
91