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\Http; |
13
|
|
|
|
14
|
|
|
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException; |
15
|
|
|
use EasyWeChat\Kernel\Exceptions\RuntimeException; |
16
|
|
|
use EasyWeChat\Kernel\Support\File; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class StreamResponse. |
20
|
|
|
* |
21
|
|
|
* @author overtrue <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class StreamResponse extends Response |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @param string $directory |
27
|
|
|
* @param string $filename |
28
|
|
|
* @param bool $appendSuffix |
29
|
|
|
* |
30
|
|
|
* @return bool|int |
31
|
|
|
* |
32
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
33
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\RuntimeException |
34
|
|
|
*/ |
35
|
2 |
|
public function save(string $directory, string $filename = '', bool $appendSuffix = true) |
36
|
|
|
{ |
37
|
2 |
|
$this->getBody()->rewind(); |
38
|
|
|
|
39
|
2 |
|
$directory = rtrim($directory, '/'); |
40
|
|
|
|
41
|
2 |
|
if (!is_dir($directory)) { |
42
|
|
|
mkdir($directory, 0755, true); // @codeCoverageIgnore |
43
|
|
|
} |
44
|
|
|
|
45
|
2 |
|
if (!is_writable($directory)) { |
46
|
1 |
|
throw new InvalidArgumentException(sprintf("'%s' is not writable.", $directory)); |
47
|
|
|
} |
48
|
|
|
|
49
|
2 |
|
$contents = $this->getBody()->getContents(); |
50
|
|
|
|
51
|
2 |
|
if (empty($contents) || '{' === $contents[0]) { |
52
|
1 |
|
throw new RuntimeException('Invalid media response content.'); |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
if (empty($filename)) { |
56
|
1 |
|
if (preg_match('/filename="(?<filename>.*?)"/', $this->getHeaderLine('Content-Disposition'), $match)) { |
57
|
1 |
|
$filename = $match['filename']; |
58
|
|
|
} else { |
59
|
1 |
|
$filename = md5($contents); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
if ($appendSuffix && empty(pathinfo($filename, PATHINFO_EXTENSION))) { |
64
|
1 |
|
$filename .= File::getStreamExt($contents); |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
file_put_contents($directory.'/'.$filename, $contents); |
68
|
|
|
|
69
|
1 |
|
return $filename; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $directory |
74
|
|
|
* @param string $filename |
75
|
|
|
* @param bool $appendSuffix |
76
|
|
|
* |
77
|
|
|
* @return bool|int |
78
|
|
|
* |
79
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
80
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\RuntimeException |
81
|
|
|
*/ |
82
|
1 |
|
public function saveAs(string $directory, string $filename, bool $appendSuffix = true) |
83
|
|
|
{ |
84
|
1 |
|
return $this->save($directory, $filename, $appendSuffix); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|