Completed
Pull Request — master (#292)
by Carlos
05:19 queued 02:03
created

File   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 5
c 4
b 1
f 0
lcom 1
cbo 0
dl 0
loc 81
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getStreamExt() 0 8 2
A getExtBySignature() 0 12 3
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
/**
13
 * File.php.
14
 *
15
 * @author    overtrue <[email protected]>
16
 * @copyright 2015 overtrue <[email protected]>
17
 *
18
 * @link      https://github.com/overtrue
19
 * @link      http://overtrue.me
20
 */
21
namespace EasyWeChat\Support;
22
23
use finfo;
24
25
/**
26
 * Class File.
27
 */
28
class File
29
{
30
    /**
31
     * MIME mapping.
32
     *
33
     * @var array
34
     */
35
    protected static $extensionMap = array(
36
        'audio/wav' => '.wav',
37
        'audio/x-ms-wma' => '.wma',
38
        'video/x-ms-wmv' => '.wmv',
39
        'video/mp4' => '.mp4',
40
        'audio/mpeg' => '.mp3',
41
        'audio/amr' => '.amr',
42
        'application/vnd.rn-realmedia' => '.rm',
43
        'audio/mid' => '.mid',
44
        'image/bmp' => '.bmp',
45
        'image/gif' => '.gif',
46
        'image/png' => '.png',
47
        'image/tiff' => '.tiff',
48
        'image/jpeg' => '.jpg',
49
    );
50
51
    /**
52
     * File header signatures.
53
     *
54
     * @var array
55
     */
56
    protected static $signatures = [
57
        'ffd8ff' => '.jpg',
58
        '424d' => '.bmp',
59
        '47494638' => '.gif',
60
        '89504e47' => '.png',
61
        '494433' => '.mp3',
62
        'fffb' => '.mp3',
63
        'fff3' => '.mp3',
64
        '3026b2758e66cf11' => '.wma',
65
        '52494646' => '.wav',
66
        '57415645' => '.wav',
67
        '41564920' => '.avi',
68
        '000001ba' => '.mpg',
69
        '000001b3' => '.mpg',
70
        '2321414d52' => '.amr',
71
    ];
72
73
    /**
74
     * Return steam extension.
75
     *
76
     * @param string $stream
77
     *
78
     * @return string
79
     */
80
    public static function getStreamExt($stream)
81
    {
82
        $finfo = new finfo(FILEINFO_MIME);
83
84
        $mime = strstr($finfo->buffer($stream), ';', true);
85
86
        return isset(self::$extensionMap[$mime]) ? self::$extensionMap[$mime] : self::getExtBySignature($stream);
87
    }
88
89
    /**
90
     * Get file extension by file header signature.
91
     *
92
     * @param string $stream
93
     *
94
     * @return string
95
     */
96
    public static function getExtBySignature($stream)
97
    {
98
        $prefix = strval(bin2hex(mb_strcut($stream, 0, 10)));
99
100
        foreach (self::$signatures as $signature => $extension) {
101
            if (0 === strpos($prefix, strval($signature))) {
102
                return $extension;
103
            }
104
        }
105
106
        return '';
107
    }
108
}
109