-
Notifications
You must be signed in to change notification settings - Fork 11
/
api_media.js
70 lines (66 loc) · 2.15 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
var path = require('path');
var fs = require('co-fs');
var formstream = require('formstream');
var util = require('./util');
var postJSON = util.postJSON;
/**
* 上传多媒体文件,分别有图片(image)、语音(voice)、视频(video)和普通文件(file)
* 详情请见:<http://qydev.weixin.qq.com/wiki/index.php?title=上传媒体文件>
* Examples:
* ```
* api.uploadMedia('filepath', type);
* ```
* Result:
* ```
* {"type":"image","media_id":"0000001","created_at":123456789}
* ```
* Shortcut:
*
* - `exports.uploadImage(filepath, callback);`
* - `exports.uploadVoice(filepath, callback);`
* - `exports.uploadVideo(filepath, callback);`
* - `exports.uploadFile(filepath, callback);`
*
* @param {String} filepath 文件路径
* @param {String} type 媒体类型,可用值有image、voice、video、file
*/
exports.uploadMedia = function* (filepath, type) {
var token = yield* this.ensureAccessToken();
var stat = yield fs.stat(filepath);
var form = formstream();
form.file('media', filepath, path.basename(filepath), stat.size);
var url = this.prefix + 'media/upload?access_token=' + token.accessToken + '&type=' + type;
var opts = {
dataType: 'json',
type: 'POST',
timeout: 60000, // 60秒超时
headers: form.headers(),
data: form
};
return yield* this.request(url, opts);
};
['image', 'voice', 'video', 'file'].forEach(function (type) {
var method = 'upload' + type[0].toUpperCase() + type.substring(1);
exports[method] = function* (filepath, callback) {
return yield* this.uploadMedia(filepath, type);
};
});
/**
* 根据媒体ID获取媒体内容
* 详情请见:<http://qydev.weixin.qq.com/wiki/index.php?title=获取媒体文件>
* Examples:
* ```
* var ret = yield api.getMedia(mediaId);
* ```
* - `result`, 调用正常时得到的文件Buffer对象
*
* @param {String} mediaId 媒体文件的ID
*/
exports.getMedia = function* (mediaId) {
var token = yield* this.ensureAccessToken();
var url = this.prefix + 'media/get?access_token=' + token.accessToken + '&media_id=' + mediaId;
var opts = {
timeout: 60000 // 60秒超时
};
return yield* this.request(url, opts);
};