1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the light/easemob. |
5
|
|
|
* |
6
|
|
|
* (c) lichunqiang <[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 light\Easemob\Rest; |
13
|
|
|
|
14
|
|
|
use Psr\Http\Message\RequestInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class File. |
18
|
|
|
* |
19
|
|
|
* ~~~ |
20
|
|
|
* $file = $easemob->file; |
21
|
|
|
* $result = $file->upload('/home/1.png'); |
22
|
|
|
* $file_url = $file->url($result['uuid']); |
23
|
|
|
* ~~~ |
24
|
|
|
*/ |
25
|
|
|
class File extends Rest |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
protected function registerHttpMiddleware() |
31
|
|
|
{ |
32
|
|
|
parent::registerHttpMiddleware(); |
33
|
|
|
|
34
|
|
|
$this->http->addMiddleware($this->RestrictAccessMiddleware()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return \Closure |
39
|
|
|
*/ |
40
|
|
|
public function RestrictAccessMiddleware() |
41
|
|
|
{ |
42
|
|
|
return function (callable $handler) { |
43
|
|
|
return function (RequestInterface $request, array $options) use ($handler) { |
44
|
|
|
|
45
|
|
|
if (!$this->accessToken->getToken()) { |
46
|
|
|
return $handler($request, $options); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$request = $request->withHeader('restrict-access ', 'true'); |
50
|
|
|
|
51
|
|
|
return $handler($request, $options); |
52
|
|
|
}; |
53
|
|
|
}; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* 上传文件. |
58
|
|
|
* |
59
|
|
|
* ~~~ |
60
|
|
|
* [ |
61
|
|
|
* 'uuid' => 'd8329590-c018-11e5-a936-0f89b914ff91', |
62
|
|
|
* 'type' => 'chatfile', |
63
|
|
|
* 'share-secret' => '2DKVmsAYEeWn7wsNJSQy-QxD0mQSSXd6jqC7PpD7_YQuIDr6' |
64
|
|
|
* ] |
65
|
|
|
* ~~~ |
66
|
|
|
* |
67
|
|
|
* @param string $file |
68
|
|
|
* |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
public function upload($file) |
72
|
|
|
{ |
73
|
|
|
$response = $this->http->upload('chatfiles', [ |
74
|
|
|
'file' => $file, |
75
|
|
|
]); |
76
|
|
|
|
77
|
|
|
return array_shift($this->parseResponse($response)['entities']); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Return the resource web access url. |
82
|
|
|
* |
83
|
|
|
* @param string $uuid |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function url($uuid) |
88
|
|
|
{ |
89
|
|
|
return (string) $this->http->buildUri("chatfiles/{$uuid}"); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: