forked from node-webot/wechat-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_media.js
121 lines (115 loc) · 3.31 KB
/
api_media.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
var path = require('path');
var fs = require('fs');
var formstream = require('formstream');
var util = require('./util');
var wrapper = util.wrapper;
/**
* 新增临时素材,分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb)
* 详情请见:<http://mp.weixin.qq.com/wiki/5/963fc70b80dc75483a271298a76a8d59.html>
* Examples:
* ```
* api.uploadMedia('filepath', type, callback);
* ```
* Callback:
*
* - `err`, 调用失败时得到的异常
* - `result`, 调用正常时得到的对象
*
* Result:
* ```
* {"type":"TYPE","media_id":"MEDIA_ID","created_at":123456789}
* ```
* Shortcut:
*
* - `exports.uploadImage(filepath, callback);`
* - `exports.uploadVoice(filepath, callback);`
* - `exports.uploadVideo(filepath, callback);`
* - `exports.uploadThumb(filepath, callback);`
*
* @param {String} filepath 文件路径
* @param {String} type 媒体类型,可用值有image、voice、video、thumb
* @param {Function} callback 回调函数
*/
exports.uploadMedia = function (filepath, type, callback) {
this.preRequest(this._uploadMedia, arguments);
};
/*!
* 上传多媒体文件的未封装版本
*/
exports._uploadMedia = function (filepath, type, callback) {
var that = this;
fs.stat(filepath, function (err, stat) {
if (err) {
return callback(err);
}
var form = formstream();
form.file('media', filepath, path.basename(filepath), stat.size);
var url = that.prefix + 'media/upload?access_token=' + that.token.accessToken + '&type=' + type;
var opts = {
dataType: 'json',
type: 'POST',
timeout: 60000, // 60秒超时
headers: form.headers(),
stream: form
};
that.request(url, opts, wrapper(callback));
});
};
['image', 'voice', 'video', 'thumb'].forEach(function (type) {
var method = 'upload' + type[0].toUpperCase() + type.substring(1);
exports[method] = function (filepath, callback) {
this.uploadMedia(filepath, type, callback);
};
});
/**
* 获取临时素材
* 详情请见:<http://mp.weixin.qq.com/wiki/11/07b6b76a6b6e8848e855a435d5e34a5f.html>
* Examples:
* ```
* api.getMedia('media_id', callback);
* ```
* Callback:
*
* - `err`, 调用失败时得到的异常
* - `result`, 调用正常时得到的文件Buffer对象
* - `res`, HTTP响应对象
*
* @param {String} mediaId 媒体文件的ID
* @param {Function} callback 回调函数
*/
exports.getMedia = function (mediaId, callback) {
this.preRequest(this._getMedia, arguments);
};
/*!
* 获取临时素材的未封装版本
*/
exports._getMedia = function (mediaId, callback) {
var url = this.prefix + 'media/get?access_token=' + this.token.accessToken + '&media_id=' + mediaId;
var opts = {
timeout: 60000 // 60秒超时
};
this.request(url, opts, wrapper(function (err, data, res) {
// handle some err
if (err) {
return callback(err);
}
var contentType = res.headers['content-type'];
if (contentType === 'application/json') {
var ret;
try {
ret = JSON.parse(data);
if (ret.errcode) {
err = new Error(ret.errmsg);
err.name = 'WeChatAPIError';
}
} catch (ex) {
callback(ex, data, res);
return;
}
callback(err, ret, res);
} else {
// 输出Buffer对象
callback(null, data, res);
}
}));
};