UNPKG

6 kBJavaScriptView Raw
1'use strict';
2
3var path = require('path');
4var fs = require('fs');
5
6var formstream = require('formstream');
7
8var util = require('./util');
9var wrapper = util.wrapper;
10
11/**
12 * 新增临时素材,分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb)
13 * 详情请见:<http://mp.weixin.qq.com/wiki/5/963fc70b80dc75483a271298a76a8d59.html>
14 * Examples:
15 * ```
16 * api.uploadMedia('filepath', type, callback);
17 * ```
18 * Callback:
19 *
20 * - `err`, 调用失败时得到的异常
21 * - `result`, 调用正常时得到的对象
22 *
23 * Result:
24 * ```
25 * {"type":"TYPE","media_id":"MEDIA_ID","created_at":123456789}
26 * ```
27 * Shortcut:
28 *
29 * - `exports.uploadImage(filepath, callback);`
30 * - `exports.uploadVoice(filepath, callback);`
31 * - `exports.uploadVideo(filepath, callback);`
32 * - `exports.uploadThumb(filepath, callback);`
33 *
34 * @param {String} filepath 文件路径
35 * @param {String} type 媒体类型,可用值有image、voice、video、thumb
36 * @param {Function} callback 回调函数
37 */
38exports.uploadMedia = function (filepath, type, callback) {
39 this.preRequest(this._uploadMedia, arguments);
40};
41
42/*!
43 * 上传多媒体文件的未封装版本
44 */
45exports._uploadMedia = function (filepath, type, callback) {
46 var that = this;
47 fs.stat(filepath, function (err, stat) {
48 if (err) {
49 return callback(err);
50 }
51 var form = formstream();
52 form.file('media', filepath, path.basename(filepath), stat.size);
53 var url = that.endpoint + '/cgi-bin/media/upload?access_token=' + that.token.accessToken + '&type=' + type;
54 var opts = {
55 dataType: 'json',
56 type: 'POST',
57 timeout: 60000, // 60秒超时
58 headers: form.headers(),
59 stream: form
60 };
61 that.request(url, opts, wrapper(callback));
62 });
63};
64
65['image', 'voice', 'video', 'thumb'].forEach(function (type) {
66 var method = 'upload' + type[0].toUpperCase() + type.substring(1);
67 exports[method] = function (filepath, callback) {
68 this.uploadMedia(filepath, type, callback);
69 };
70});
71
72/**
73 * 获取临时素材
74 * 详情请见:<http://mp.weixin.qq.com/wiki/11/07b6b76a6b6e8848e855a435d5e34a5f.html>
75 * Examples:
76 * ```
77 * api.getMedia('media_id', callback);
78 * ```
79 * Callback:
80 *
81 * - `err`, 调用失败时得到的异常
82 * - `result`, 调用正常时得到的文件Buffer对象
83 * - `res`, HTTP响应对象
84 *
85 * @param {String} mediaId 媒体文件的ID
86 * @param {Function} callback 回调函数
87 */
88exports.getMedia = function (mediaId, callback) {
89 this.preRequest(this._getMedia, arguments);
90};
91
92/*!
93 * 获取临时素材的未封装版本
94 */
95exports._getMedia = function (mediaId, callback) {
96 var url = this.endpoint + '/cgi-bin/media/get?access_token=' + this.token.accessToken + '&media_id=' + mediaId;
97 var opts = {
98 timeout: 60000 // 60秒超时
99 };
100 this.request(url, opts, wrapper(function (err, data, res) {
101 // handle some err
102 if (err) {
103 return callback(err);
104 }
105 var contentType = res.headers['content-type'];
106 if (contentType === 'application/json' || contentType === 'text/plain') {
107 var ret;
108 try {
109 ret = JSON.parse(data);
110 if (ret.errcode) {
111 err = new Error(ret.errmsg);
112 err.name = 'WeChatAPIError';
113 }
114 } catch (ex) {
115 callback(ex, data, res);
116 return;
117 }
118 return callback(err, ret, res);
119 }
120 // 输出Buffer对象
121 callback(null, data, res);
122 }));
123};
124
125
126/**
127 * 上传图文消息内的图片获取URL
128 * 详情请见:<http://mp.weixin.qq.com/wiki/15/5380a4e6f02f2ffdc7981a8ed7a40753.html>
129 * Examples:
130 * ```
131 * api.uploadImage('filepath');
132 * ```
133 * Callback:
134 *
135 * - `err`, 调用失败时得到的异常
136 * - `result`, 调用正常时得到的对象
137 *
138 * Result:
139 * ```
140 * {"url": "http://mmbiz.qpic.cn/mmbiz/gLO17UPS6FS2xsypf378iaNhWacZ1G1UplZYWEYfwvuU6Ont96b1roYsCNFwaRrSaKTPCUdBK9DgEHicsKwWCBRQ/0"}
141 * ```
142 *
143 * @param {String} filepath 图片文件路径
144 * @param {Function} callback 回调函数
145 */
146exports.uploadImage = function (filepath, callback) {
147 this.preRequest(this._uploadImage, arguments);
148};
149
150/*!
151 * 上传图片未封装版本
152 */
153exports._uploadImage = function (filepath, callback) {
154 var that = this;
155 fs.stat(filepath, function (err, stat) {
156 if (err) {
157 return callback(err);
158 }
159 var form = formstream();
160 form.file('media', filepath, path.basename(filepath), stat.size);
161 var url = that.endpoint + '/cgi-bin/media/uploadimg?access_token=' + that.token.accessToken;
162 var opts = {
163 dataType: 'json',
164 type: 'POST',
165 timeout: 60000, // 60秒超时
166 headers: form.headers(),
167 stream: form
168 };
169 that.request(url, opts, wrapper(callback));
170 });
171};
172
173/**
174 * 上传来自上游管道的图文消息内的图片,并获取URL。
175 * 拓展于uploadImage,用于客户端直接上传文件管道重定向到微信服务器,不经过自身缓存服务器文件。
176 * Examples:
177 * ```
178 * api.uploadImageStream(req, callback);
179 * ```
180 * Callback:
181 *
182 * - `err`, 调用失败时得到的异常
183 * - `result`, 调用正常时得到的对象
184 *
185 * Result:
186 * ```
187 * {"url": "http://mmbiz.qpic.cn/mmbiz/gLO17UPS6FS2xsypf378iaNhWacZ1G1UplZYWEYfwvuU6Ont96b1roYsCNFwaRrSaKTPCUdBK9DgEHicsKwWCBRQ/0"}
188 * ```
189 *
190 * @param {Object} req 上游Stream对象,必须包含headers属性;例如expressjs中request对象。
191 * @param {Function} callback 回调函数
192 */
193exports.uploadImageStream = function (req, callback) {
194 this.preRequest(this._uploadImageStream, arguments);
195};
196
197/*!
198 * 上传来自上游管道的图文消息内的图片未封装版本
199 */
200exports._uploadImageStream = function (req, callback) {
201 var that = this;
202 var url = that.endpoint + '/cgi-bin/media/uploadimg?access_token=' + that.token.accessToken;
203 var opts = {
204 dataType: 'json',
205 type: 'POST',
206 timeout: 60000, // 60秒超时
207 headers: req.headers,
208 stream: req
209 };
210 delete opts.headers.host;
211 that.request(url, opts, callback);
212};