File   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 109
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getStreamExt() 0 12 4
A getExtBySignature() 0 12 3
1
<?php
2
3
namespace YEntWeChat\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
32
        // 列举更多的文件 mime, 企业号是支持的, 公众平台这边之后万一也更新了呢
33
        'application/msword'           => '.doc',
34
35
        'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => '.docx',
36
        'application/vnd.openxmlformats-officedocument.wordprocessingml.template' => '.dotx',
37
        'application/vnd.ms-word.document.macroEnabled.12'                        => '.docm',
38
        'application/vnd.ms-word.template.macroEnabled.12'                        => '.dotm',
39
40
        'application/vnd.ms-excel' => '.xls',
41
42
        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'    => '.xlsx',
43
        'application/vnd.openxmlformats-officedocument.spreadsheetml.template' => '.xltx',
44
        'application/vnd.ms-excel.sheet.macroEnabled.12'                       => '.xlsm',
45
        'application/vnd.ms-excel.template.macroEnabled.12'                    => '.xltm',
46
        'application/vnd.ms-excel.addin.macroEnabled.12'                       => '.xlam',
47
        'application/vnd.ms-excel.sheet.binary.macroEnabled.12'                => '.xlsb',
48
49
        'application/vnd.ms-powerpoint' => '.ppt',
50
51
        'application/vnd.openxmlformats-officedocument.presentationml.presentation' => '.pptx',
52
        'application/vnd.openxmlformats-officedocument.presentationml.template'     => '.potx',
53
        'application/vnd.openxmlformats-officedocument.presentationml.slideshow'    => '.ppsx',
54
        'application/vnd.ms-powerpoint.addin.macroEnabled.12'                       => '.ppam',
55
    ];
56
57
    /**
58
     * File header signatures.
59
     *
60
     * @var array
61
     */
62
    protected static $signatures = [
63
        'ffd8ff'           => '.jpg',
64
        '424d'             => '.bmp',
65
        '47494638'         => '.gif',
66
        '89504e47'         => '.png',
67
        '494433'           => '.mp3',
68
        'fffb'             => '.mp3',
69
        'fff3'             => '.mp3',
70
        '3026b2758e66cf11' => '.wma',
71
        '52494646'         => '.wav',
72
        '57415645'         => '.wav',
73
        '41564920'         => '.avi',
74
        '000001ba'         => '.mpg',
75
        '000001b3'         => '.mpg',
76
        '2321414d52'       => '.amr',
77
    ];
78
79
    /**
80
     * Return steam extension.
81
     *
82
     * @param string $stream
83
     *
84
     * @return string
85
     */
86
    public static function getStreamExt($stream)
87
    {
88
        if (is_dir(pathinfo($stream, PATHINFO_DIRNAME)) && is_readable($stream)) {
89
            $stream = file_get_contents($stream);
90
        }
91
92
        $finfo = new finfo(FILEINFO_MIME);
93
94
        $mime = strstr($finfo->buffer($stream), ';', true);
95
96
        return isset(self::$extensionMap[$mime]) ? self::$extensionMap[$mime] : self::getExtBySignature($stream);
97
    }
98
99
    /**
100
     * Get file extension by file header signature.
101
     *
102
     * @param string $stream
103
     *
104
     * @return string
105
     */
106
    public static function getExtBySignature($stream)
107
    {
108
        $prefix = strval(bin2hex(mb_strcut($stream, 0, 10)));
109
110
        foreach (self::$signatures as $signature => $extension) {
111
            if (0 === strpos($prefix, strval($signature))) {
112
                return $extension;
113
            }
114
        }
115
116
        return '';
117
    }
118
}
119