File   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 115
ccs 0
cts 22
cp 0
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getStreamExt() 0 15 4
A getExtBySignature() 0 12 3
1
<?php
2
3
namespace Kaylyu\Alipay\Kernel\Support;
4
5
use finfo;
6
7
/**
8
 * Class File.
9
 */
10
class File
11
{
12
    /**
13
     * MIME mapping.
14
     *
15
     * @var array
16
     */
17
    protected static $extensionMap = [
18
        'audio/wav' => '.wav',
19
        'audio/x-ms-wma' => '.wma',
20
        'video/x-ms-wmv' => '.wmv',
21
        'video/mp4' => '.mp4',
22
        'audio/mpeg' => '.mp3',
23
        'audio/amr' => '.amr',
24
        'application/vnd.rn-realmedia' => '.rm',
25
        'audio/mid' => '.mid',
26
        'image/bmp' => '.bmp',
27
        'image/gif' => '.gif',
28
        'image/png' => '.png',
29
        'image/tiff' => '.tiff',
30
        'image/jpeg' => '.jpg',
31
        'application/pdf' => '.pdf',
32
33
        // 列举更多的文件 mime, 企业号是支持的,公众平台这边之后万一也更新了呢
34
        'application/msword' => '.doc',
35
36
        'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => '.docx',
37
        'application/vnd.openxmlformats-officedocument.wordprocessingml.template' => '.dotx',
38
        'application/vnd.ms-word.document.macroEnabled.12' => '.docm',
39
        'application/vnd.ms-word.template.macroEnabled.12' => '.dotm',
40
41
        'application/vnd.ms-excel' => '.xls',
42
43
        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => '.xlsx',
44
        'application/vnd.openxmlformats-officedocument.spreadsheetml.template' => '.xltx',
45
        'application/vnd.ms-excel.sheet.macroEnabled.12' => '.xlsm',
46
        'application/vnd.ms-excel.template.macroEnabled.12' => '.xltm',
47
        'application/vnd.ms-excel.addin.macroEnabled.12' => '.xlam',
48
        'application/vnd.ms-excel.sheet.binary.macroEnabled.12' => '.xlsb',
49
50
        'application/vnd.ms-powerpoint' => '.ppt',
51
52
        'application/vnd.openxmlformats-officedocument.presentationml.presentation' => '.pptx',
53
        'application/vnd.openxmlformats-officedocument.presentationml.template' => '.potx',
54
        'application/vnd.openxmlformats-officedocument.presentationml.slideshow' => '.ppsx',
55
        'application/vnd.ms-powerpoint.addin.macroEnabled.12' => '.ppam',
56
    ];
57
58
    /**
59
     * File header signatures.
60
     *
61
     * @var array
62
     */
63
    protected static $signatures = [
64
        'ffd8ff' => '.jpg',
65
        '424d' => '.bmp',
66
        '47494638' => '.gif',
67
        '2f55736572732f6f7665' => '.png',
68
        '89504e47' => '.png',
69
        '494433' => '.mp3',
70
        'fffb' => '.mp3',
71
        'fff3' => '.mp3',
72
        '3026b2758e66cf11' => '.wma',
73
        '52494646' => '.wav',
74
        '57415645' => '.wav',
75
        '41564920' => '.avi',
76
        '000001ba' => '.mpg',
77
        '000001b3' => '.mpg',
78
        '2321414d52' => '.amr',
79
        '25504446' => '.pdf',
80
    ];
81
82
    /**
83
     * Return steam extension.
84
     *
85
     * @param string $stream
86
     *
87
     * @return string|false
88
     */
89
    public static function getStreamExt($stream)
90
    {
91
        try {
92
            if (is_readable($stream)) {
93
                $stream = file_get_contents($stream);
94
            }
95
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
96
        }
97
98
        $fileInfo = new finfo(FILEINFO_MIME);
99
100
        $mime = strstr($fileInfo->buffer($stream), ';', true);
101
102
        return isset(self::$extensionMap[$mime]) ? self::$extensionMap[$mime] : self::getExtBySignature($stream);
103
    }
104
105
    /**
106
     * Get file extension by file header signature.
107
     *
108
     * @param string $stream
109
     *
110
     * @return string
111
     */
112
    public static function getExtBySignature($stream)
113
    {
114
        $prefix = strval(bin2hex(mb_strcut($stream, 0, 10)));
115
116
        foreach (self::$signatures as $signature => $extension) {
117
            if (0 === strpos($prefix, strval($signature))) {
118
                return $extension;
119
            }
120
        }
121
122
        return '';
123
    }
124
}
125