Passed
Push — master ( 973570...86b515 )
by Carlos
48:23 queued 11s
created

File::getExtBySignature()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

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