Completed
Push — 6.0 ( fca71f...2ef662 )
by liu
07:18
created

File::isContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: liu21st <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think\response;
14
15
use think\Exception;
16
use think\Response;
17
18
/**
19
 * File Response
20
 */
21
class File extends Response
22
{
23
    protected $expire = 360;
24
    protected $name;
25
    protected $mimeType;
26
    protected $isContent = false;
27
28
    public function __construct($data = '', int $code = 200)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
29
    {
30
        $this->init($data, $code);
31
    }
32
33
    /**
34
     * 处理数据
35
     * @access protected
36
     * @param  mixed $data 要处理的数据
37
     * @return mixed
38
     * @throws \Exception
39
     */
40
    protected function output($data)
41
    {
42
        if (!$this->isContent && !is_file($data)) {
43
            throw new Exception('file not exists:' . $data);
44
        }
45
46
        ob_end_clean();
47
48
        if (!empty($this->name)) {
49
            $name = $this->name;
50
        } else {
51
            $name = !$this->isContent ? pathinfo($data, PATHINFO_BASENAME) : '';
52
        }
53
54
        if ($this->isContent) {
55
            $mimeType = $this->mimeType;
56
            $size     = strlen($data);
57
        } else {
58
            $mimeType = $this->getMimeType($data);
59
            $size     = filesize($data);
60
        }
61
62
        $this->header['Pragma']                    = 'public';
63
        $this->header['Content-Type']              = $mimeType ?: 'application/octet-stream';
64
        $this->header['Cache-control']             = 'max-age=' . $this->expire;
65
        $this->header['Content-Disposition']       = 'attachment; filename="' . $name . '"';
66
        $this->header['Content-Length']            = $size;
67
        $this->header['Content-Transfer-Encoding'] = 'binary';
68
        $this->header['Expires']                   = gmdate("D, d M Y H:i:s", time() + $this->expire) . ' GMT';
69
70
        $this->lastModified(gmdate('D, d M Y H:i:s', time()) . ' GMT');
71
72
        return $this->isContent ? $data : file_get_contents($data);
73
    }
74
75
    /**
76
     * 设置是否为内容 必须配合mimeType方法使用
77
     * @access public
78
     * @param  bool $content
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
79
     * @return $this
80
     */
81
    public function isContent(bool $content = true)
82
    {
83
        $this->isContent = $content;
84
        return $this;
85
    }
86
87
    /**
88
     * 设置有效期
89
     * @access public
90
     * @param  integer $expire 有效期
91
     * @return $this
92
     */
93
    public function expire(int $expire)
94
    {
95
        $this->expire = $expire;
96
        return $this;
97
    }
98
99
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $mimeType should have a doc-comment as per coding-style.
Loading history...
100
     * 设置文件类型
101
     * @access public
102
     * @param  string $filename 文件名
0 ignored issues
show
Coding Style introduced by
Doc comment for parameter $filename does not match actual variable name $mimeType
Loading history...
103
     * @return $this
104
     */
105
    public function mimeType(string $mimeType)
106
    {
107
        $this->mimeType = $mimeType;
108
        return $this;
109
    }
110
111
    /**
112
     * 获取文件类型信息
113
     * @access public
114
     * @param  string $filename 文件名
115
     * @return string
116
     */
117
    protected function getMimeType(string $filename): string
118
    {
119
        if (!empty($this->mimeType)) {
120
            return $this->mimeType;
121
        }
122
123
        $finfo = finfo_open(FILEINFO_MIME_TYPE);
124
125
        return finfo_file($finfo, $filename);
126
    }
127
128
    /**
129
     * 设置下载文件的显示名称
130
     * @access public
131
     * @param  string $filename 文件名
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
132
     * @param  bool   $extension 后缀自动识别
133
     * @return $this
134
     */
135
    public function name(string $filename, bool $extension = true)
136
    {
137
        $this->name = $filename;
138
139
        if ($extension && false === strpos($filename, '.')) {
140
            $this->name .= '.' . pathinfo($this->data, PATHINFO_EXTENSION);
141
        }
142
143
        return $this;
144
    }
145
}
146