Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge Develop #292

Merged
merged 7 commits into from
Feb 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md → .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ EasyWeChat 是一个开源的,社区驱动的项目。

## 报告 Bug

当你在使用过程中遇到问题,请查阅 [疑难解答](troubleshooting.html) 或者在这里提问 [GitHub](https://github.com/overtrue/wechat/issues). 如果还是不能解决你的问题,请到 GitHub 联系我们。
当你在使用过程中遇到问题,请查阅 [疑难解答](http://easywechat.org/docs/zh-cn/troubleshooting.html) 或者在这里提问 [GitHub](https://github.com/overtrue/wechat/issues). 如果还是不能解决你的问题,请到 GitHub 联系我们。
9 changes: 9 additions & 0 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## 我用的环境

PHP 版本:
overtrue/wechat 版本:

## 问题及现象

描述你的问题现象,报错贴截图粘贴或者贴具体信息

5 changes: 5 additions & 0 deletions src/Material/Material.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ public function get($mediaId)

$json = $this->getHttp()->parseJSON($response);

// XXX: 微信开发这帮混蛋,尼玛文件二进制输出不带header,简直日了!!!
if (!$json) {
return $response->getBody();
}

$this->checkAndThrow($json);

return $json;
Expand Down
11 changes: 1 addition & 10 deletions src/Material/Temporary.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

use EasyWeChat\Core\AbstractAPI;
use EasyWeChat\Core\Exceptions\InvalidArgumentException;
use EasyWeChat\Core\Exceptions\RuntimeException;
use EasyWeChat\Support\File;

/**
Expand Down Expand Up @@ -81,15 +80,7 @@ public function getStream($mediaId)
{
$response = $this->getHttp()->get(self::API_GET, ['media_id' => $mediaId]);

foreach ($response->getHeader('Content-Type') as $mime) {
if (preg_match('/(image|video|audio)/i', $mime)) {
return $response->getBody();
}
}

$this->checkAndThrow($this->getHttp()->parseJSON($response));

throw new RuntimeException('Unknown API response.'.$response->getBody());
return $response->getBody();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Staff/MessageBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function __construct(Staff $staff)
/**
* Set message to send.
*
* @param AbstractMessage $message
* @param string|AbstractMessage $message
*
* @return MessageBuilder
*
Expand Down
7 changes: 1 addition & 6 deletions src/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@
/**
* Class Collection.
*/
class Collection implements
ArrayAccess,
Countable,
IteratorAggregate,
JsonSerializable,
Serializable
class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Serializable
{
/**
* The collection data.
Expand Down
73 changes: 43 additions & 30 deletions src/Support/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,6 @@ class File
* @var array
*/
protected static $extensionMap = array(
'application/msword' => '.doc',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => '.docx',
'application/rtf' => '.rtf',
'application/vnd.ms-excel' => '.xls',
'application/x-excel' => '.xls',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => '.xlsx',
'application/vnd.ms-powerpoint' => '.ppt',
'application/vnd.openxmlformats-officedocument.presentationml.presentation' => '.pptx',
'application/vnd.ms-powerpoint' => '.pps',
'application/vnd.openxmlformats-officedocument.presentationml.slideshow' => '.ppsx',
'application/pdf' => '.pdf',
'application/x-shockwave-flash' => '.swf',
'application/x-msdownload' => '.dll',
'application/octet-stream' => '.exe',
'application/octet-stream' => '.msi',
'application/octet-stream' => '.chm',
'application/octet-stream' => '.cab',
'application/octet-stream' => '.ocx',
'application/octet-stream' => '.rar',
'application/x-tar' => '.tar',
'application/x-compressed' => '.tgz',
'application/x-zip-compressed' => '.zip',
'application/x-compress' => '.z',
'audio/wav' => '.wav',
'audio/x-ms-wma' => '.wma',
'video/x-ms-wmv' => '.wmv',
Expand All @@ -69,14 +46,30 @@ class File
'image/png' => '.png',
'image/tiff' => '.tiff',
'image/jpeg' => '.jpg',
'text/plain' => '.txt',
'text/xml' => '.xml',
'text/html' => '.html',
'text/css' => '.css',
'text/javascript' => '.js',
'message/rfc822' => '.mhtml',
);

/**
* File header signatures.
*
* @var array
*/
protected static $signatures = [
'ffd8ff' => '.jpg',
'424d' => '.bmp',
'47494638' => '.gif',
'89504e47' => '.png',
'494433' => '.mp3',
'fffb' => '.mp3',
'fff3' => '.mp3',
'3026b2758e66cf11' => '.wma',
'52494646' => '.wav',
'57415645' => '.wav',
'41564920' => '.avi',
'000001ba' => '.mpg',
'000001b3' => '.mpg',
'2321414d52' => '.amr',
];

/**
* Return steam extension.
*
Expand All @@ -90,6 +83,26 @@ public static function getStreamExt($stream)

$mime = strstr($finfo->buffer($stream), ';', true);

return isset(self::$extensionMap[$mime]) ? self::$extensionMap[$mime] : '';
return isset(self::$extensionMap[$mime]) ? self::$extensionMap[$mime] : self::getExtBySignature($stream);
}

/**
* Get file extension by file header signature.
*
* @param string $stream
*
* @return string
*/
public static function getExtBySignature($stream)
{
$prefix = strval(bin2hex(mb_strcut($stream, 0, 10)));

foreach (self::$signatures as $signature => $extension) {
if (0 === strpos($prefix, strval($signature))) {
return $extension;
}
}

return '';
}
}